Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream)

           Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream)

                                               作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

  之前我已经分享过很多的Java的IO流了,和其他的IO流用法类似,我们要介绍的是压缩流,使用方法很简单。话不多说,一切尽在注释中。

一.压缩文件

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.compress;
 8 
 9 import java.io.FileOutputStream;
10 import java.util.zip.ZipEntry;
11 import java.util.zip.ZipOutputStream;
12 
13 public class Zip {
14     public static void main(String[] args) {
15         try {
16             testZip();
17         } catch (Exception e) {
18             e.printStackTrace();
19         }
20     }
21 
22 
23     public static void testZip() throws Exception {
24         FileOutputStream fos = new FileOutputStream("D:\\BigData\\JavaSE\\yinzhengjieData\\yinzhengjie.zip") ;
25         ZipOutputStream zos= new ZipOutputStream(fos) ;
26         //写入一个条目,我们需要给这个条目起个名字,相当于起一个文件名称
27         zos.putNextEntry(new ZipEntry("yinzhengjie_entry1"));
28         //往这个条目中写入一定的数据
29         zos.write("尹正杰".getBytes());
30         //关闭条目
31         zos.closeEntry();
32 
33         zos.putNextEntry(new ZipEntry("yinzhengjie_entry2"));
34         zos.write("BigData".getBytes());
35         zos.closeEntry();
36 
37         zos.putNextEntry(new ZipEntry("yinzhengjie_entry3"));
38         zos.write("2018".getBytes());
39         zos.closeEntry();
40         zos.close();
41     }
42 }

  压缩后我们可以看到指定目录中生成了zip格式的压缩文件如下:

二.解压文件

 1 /*
 2 @author :yinzhengjie
 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
 4 EMAIL:[email protected]
 5 */
 6 
 7 package cn.org.yinzhengjie.compress;
 8 
 9 import java.io.FileInputStream;
10 import java.io.FileOutputStream;
11 import java.util.zip.ZipEntry;
12 import java.util.zip.ZipInputStream;
13 
14 public class Unzip {
15 
16     public static void main(String[] args) {
17         try {
18             testUnzip();
19         } catch (Exception e) {
20             e.printStackTrace();
21         }
22     }
23 
24 
25     public static void testUnzip() throws Exception {
26         FileInputStream fis = new FileInputStream("D:\\BigData\\JavaSE\\yinzhengjieData\\yinzhengjie.zip");
27         ZipInputStream zis = new ZipInputStream(fis);
28         ZipEntry entry = null ;
29         byte[] buf = new byte[1024] ;
30         int len = 0 ;
31         //此处我们需要判断是否zis流中存在条目,你可以理解为是否存在文件内容
32         while((entry = zis.getNextEntry()) != null){
33             //此处我们获取条目名称
34             String name = entry.getName();
35             System.out.println(name);
36             //我们将每一个条目进行解压,我们需要指定一个输出路径
37             FileOutputStream fos = new FileOutputStream("D:\\BigData\\JavaSE\\yinzhengjieData\\unzip\\"+ name) ;
38             while((len = zis.read(buf)) != -1){
39                 fos.write(buf , 0 , len);
40             }
41             fos.close();
42         }
43         zis.close();
44         fis.close();
45     }
46 }
47 
48 /*
49 以上代码执行结果如下:
50     yinzhengjie_entry1
51     yinzhengjie_entry2
52     yinzhengjie_entry3
53  */

  解压后我们查看指定目录中存在的文件如下:

猜你喜欢

转载自www.cnblogs.com/yinzhengjie/p/9240106.html