ZIP压缩文件,并下载
来源:互联网 发布:linux lnmp一键安装包 编辑:程序博客网 时间:2024/06/02 15:38
类:
/**
* <br>入口参数:path,filenames
* <br>出口参数:String
* <br>功能介绍:从数据库读取EXCEL文件路径(不包含文件名),名称,压缩成一个zip文件
* @return Zip文件保存路径
*/
public String getZipFilepath(String path, ArrayList filenames) throws
Exception {
String zipurl = path + "Excel.zip";
String dir = path;
ArrayList fileNames = filenames; // 存放文件名,并非含有路径的名字
ArrayList files = new ArrayList(); // 存放文件对象
try {
FileOutputStream fileOut = new FileOutputStream(path + "Excel.zip");
ZipOutputStream zipos = new ZipOutputStream(fileOut);
File rootDir = new File(dir);
listFile(rootDir, fileNames, files);
for (int i = 0; i < files.size(); i++) {
FileInputStream fileIn = new FileInputStream( (File) files.get(i));
zipos.putNextEntry(new ZipEntry( (String) fileNames.get(i)));
byte[] buffer = new byte[1024];
while (fileIn.read(buffer) != -1) {
zipos.write(buffer);
zipos.flush();
}
zipos.closeEntry();
fileIn.close();
}
zipos.close();
}
catch (IOException io) {
io.printStackTrace();
throw new Exception("Zip压缩文件保存失败!");
}
return zipurl;
}
private static void listFile(File parentFile, List nameList, List fileList) {
if (parentFile.isDirectory()) {
File[] files = parentFile.listFiles();
for (int i = 0; i < files.length; i++) {
listFile(files[i], nameList, fileList);
}
}
else {
fileList.add(parentFile);
nameList.add(parentFile.getName());
}
}
JSP:
<%@ page contentType="text/html;charset=gb2312" import="com.jspsmart.upload.*,java.io.*,java.sql.*,oracle.sql.*,org.apache.log4j.*" %><jsp:useBean id="db" scope="page" class="com.ehub.DBDriver"/><%
ArrayList excelNames = new ArrayList();
ArrayList newNames = new ArrayList();
for(int i=0;i<excelNames.size();i++){
String excelid = excelNames.get(i).toString();
download.setExcelId(excelid);
String filename=download.getExcelId();
String fileUrl = path + "/" + filename;
String filePath = download.getFilepath(excelid,fileUrl); // ??excel??
newNames.add(i,filename);
}
String zipPath = download.getZipFilepath(path,newNames);
java.io.File rootDir = new java.io.File(path);
download.delAllFile(rootDir);
download.isAllCheckOut(excelNames);
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
su.setContentDisposition(null);
su.downloadFile(zipPath);
- ZIP压缩文件,并下载
- Android--下载zip压缩文件并解压
- Android--下载zip压缩文件并解压
- php zip压缩文件及下载
- PHP使用ZipArchive类压缩文件成ZIP并下载,兼容Winows和Linux
- python 破解并解压zip压缩文件
- Struts2下载zip压缩文件-不生成临时文件
- zip 压缩文件
- zip压缩文件
- JasperPrint 生成包含.XLSX的ZIP压缩文件下载
- vs2003 asp.net服务端压缩文件并下载
- 下载zip文件并解压
- 下载Zip文件并解压
- Unity下载zip并解压
- 打包zip文件并下载
- Java建立Zip压缩文件
- java Zip压缩文件
- 用java压缩文件 (ZIP)
- 切勿“用电脑来思考”-----给广告新人的建议
- 楚歌 - 张学友 - amour/遥远的她
- CString LPCTSTR LPTSTR 类型的相互转化
- XML文件处理的思考
- 使用Java将Word转为Html或txt等···
- ZIP压缩文件,并下载
- 加载DataGrid样式时的问题
- 删除Windows服务
- 认清VoiceXML -- 一篇老文章.
- 程序员的时间管理---做一个高效的程序员
- VC+ADO+Access2000数据库文件密码的设置和修改
- 一种巧妙的删除程序自己的方法
- 我常去的几家GIS论坛
- javascript函数库