zip压缩和解压缩

package fanyongjun.zip;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**\
 * zip压缩与解压缩
 * @author Frenzy Fan
 *
 */
public class ZipDemo {
 public static void main(String[] args) {
  FileOutputStream fout;
   try {
    //读取文件
    FileInputStream fis=new FileInputStream("d:/hello.txt"); 
    //压缩流
   fout=new FileOutputStream("d:/hello.zip");
   ZipOutputStream zout=new ZipOutputStream(fout);
   //压缩条目
   ZipEntry entry=new ZipEntry("");
   //放入下一个条目
   zout.putNextEntry(entry); 
   byte[] buffer=new byte[1024];
   int len=-1;
   while((len=fis.read(buffer))!=-1) {
  zout.write(buffer,0,len);   
   }
   zout.closeEntry();
   zout.close();
   fout.close();
   fis.close();
   System.out.println("压缩成功了");
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public void zipFIles() {
  try {
   //hello.txt
   FileInputStream fis=new FileInputStream("d:/hello.txt");
   ZipOutputStream zout=new ZipOutputStream(new FileOutputStream("d:/test.zip"));
   //在压缩输出放置下一个条目
   ZipEntry entry=new ZipEntry("hello.txt");
   zout.putNextEntry(entry);
   //开始写入数据
   byte[]buffer=new byte[1024];
   int len=-1;
   while((len=fis.read(buffer))!=-1) {
    zout.write(buffer, 0, len);
    }
   zout.closeEntry();
   fis.close();
    //hello.txt
     fis=new FileInputStream("d:/beijing.jpg");
    //在压缩输出放置下一个条目
    zout.putNextEntry(new ZipEntry("beijing.jpg"));  
    while((len=fis.read(buffer))!=-1) {
     zout.write(buffer, 0, len);
     }
    zout.closeEntry();
    zout.close();
    fis.close();
    fis.close();
System.out.println("压缩完成");
   }    
  catch (Exception e) {
   e.printStackTrace();
  }
 }
 public void zipFIles2() {
  try {
   //hello.txt
   FileInputStream fis=new FileInputStream("d:/hello.txt");
   ZipOutputStream zout=new ZipOutputStream(new FileOutputStream("d:/test.zip"));
   //在压缩输出放置下一个条目
   ZipEntry entry=new ZipEntry("hello.txt");
   //设置未压缩文件的大小
   entry.setSize(fis.available());
   zout.putNextEntry(entry);
   //开始写入数据
   byte[]buffer=new byte[1024];
   int len=-1;
   while((len=fis.read(buffer))!=-1) {
    zout.write(buffer, 0, len);
    }
   zout.closeEntry();
   fis.close();
    //hello.txt
     fis=new FileInputStream("d:/beijing.jpg");
    //在压缩输出放置下一个条目
    entry= new ZipEntry("beijing.jpg");
    entry.setSize(fis.available());
    zout.putNextEntry(entry);   
    while((len=fis.read(buffer))!=-1) {
     zout.write(buffer, 0, len);
     }
    zout.closeEntry();
    zout.close();
    fis.close();
    fis.close();
System.out.println("压缩完成");
   }  
  catch (Exception e) {
   e.printStackTrace();
  }
 }
 /**
  * 解压缩
  * @throws Exception 
  */
 public void unzip() throws Exception {
  ZipInputStream zin = null;
  try {
    zin=new ZipInputStream(new FileInputStream("D:/hello.zip"));
   ZipEntry entry=null;
   byte[]buffer=new byte[1024];
   int len=-1;
   while((entry=zin.getNextEntry())!=null) {
    FileOutputStream fos=new FileOutputStream("d:/hello2"+entry.getName());
    while((len=zin.read(buffer))!=-1) {
     fos.write(buffer,0,len); 
    }
    fos.close();
   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  zin.close();
  System.out.println("解压缩完成");
 }
 /**
  * 解压缩2
  * @throws Exception 
  */
 public void unzip2() throws Exception {
  ZipInputStream zin=new ZipInputStream(new FileInputStream("D:/hello.zip"));
  ZipEntry entry=null;
  byte[]buffer=new byte[1024];
  int len=-1;
  while((entry=zin.getNextEntry())!=null) {
   long size=entry.getSize();
   System.out.println(size);
   FileOutputStream fos=new FileOutputStream("d:/hello2"+entry.getName());  
  }
  zin.close();
  System.out.println("解压缩完成");
 }
}
发布了61 篇原创文章 · 获赞 114 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_45822638/article/details/104880459
今日推荐