温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在java中压缩文件

发布时间:2021-04-21 16:32:32 来源:亿速云 阅读:121 作者:Leah 栏目:编程语言

怎么在java中压缩文件?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

Java是什么

Java是一门面向对象编程语言,可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序。

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());

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI