文件压缩

来源:互联网 发布:淘宝店能贷 编辑:程序博客网 时间:2024/05/17 03:14
public byte[] getZipBytes(List<ReportDownload> list){
Properties pro = System.getProperties();
String osName = pro.getProperty("os.name");
String basePath = reportExportService.getExportFilePath();
ByteArrayOutputStream bos = null;
ZipOutputStream out = null;
try{
bos = new ByteArrayOutputStream();
out = new ZipOutputStream(bos);
for (ReportDownload item : list) {
String filePath = basePath + File.separator + item.getReportId() + File.separator + item.getName();
File file = new File(filePath);
if(!file.exists()){
throw new ReportException(item.getName() + "文件不存在!");
}
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));  
ZipEntry entry = new ZipEntry(file.getName());
if("Linux".equals(osName)||"linux".equals(osName)){
entry.setUnixMode(644);
}
out.putNextEntry(entry);  
int count;  
byte data[] = new byte[8192];  
while ((count = bis.read(data, 0, 8192)) != -1){  
out.write(data, 0, count);  
}  
bis.close();  
}
out.flush();
out.close();
return bos.toByteArray();
   }catch (Exception e){
    e.printStackTrace();
    throw new ReportException("文件下载失败" + e);  
   }finally{
try {
    if(bos != null)bos.close();
} catch (IOException e) {
e.printStackTrace();
throw new ReportException("文件下载失败" + e);
}
   }
    } 
0 0
原创粉丝点击