文件压缩
来源:互联网 发布:淘宝店能贷 编辑:程序博客网 时间: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);
}
}
}
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
- 文件压缩和解压缩
- 文件压缩
- 文件压缩
- 文件压缩
- 文件压缩
- 文件压缩
- 文件压缩
- 文件压缩
- 文件压缩
- 文件压缩
- 文件压缩
- 文件压缩
- 文件压缩
- 文件压缩
- 文件压缩
- 文件压缩
- 文件压缩
- 文件压缩
- [字符串]Codeforces Round #307 (Div. 2) B.ZgukistringZ
- codeforces 374C C. Inna and Dima(DFS)
- spring mvc的学习
- Java面试题全集(中)
- Hibernate主键生成器详解
- 文件压缩
- IOS中CALayer的使用
- C++中函数的定义和声明深度解析
- Python的Django框架中的Context使用
- 领域对象的关系体现到数据库
- 工业视觉检测关键技术
- html5的基础标记
- fieldset背景溢出BUG的解决
- [原]iOS动态特性初研究(利用JSON动态创建类型和对象)