try {
//用這個構造最終壓縮包的輸出流
ZipOutputStream zipoutStream = new ZipOutputStream(new FileOutputStream(zipFile));
InputStream is;
byte[] bufferArea = new byte[1024];//讀寫緩沖區
//循環所有文件,將文件打包
for(Object bi : billlist){
Cdncontractbill bill = (Cdncontractbill)bi;
File file = new File(bill.getPath());
if(file.exists()){
FileInputStream zipinSource = new FileInputStream(file);
int read = 0;
ZipEntry zipEntry = new ZipEntry(file.getName());
zipoutStream.putNextEntry(zipEntry);//定位到該壓縮條目位置,開始寫入文件到壓縮包中
while((read = zipinSource.read(bufferArea, 0, 1024)) != -1)
{
zipoutStream.write(bufferArea, 0, read);
}
zipoutStream.closeEntry();
zipinSource.close();
}
}
zipoutStream.close();
is = new FileInputStream(zipFile);
Filedownload.save(is, "", zipFile.getName());
} catch (Exception e) {
e.printStackTrace();
}
//刪除生成的打包文件
zipFile.delete();