java中实现zip的压缩与解压缩。java自带的 能实现的功能比较有限。
本程序功能:实现简单的压缩和解压缩,压缩文件夹下的所有文件(文件过滤的话需要对File进一步细节处理)。
对中文的支持需要使用java7或java8,可以在ZipOutputStream和ZipInputStream中指定Charset参数,详见API中的构造参数。
1.压缩文件或文件夹
1 public void zip() throws IOException { 2 File fileSrc = new File("E:\\abc"); 3 File destFile = new File("E:\\abc.zip"); 4 zip(fileSrc,destFile); 5 }
1 public void zip(File fileSrc,File dectFile) throws IOException { 2 ZipOutputStream zipOutputStream = new ZipOutputStream(new CheckedOutputStream(new FileOutputStream(dectFile),new CRC32())); 3 String name = fileSrc.getName(); 4 zip(zipOutputStream, name,fileSrc); 5 zipOutputStream.flush(); 6 zipOutputStream.close(); 7 }