怎么在java中压缩文件并下载
作者:暮色微凉
时间:2023-04-24
来源:互联网
浏览:0
1、ZIP文件格式[localfileheader+filedata+datadescriptor]{1,n}+centraldirectory+endofcentraldirectoryrecord即[文件头+文件数据+数据描述符]{此处可重复n次}+核心目录+目录结束标识当压缩包中有多个文件时,就会有多个[文件头+文件数据+数据描述符]2、压缩和下载步骤(1)创建压缩包前准备//定义压缩包存在服务器的路径Stringpath=request.getSession().getServletContext
1、ZIP文件格式
[local file header + file data + data descriptor]{1,n} + central directory + end of central directory record
即
[文件头+文件数据+数据描述符]{此处可重复n次}+核心目录+目录结束标识
当压缩包中有多个文件时,就会有多个[文件头+文件数据+数据描述符]2、压缩和下载步骤
(1)创建压缩包前准备
//定义压缩包存在服务器的路径 String path = request.getSession().getServletContext().getRealPath("/WEB-INF/fileTemp"); //创建路径 File FilePath = new File(path + "/file"); if (!FilePath.exists()) { FilePath.mkdir(); } String path = FilePath.getPath() + "/"; //定义导出压缩包的名称 String title ="问价压缩包"; //压缩包格式 String fileNamezip = title + ".zip"; String zipPath = path + fileNamezip; //创建一个ZIP输出流并实例化缓冲区域 ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipPath))); //设置编码格式(解决linux出现乱码) out.setEncoding("gbk"); //定义字节数组 byte data[] = new byte[2048]; //获取文件记录(获取文件记录代码省略) List FileList =。。。; if (!FileList.isEmpty()) { ExportUtil util = new ExportUtil(title,title, request, response, FilePath.getPath()); }
(2)删除压缩包之前的数据,创建压缩包
util.startZip(FilePath.getPath());
(3)循环将需要压缩的文件放到压缩包中
for (int i = 0; i < FileList.size(); i++) {
fileVo fileVo=FileList.get(i);
export(fileVo,request,response,title,FilePath.getPath(),fileName);
}
------
public void export(fileVo fileVo, HttpServletRequest request,
HttpServletResponse response, String title,String path, String fileName) {
FileOutputStream fileOutputStream = null;
try {
File dirFile = null;
int i = fileVo.getName().lastIndexOf(".");
if(i!=-1){//存在文件类型
fileName1 = fileName1 + "." + (fileVo.getName()).substring(i+1);
}
boolean bFile = false;
String mkdirName = path + File.separatorChar + title;
dirFile = new File(mkdirName);
if(!dirFile.exists()) {
dirFile.getParentFile().mkdirs();
}
if (dirFile.isDirectory()) {
path = mkdirName + File.separatorChar + fileName1;
} else {
bFile = dirFile.mkdirs();
}
if (bFile) {
path = mkdirName + File.separatorChar + fileName1;
}
fileOutputStream = new FileOutputStream(path.replace("*", ""));
String fileName = URLEncoder.encode(fileName1, "UTF-8");
if (fileName.length() > 110) {
fileName = new String(fileName1.getBytes("gb2312"), "ISO8859-1");
}
response.setHeader("Connection", "close");
response.setHeader("Content-Type", "application/octet-stream");
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=\""
+ Utf8Util.toUtf8String(fileName) + "\"");
//读取文件流输出到到另一个位置
fileVo.getFileIo(fileOutputStream);
fileOutputStream.close();
} catch (Exception e) {
logger.error("异常:原因如下"+e.getMessage(), e);
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
logger.error("异常:原因如下"+e1.getMessage(), e1);
}
}
}
------(4)压缩完成,关闭输出流。
util.entdZip(FilePath.getPath());
作者最新文章
白描 PDF
2026-09-16 17:44
密码键盘
2026-09-16 17:43
3dmax快捷键失效了怎么办
2026-09-16 13:53
Xiaomi 18 Fold首销数据解读:较上代大折叠增长310%的原因与配置分析
2026-09-08 16:55
PDF文件太大怎么压缩?在线减小体积的操作步骤
2026-09-03 11:12
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















