java读取zip文件和压缩zip文件

来源:互联网 发布:淘宝服装货源怎么找 编辑:程序博客网 时间:2024/05/17 22:28

java读取名为T11.zip的文件,解压后再生成名为T22.zip的文件。

代码如下:

import java.io.*;import java.util.zip.*;public class ReadWriteZip {public static void main(String[] args) throws Exception {FileInputStream fi = new FileInputStream("/home/tom/test/T11.zip");ZipInputStream zi = new ZipInputStream(fi);FileOutputStream fo = new FileOutputStream("/home/tom/test/T22.zip");ZipOutputStream zo = new ZipOutputStream(fo);ZipEntry ze;while ((ze = zi.getNextEntry()) != null) {zo.putNextEntry(ze);byte[] buffer = new byte[1024];int len;while ((len = zi.read(buffer)) > 0) {zo.write(buffer, 0, len);}}zi.closeEntry();zo.closeEntry();zi.close();zo.close();}}