|
@@ -8,7 +8,9 @@ import javax.servlet.http.HttpServletResponse;
|
|
import java.io.*;
|
|
import java.io.*;
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
+import java.util.List;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipOutputStream;
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
@@ -21,7 +23,7 @@ public class FileZipUtil {
|
|
* @param fileName 下载时的文件名称
|
|
* @param fileName 下载时的文件名称
|
|
* @param postfix 下载时的文件后缀 .zip/.rar
|
|
* @param postfix 下载时的文件后缀 .zip/.rar
|
|
*/
|
|
*/
|
|
- public static void exportZip(HttpServletResponse response, String sourceFilePath, String fileName, String postfix) {
|
|
|
|
|
|
+ public static void exportZip(HttpServletResponse response, String sourceFilePath, List<String> fileNames, String fileName, String postfix) {
|
|
// 默认文件名以时间戳作为前缀
|
|
// 默认文件名以时间戳作为前缀
|
|
if(StringUtils.isBlank(fileName)){
|
|
if(StringUtils.isBlank(fileName)){
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
@@ -32,7 +34,7 @@ public class FileZipUtil {
|
|
try {
|
|
try {
|
|
OutputStream os = response.getOutputStream();
|
|
OutputStream os = response.getOutputStream();
|
|
// 接收压缩包字节
|
|
// 接收压缩包字节
|
|
- byte[] data = createZip(sourceFilePath);
|
|
|
|
|
|
+ byte[] data = createZip(sourceFilePath,fileNames);
|
|
response.reset();
|
|
response.reset();
|
|
response.setCharacterEncoding("UTF-8");
|
|
response.setCharacterEncoding("UTF-8");
|
|
response.addHeader("Access-Control-Allow-Origin", "*");
|
|
response.addHeader("Access-Control-Allow-Origin", "*");
|
|
@@ -56,12 +58,12 @@ public class FileZipUtil {
|
|
* @return byte[]
|
|
* @return byte[]
|
|
* @throws Exception
|
|
* @throws Exception
|
|
*/
|
|
*/
|
|
- private static byte[] createZip(String sourceFilePath) throws Exception{
|
|
|
|
|
|
+ private static byte[] createZip(String sourceFilePath,List<String> fileNames) throws Exception{
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
ZipOutputStream zip = new ZipOutputStream(outputStream);
|
|
ZipOutputStream zip = new ZipOutputStream(outputStream);
|
|
// 将目标文件打包成zip导出
|
|
// 将目标文件打包成zip导出
|
|
File file = new File(sourceFilePath);
|
|
File file = new File(sourceFilePath);
|
|
- handlerFile(zip, file,"");
|
|
|
|
|
|
+ handlerFile(zip, file,"",fileNames);
|
|
// 无异常关闭流,它将无条件的关闭一个可被关闭的对象而不抛出任何异常。
|
|
// 无异常关闭流,它将无条件的关闭一个可被关闭的对象而不抛出任何异常。
|
|
IOUtils.closeQuietly(zip);
|
|
IOUtils.closeQuietly(zip);
|
|
return outputStream.toByteArray();
|
|
return outputStream.toByteArray();
|
|
@@ -74,32 +76,41 @@ public class FileZipUtil {
|
|
* @param dir
|
|
* @param dir
|
|
* @throws Exception
|
|
* @throws Exception
|
|
*/
|
|
*/
|
|
- private static void handlerFile(ZipOutputStream zip, File file, String dir) throws Exception {
|
|
|
|
- // 如果当前的是文件夹,则循环里面的内容继续处理
|
|
|
|
- if (file.isDirectory()) {
|
|
|
|
- //得到文件列表信息
|
|
|
|
- File[] fileArray = file.listFiles();
|
|
|
|
- if (fileArray == null) {
|
|
|
|
- return;
|
|
|
|
|
|
+ private static void handlerFile(ZipOutputStream zip, File file, String dir,List<String> fileNames) throws Exception {
|
|
|
|
+ // 得到文件列表信息
|
|
|
|
+ File[] fileArray = file.listFiles();
|
|
|
|
+
|
|
|
|
+ if (fileArray == null) {
|
|
|
|
+ extracted(zip, file, dir);
|
|
|
|
+ } else {
|
|
|
|
+ // 过滤出与 fileNames 匹配的文件
|
|
|
|
+ for (File f : fileArray) {
|
|
|
|
+ if (fileNames.contains(f.getName())) { // 使用 contains 方法检查
|
|
|
|
+ extracted(zip, f, dir + f.getName()); // 打包处理匹配的文件
|
|
|
|
+ } else if (f.isDirectory()) {
|
|
|
|
+ // 递归处理子目录
|
|
|
|
+ handlerFile(zip, f, dir + f.getName() + "/", fileNames);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- //将文件夹添加到下一级打包目录
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*//将文件夹添加到下一级打包目录
|
|
zip.putNextEntry(new ZipEntry(dir + "/"));
|
|
zip.putNextEntry(new ZipEntry(dir + "/"));
|
|
dir = dir.length() == 0 ? "" : dir + "/";
|
|
dir = dir.length() == 0 ? "" : dir + "/";
|
|
// 递归将文件夹中的文件打包
|
|
// 递归将文件夹中的文件打包
|
|
for (File f : fileArray) {
|
|
for (File f : fileArray) {
|
|
- handlerFile(zip, f, dir + f.getName());
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- // 如果当前的是文件,打包处理
|
|
|
|
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
|
|
|
|
- ZipEntry entry = new ZipEntry(dir);
|
|
|
|
- zip.putNextEntry(entry);
|
|
|
|
- zip.write(FileUtils.readFileToByteArray(file));
|
|
|
|
- IOUtils.closeQuietly(bis);
|
|
|
|
- zip.flush();
|
|
|
|
- zip.closeEntry();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ handlerFile(zip, f, dir + f.getName(),fileNames);
|
|
|
|
+ }*/
|
|
|
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ private static void extracted(ZipOutputStream zip, File file, String dir) throws IOException {
|
|
|
|
+ // 如果当前的是文件,打包处理
|
|
|
|
+ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
|
|
|
|
+ ZipEntry entry = new ZipEntry(dir); // 使用完整路径(包含文件名)
|
|
|
|
+ zip.putNextEntry(entry);
|
|
|
|
+ zip.write(FileUtils.readFileToByteArray(file));
|
|
|
|
+ IOUtils.closeQuietly(bis);
|
|
|
|
+ zip.closeEntry(); // 关闭当前条目
|
|
|
|
+ }
|
|
}
|
|
}
|