package cn.jxjycn.ckadmin.core.utils;
import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletResponse;
/**
* @program: ckadmin
* @description 压缩文件
* @author: ZhangXu
* @create: 2020-06-30 16:22
**/
public class ZipUtils {
public static void zip(List<String> filePaths,String zipFilePath,Boolean keepDirStructure,HttpServletResponse response) throws IOException {
compress(filePaths,zipFilePath,keepDirStructure);
downLoadZip(zipFilePath,response);
deleteZip(zipFilePath);
}
/**
* @Title: compress
* @Description: TODO
* @param filePaths 需要压缩的文件地址列表(绝对路径或url)
* @param zipFilePath 需要压缩到哪个zip文件(无需创建这样一个zip,只需要指定一个全路径)
* @param keepDirStructure 压缩后目录是否保持原目录结构
* @throws IOException
* @return int 压缩成功的文件个数
*/
public static int compress(List<String> filePaths, String zipFilePath, Boolean keepDirStructure) throws IOException{
byte[] buf = new byte[1024];
//zip文件不存在,则创建文件,用于压缩
// path表示你所创建文件的路径
String path = zipFilePath.substring(0,zipFilePath.lastIndexOf("/"));
// fileName表示你创建的文件名
String fileName = zipFilePath.substring(zipFilePath.lastIndexOf("/")+1) + ".zip";
File f = new File(path);
if (!f.exists()) {
f.mkdirs();
}
File file = new File(f, fileName);
if (!file.exists()){
file.createNewFile();
}
//记录压缩了几个文件?
int fileCount = 0;
try {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
for(int i = 0; i < filePaths.size(); i++){
String relativePath = filePaths.get(i);
InputStream inputStream = null;
if(StringUtils.isEmpty(relativePath)){
continue;
}
//绝对路径找到file
File sourceFile = new File(relativePath);
if(sourceFile == null || !sourceFile.exists()){
inputStream = new URL(relativePath).openStream();
if (inputStream == null){
continue;
}
} else {
inputStream = new FileInputStream(sourceFile);
}
if(keepDirStructure!=null && keepDirStructure){
//保持目录结构
zos.putNextEntry(new ZipEntry(relativePath));
}else{
//直接放到压缩包的根目录
zos.putNextEntry(new ZipEntry(sourceFile.getName()));
}
//System.out.println("压缩当前文件:"+sourceFile.getName());
int len;
while((len = inputStream.read(buf)) > 0){
zos.write(buf, 0, len);
}
zos.closeEntry();
inputStream.close();
fileCount++;
}
zos.close();
//System.out.println("压缩完成");
} catch (Exception e) {
e.printStackTrace();
}
return fileCount;
}
/**
* 下载
*
* @param zipFile 文件名称
* @param response
*
*/
public static void downLoadZip(String zipFile, HttpServletResponse response) throws IOException {
try {
int len = 0;
zipFile = zipFile + ".zip";
// 要下载的文件绝对路径
File file = new File(zipFile);
InputStream ins;
ins = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[ins.available()];
ins.read(buffer);
ins.close();
response.reset(); // 非常重要
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/x-zip-compressed");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFile , "UTF-8"));
OutputStream out = new BufferedOutputStream(response.getOutputStream());
out.write(buffer);
out.flush();
out.close();
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
/**
* 删除单个文件
*
* @param fileName
* 要删除的文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
public static boolean deleteZip(String fileName) {
fileName = fileName + ".zip";
File file = new File(fileName);
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
if (file.exists() && file.isFile()) {
if (file.delete()) {
System.out.println("删除单个文件" + fileName + "成功!");
return true;
} else {
System.out.println("删除单个文件" + fileName + "失败!");
return false;
}
} else {
System.out.println("删除单个文件失败:" + fileName + "不存在!");
return false;
}
}
}
来源:oschina
链接:https://my.oschina.net/u/3358860/blog/4332564