import java.io.*;
import java.net.URLEncoder;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import javax.servlet.http.HttpServletResponse;
public class Zip4jUtils {
/**
* @param zipPath 创建zip文件的 文件路径+zip名称
* @param folderPath 待压缩文件夹或者待压缩文件
*/
public static void zipFile(String zipPath, String folderPath) throws
ZipException {
File file = new File(folderPath);
final ZipFile zipFile = new ZipFile(zipPath); // 創建zip包,指定了zip路徑和zip名稱
final ZipParameters parameters = new ZipParameters(); // 设置zip包的一些参数集合
parameters.setEncryptFiles(false); // 是否设置密码(此处设置为:是)
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 压缩方式(默认值)
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 普通级别(参数很多)
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密级别
if (file.isDirectory()) {
zipFile.createZipFileFromFolder(new File(folderPath), parameters, false, -1L);
} else {
zipFile.createZipFile(file, parameters);
}
}
/**
* @param zipPath 创建zip文件的 文件路径+zip名称
* @param folderPath 待压缩文件夹或者待压缩文件
* @param passWord 密碼
*/
public static void zipFile(String zipPath, String folderPath, String passWord) throws
ZipException {
File file = new File(folderPath);
final ZipFile zipFile = new ZipFile(zipPath); // 創建zip包,指定了zip路徑和zip名稱
final ZipParameters parameters = new ZipParameters(); // 设置zip包的一些参数集合
parameters.setEncryptFiles(true); // 是否设置密码(此处设置为:是)
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 压缩方式(默认值)
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 普通级别(参数很多)
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密级别
parameters.setPassword(passWord); // 压缩包密码为123456
if (file.isDirectory()) {
zipFile.createZipFileFromFolder(new File(folderPath), parameters, false, -1L);
} else {
zipFile.createZipFile(file, parameters);
}
}
/**
* 解压文件
*
* @param zipPath 压缩文件路径
* @param folderPath 解压位置
* @param passWord 密码
*/
public static void unZip(String zipPath, String folderPath, String passWord) throws
ZipException {
final ZipFile zipFile = new ZipFile(zipPath); // 根据路径取得需要解压的Zip文件
if (zipFile.isEncrypted()) { // 判断文件是否加码
zipFile.setPassword(passWord); // 密码为123456
}
zipFile.extractAll(folderPath); // 压缩包文件解压路径
}
public static HttpServletResponse downloadZip(File file, HttpServletResponse response) {
if (file.exists() == false) {
System.out.println("待压缩的文件目录:" + file + "不存在.");
} else {
try {
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
//如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(file.getName().getBytes("GB2312"), "ISO8859-1"));
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
File f = new File(file.getPath());
if(f.delete()){
System.out.println("文件删除成功");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
return response;
}
public static HttpServletResponse downloadZip(File file, HttpServletResponse response, String
userAgent) {
if (userAgent == null) {
downloadZip(file, response);
}
if (file.exists() == false) {
System.out.println("待压缩的文件目录:" + file + "不存在.");
} else {
try {
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
response.setHeader("Content-Disposition", "attachment;filename="
+ URLEncoder.encode(file.getName(), "UTF-8"));
} else {
response.setHeader("Content-Disposition",
"attachment;filename=" + new String(file.getName().getBytes("UTF-8"),
"iso8859-1"));
}
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
File f = new File(file.getPath());
if(f.delete()){
System.out.println("文件删除成功");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
return response;
}
}
zip压缩包工具
最新推荐文章于 2020-12-22 18:13:28 发布