parent
fa3522b71d
commit
37d3197e60
Binary file not shown.
@ -0,0 +1,24 @@
|
||||
package com.platform.controller;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import com.base.BaseController;
|
||||
import com.platform.utils.ZipCompressUtils;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/filePackage")
|
||||
public class FilePackageController extends BaseController {
|
||||
|
||||
@RequestMapping("download")
|
||||
public void downloadFile(HttpServletRequest request,
|
||||
HttpServletResponse response) {
|
||||
ZipCompressUtils.zip(new File("D:\\20160906-汇总-V2.4"), "D:\\文件包.zip");
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package com.platform.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.apache.tools.zip.ZipEntry;
|
||||
import org.apache.tools.zip.ZipOutputStream;
|
||||
|
||||
public class ZipCompressUtils {
|
||||
private static Logger logger = Logger.getLogger(ZipCompressUtils.class);
|
||||
|
||||
public static void zip(File inputFile, String zipFileName) {
|
||||
try {
|
||||
// 解决中文文件夹名乱码的问题
|
||||
FileOutputStream fos = new FileOutputStream(new String(
|
||||
zipFileName.getBytes("UTF-8")));
|
||||
ZipOutputStream zos = new ZipOutputStream(fos); // 将文件输出流与zip输出流接起来
|
||||
logger.info("压缩-->开始");
|
||||
zip(zos, inputFile, "");
|
||||
logger.info("压缩结束");
|
||||
zos.close();
|
||||
} catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
}
|
||||
}
|
||||
|
||||
public static void zip(ZipOutputStream zos, File file, String base) {
|
||||
try {
|
||||
// 如果句柄是文件夹
|
||||
if (file.isDirectory()) {
|
||||
File[] files = file.listFiles(); // 获取文件夹下的子文件或子目录
|
||||
zos.putNextEntry(new ZipEntry(base + "/"));
|
||||
logger.info("目录名:" + file.getName() + "|加入ZIP条目:" + base + "/");
|
||||
base = (base.length() == 0 ? "" : base + "/");
|
||||
// 遍历目录下的文件
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
// 递归进入本方法
|
||||
zip(zos, files[i], base + files[i].getName());
|
||||
}
|
||||
} else { // 如果句柄是文件
|
||||
if (base == "")
|
||||
base = file.getName();
|
||||
zos.putNextEntry(new ZipEntry(base)); // 填入文件句柄
|
||||
logger.info("文件名:" + file.getName() + "|加入ZIP条目:" + base);
|
||||
// 开始压缩
|
||||
// 从文件入流读,写入ZIP 出流
|
||||
writeFile(zos, file);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
}
|
||||
}
|
||||
|
||||
public static void writeFile(ZipOutputStream zos, File file)
|
||||
throws IOException {
|
||||
logger.info("开始压缩" + file.getName());
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
int len;
|
||||
while ((len = fis.read()) != -1)
|
||||
zos.write(len);
|
||||
logger.info("压缩结束");
|
||||
fis.close();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue