JDK自带的ZipOutputStream压缩文件源码

/**
*
* @param files 下载的文件组
* @param busiDate 营业日
* @param path 文件所在目录
* @param branchCode 网点的机构代码
*/
public void downVoucher(File[] files,String busiDate,String path,String branchCode){
if(files.length <= 0)
return;
byte[] bt=new byte[8192];
String strzipName = path+"\\"+branchCode+".zip";
String strs = branchCode+".zip";
try {
ZipOutputStream zout=new ZipOutputStream(new FileOutputStream(strzipName));
//循环下载每个文件并读取内容
for(int j=0;j<files.length;j++){
FileInputStream is=new FileInputStream(files[j]);
ZipEntry ze=new ZipEntry(URLEncoder.encode(files[j].getName(), "UTF-8"));//设置文件编码格式
zout.putNextEntry(ze);
int len;
//读取下载文件内容
while((len=is.read(bt))>0){
zout.write(bt, 0, len);
}
zout.closeEntry();
is.close();
}
zout.close();
this.toUpload(this.getResponse(),path,strs);//调用下载方法
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

猜你喜欢

转载自jmuboy.iteye.com/blog/1290337