Java 使用ZipOutputStream 进行打包操作

64 篇文章 1 订阅
36 篇文章 0 订阅

原文转自:http://www.tqcto.com/article/code/295139.html

打包单个文件:


[java] 

  1. public static void main(String[] args) throws IOException{  

  2.         File file = new File("d:" + File.separator + "aaa.java");  

  3.         File zipFile = new File("d:" + File.separator + "hello.zip");  

  4.         InputStream input = new FileInputStream(file);  

  5.         ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(  

  6.                 zipFile));  

  7.         zipOut.putNextEntry(new ZipEntry(file.getName()));  

  8.         // 设置注释  

  9.         zipOut.setComment("hello");  

  10.         int temp = 0;  

  11.         while((temp = input.read()) != -1){  

  12.             zipOut.write(temp);  

  13.         }  

  14.         input.close();  

  15.         zipOut.close();  

  16.     }  




打包多个文件:


[java] 

  1. public static void main(String[] args) throws IOException{  

  2.         // 要被压缩的文件夹  

  3.         File file = new File("d:" + File.separator + "temp");  

  4.         File zipFile = new File("d:" + File.separator + "zipFile.zip");  

  5.         InputStream input = null;  

  6.         ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(  

  7.                 zipFile));  

  8.         zipOut.setComment("hello");  

  9.         if(file.isDirectory()){  

  10.             File[] files = file.listFiles();  

  11.             for(int i = 0; i < files.length; ++i){  

  12.                 input = new FileInputStream(files[i]);  

  13.                 zipOut.putNextEntry(new ZipEntry(file.getName()  

  14.                         + File.separator + files[i].getName()));  

  15.                 int temp = 0;  

  16.                 while((temp = input.read()) != -1){  

  17.                     zipOut.write(temp);  

  18.                 }  

  19.                 input.close();  

  20.             }  

  21.         }  

  22.         zipOut.close();  

  23.     }  


大家自然想到,既然能压缩,自然能解压缩,在谈解压缩之前,我们会用到一个ZipFile类,先给一个这个例子吧。java中的每一个压缩文件都是可以使用ZipFile来进行表示的


[java] 

  1. import java.io.File;  

  2. import java.io.IOException;  

  3. import java.util.zip.ZipFile;  

  4.    

  5. /** 

  6.  * ZipFile演示 

  7.  * */  

  8. public class ZipFileDemo{  

  9.     public static void main(String[] args) throws IOException{  

  10.         File file = new File("d:" + File.separator + "hello.zip");  

  11.         ZipFile zipFile = new ZipFile(file);  

  12.         System.out.println("压缩文件的名称为:" + zipFile.getName());  

  13.     }  

  14. }  


解压单个文件:


[java] 

  1. public static void main(String[] args) throws IOException{  

  2.        File file = new File("d:" + File.separator + "hello.zip");  

  3.        File outFile = new File("d:" + File.separator + "unZipFile.txt");  

  4.        ZipFile zipFile = new ZipFile(file);  

  5.        ZipEntry entry = zipFile.getEntry("hello.txt");//hello.txt 为压缩包中文件的名称  

  6.        InputStream input = zipFile.getInputStream(entry);  

  7.        OutputStream output = new FileOutputStream(outFile);  

  8.        int temp = 0;  

  9.        while((temp = input.read()) != -1){  

  10.            output.write(temp);  

  11.        }  

  12.        input.close();  

  13.        output.close();  

  14.    }  


解压多个文件:


当我们需要解压缩多个文件的时候,ZipEntry就无法使用了,如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类

[java] 

  1. public static void main(String[] args) throws IOException{  

  2.         File file = new File("d:" + File.separator + "zipFile.zip");  

  3.         File outFile = null;  

  4.         ZipFile zipFile = new ZipFile(file);  

  5.         ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));  

  6.         ZipEntry entry = null;  

  7.         InputStream input = null;  

  8.         OutputStream output = null;  

  9.         while((entry = zipInput.getNextEntry()) != null){  

  10.             System.out.println("解压缩" + entry.getName() + "文件");//entry.getName()获得压缩包内的文件及文件路径(aaaa/bb.txt)  

  11.             outFile = new File("d:" + File.separator + entry.getName());  

  12.             if(!outFile.getParentFile().exists()){  

  13.                 outFile.getParentFile().mkdir();  

  14.             }  

  15.             if(!outFile.exists()){  

  16.                 outFile.createNewFile();  

  17.             }  

  18.             input = zipFile.getInputStream(entry);  

  19.             output = new FileOutputStream(outFile);  

  20.             int temp = 0;  

  21.             while((temp = input.read()) != -1){  

  22.                 output.write(temp);  

  23.             }  

  24.             input.close();  

  25.             output.close();  

  26.         }  

  27.     }  



原文转自:编程技术

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值