ZipOutputStream输出流

来源:互联网 发布:公安网络报警平台 编辑:程序博客网 时间:2024/06/18 15:27

转自:http://blog.csdn.net/z69183787/article/details/8290811


public static void zip(File zf, File[] sf) {//sf 需要压缩的所有文件,zf 输出的压缩包的文件路径
byte[] buf = new byte[1024];
try {
FileOutputStream fos = new FileOutputStream(zf);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(fos));
for (int i = 0; i < sf.length; i++) {
FileInputStream in = new FileInputStream(sf[i]);
out.putNextEntry(new ZipEntry("" + sf[i].getName()));//把所有文件压缩到一个压缩包里面
int len;
if(in.available() > 0){
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
out.closeEntry();
in.close();
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}