使用apache的commons-compress可以实现文件的7z解压缩功能
java使用7z对文件压缩可以使文件大小被压缩的很小,便于对文件的归档处理,使用apache的commons-compress可以实现文件的7z解压缩功能
1.压缩
/**
* 7z文件压缩
*
* @param inputFile 待压缩文件夹/文件名
* @param outputFile 生成的压缩包名字
*/
public static void compress7z(String inputFile, String outputFile) {
log.info("开始7z压缩本地路径{}的相关日志到{}压缩文件中", inputFile, outputFile);
StopWatch watch = new StopWatch();
watch.start("7z压缩");
SevenZOutputFile outArchive = null;
try {
File input = new File(inputFile);
if (!input.exists()) {
throw new RuntimeException(input.getPath() + "待压缩文件不存在");
}
File output = new File(outputFile);
outArchive = new SevenZOutputFile(output);
// 递归压缩
compress(outArchive, input, null);
} catch (Exception e) {
log.error("7z文件压缩出现异常源路径{},目标路径{}", inputFile, outputFile, e);
throw new RuntimeException("7z文件压缩出现异常");
} finally {
// 关闭
closeSevenZOutputFile(outArchive);
}
watch.stop();
log.info("结束7z压缩本地路径{}的相关日志到{}压缩文件中,耗时{}ms", inputFile, outputFile, watch.getTotalTimeMillis());
}
2.解压
/**
* 7z文件解压
*
* @param inputFile 待解压文件名
* @param destDirPath 解压路径
*/
public static void unCompress7z(String inputFile, String destDirPath) throws Exception {
StopWatch watch = new StopWatch();
watch.start("7z解缩");
// 获取当前压缩文件
File srcFile = new File(inputFile);
// 判断源文件是否存在
if (!srcFile.exists()) {
throw new Exception(srcFile.getPath() + "所指文件不存在");
}
// 开始解压
SevenZFile zIn = new SevenZFile(srcFile);
SevenZArchiveEntry entry = null;
File file = null;
while ((entry = zIn.getNextEntry()) != null) {
if (!entry.isDirectory()) {
file = new File(destDirPath, entry.getName());
if (!file.exists()) {
// 创建此文件的上级目录
new File(file.getParent()).mkdirs();
}
OutputStream out = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(out);
int len = -1;
byte[] buf = new byte[1024];
while ((len = zIn.read(buf)) != -1) {
bos.write(buf, 0, len);
}
// 关流顺序,先打开的后关闭
bos.close();
out.close();
}
}
watch.stop();
log.info("结束7z解压,耗时{}ms", watch.getTotalTimeMillis());
}
3.测试
package com.common.util.sevenz.controller; import com.common.util.sevenz.SevenZipUtil; import org.apache.commons.lang3.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; /** * @author * @Describe 功能描述 * @date */ @RestController public class SevenzTestController { String srcPath = "C:\\Users\\kinson\\Desktop\\7zTest"; String dest7zFilePath = "C:\\Users\\kinson\\Desktop\\7zTest.7z"; String destPath = "C:\\Users\\kinson\\Desktop\\un7z"; @RequestMapping(value = "compress7z") public void compress7z(HttpServletResponse response, String type) throws IOException { if (StringUtils.isEmpty(type)) { // 浏览器响应 // 浏览器响应返回 response.reset(); // 设置response的header,response为HttpServletResponse接收输出流 response.setContentType("application/octet-stream"); String sevenzFileName = "7zFile-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern( "yyyyMMddHHmmss")) + ".7z"; response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(sevenzFileName, "UTF-8")); // 压缩为7z SevenZipUtil.compress7z(srcPath, dest7zFilePath); // 将压缩好的7z响应给浏览器 File file = new File(dest7zFilePath); if (null != file && file.isFile()) { ServletOutputStream os = response.getOutputStream(); FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); int len = -1; // 将源文件写入到输出流 byte[] buf = new byte[1024]; while ((len = bis.read(buf)) != -1) { os.write(buf, 0, len); } bis.close(); fis.close(); os.close(); } } else { // 写到目标文件 SevenZipUtil.compress7z(srcPath, dest7zFilePath); } } @RequestMapping(value = "unCompress7z") public void unCompress7z() throws Exception { // 解压 SevenZipUtil.unCompress7z(dest7zFilePath, destPath); } }