工作中遇到很多多格式文件下载压缩及解压处理,现将通用文件下载工具类做一个总结。包含格式(doc/docx、xls/xlsx、lrm/lrmx、txt、zip/rar等)。
一、解压处理
文件解压主要处理rar及zip两种格式压缩文件。其中rar5.0及以上版本需采用命令行方式解压,所以rar格式解压直接使用命令行方式(需注意程序运行环境windows/linux命令行方式不同)。zip格式解压使用jar包。
解压所需jar包:
<!-- 解压zip -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.9.4</version>
</dependency>
<!-- io流 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
通用解压方法:
/**
* 解压zip文件
* @param zipFile
* @param outDir
* @throws IOException
*/
public static void unZip(File zipFile, String outDir) throws IOException {
File outFileDir = new File(outDir);
if (!outFileDir.exists()) {
boolean isMakDir = outFileDir.mkdirs();
if (isMakDir) {
System.out.println("创建压缩目录成功");
}
}
ZipFile zip = new ZipFile(zipFile);
for (Enumeration enumeration = zip.getEntries(); enumeration.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) enumeration.nextElement();
String zipEntryName = entry.getName();
InputStream in = zip.getInputStream(entry);
if (entry.isDirectory()) { //处理压缩文件包含文件夹的情况
File fileDir = new File(outDir + zipEntryName);
fileDir.mkdir();
continue;
}
File file = new File(outDir, zipEntryName);
file.createNewFile();
OutputStream out = new FileOutputStream(file);
byte[] buff = new byte[1024];
int len;
while ((len = in.read(buff)) > 0) {
out.write(buff, 0, len);
}
in.close();
out.close();
}
}
/**
* 采用命令行方式解压文件
* 解决rar5.0及以上版本压缩包解压不了问题
* @param rarFile 压缩文件流
* @param destDir 解压结果路径
* @param cmdPath 命令所在路径
* @return
*/
public static boolean unRar(File rarFile, String destDir, String cmdPath) throws Exception {
boolean bool = false;
if (!rarFile.exists()) {
return false;
}
File destDirPath = new File(destDir);
if (!destDirPath.exists()) {
destDirPath.mkdirs();
}
// 开始调用命令行解压,参数-o+是表示覆盖的意思
// String cmdPath = "C:\\Program Files\\WinRAR\\WinRAR.exe"; // windows中的路径
// String cmdPath = "/usr/local/bin/unrar"; 如果linux做了软连接 不需要这里配置路径
String cmd = cmdPath + " X -o+ " + rarFile + " " + destDir;
Process proc = Runtime.getRuntime().exec(cmd);
if (proc.waitFor() != 0) {
if (proc.exitValue() == 0) {
bool = false;
}
} else {
bool = true;
}
return bool;
}
需注意的是:
方法仅可以解压一层压缩文件,压缩文件套压缩文件格式本方法不适用。多层压缩文件解压思路:解压外层压缩包后依次判断文件类型,如果检测到有压缩包则进行解压,整个过程可迭代进行。(仅提供思路)
二、文件下载
文件下载在日常工作中很常见,常见的下载格式:doc/docx、xls/xlsx、txt、json、zip等。下载方法都大同小异。
下载使用说明:
- Word:使用的是freemarker生成固定模板进行下载导出。
- Excel:poi导出
- 其他格式:使用io流导出
具体怎么使用freemarker生成模板、poi导出文件本篇不做深入讨论(百度一大堆)。
涉及到的jar包(按需添加即可):
<!-- io流 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.6.0</version>
</dependency>
<!-- freemarker -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
通用文件名格式处理工具代码:
public static String checkEncode(HttpServletRequest request, String value) throws UnsupportedEncodingException {
String agent = request.getHeader("USER-AGENT");
if (null != agent){
if (-1 != agent.indexOf("Chrome") || -1 != agent.indexOf("Firefox") || -1 != agent.indexOf("Safari")) {
value = new String(value.getBytes("utf-8"), "ISO8859-1");
} else {//IE7+
value = java.net.URLEncoder.encode(value, "UTF-8");
}
}
return value;
}
使用io流下载文件
使用io流下载文件适用于很多文件格式,需注意的是字符编码集。出现文件下载下来中文乱码的十有八九都是编码集的问题。
- 示例1:zip文件下载
/**
* 根据文件,进行压缩,批量下载
* @