做眾籌網(wǎng)站需要什么條件以服務營銷出名的企業(yè)
? ? ? ?最近項目中有個需求需要對文件夾進行壓縮后傳輸,考慮數(shù)據(jù)泄露安全性問題,需要對壓縮包進行加密,特地查找了下開源壓縮加密類庫,找到了Java語言開發(fā)的zip4j庫,覺得挺好用的,在這分享給大家!
Java項目引入使用步驟如下:
1、引入maven依賴
<dependency>
? ? ? ? ? ? <groupId>net.lingala.zip4j</groupId>
? ? ? ? ? ? <artifactId>zip4j</artifactId>
? ? ? ? ? ? <version>2.11.5</version>
?</dependency>
2、封裝的工具類
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.AesKeyStrength;
import net.lingala.zip4j.model.enums.CompressionLevel;
import net.lingala.zip4j.model.enums.CompressionMethod;
import net.lingala.zip4j.model.enums.EncryptionMethod;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.List;/**
?* @desc zip解壓縮工具
?* @author: wcy
?* @date: 2024/1/12
?* @version: 1.0
?*/
public class ZipUtil {
? ? private static final Logger log = LoggerFactory.getLogger(ZipUtil.class);? ? /**
? ? ?* 壓縮指定路徑的文件
? ? ?* @param srcFilePath 待壓縮文件路徑
? ? ?* @param zipPathFileName zip文件全路徑名
? ? ?* @param password 加密密碼
? ? ?* @return
? ? ?*/
? ? public static boolean zipFile(String srcFilePath, String zipPathFileName, String password){? ? ? ? try {
? ? ? ? ? ? // 生成的壓縮文件
? ? ? ? ? ? ZipFile zipFile = new ZipFile(zipPathFileName);
? ? ? ? ? ? if (StringUtils.isNotEmpty(password)) {
? ? ? ? ? ? ? ? zipFile.setPassword(password.toCharArray());
? ? ? ? ? ? }
? ? ? ? ? ? ZipParameters parameters = new ZipParameters();
? ? ? ? ? ? // 壓縮級別
? ? ? ? ? ? parameters.setCompressionMethod(CompressionMethod.DEFLATE);
? ? ? ? ? ? parameters.setCompressionLevel(CompressionLevel.NORMAL);? ? ? ? ? ? if(StringUtils.isNotEmpty(password)){
? ? ? ? ? ? ? ? parameters.setEncryptFiles(true);
? ? ? ? ? ? ? ? parameters.setEncryptionMethod(EncryptionMethod.AES);
? ? ? ? ? ? ? ? parameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
? ? ? ? ? ? }? ? ? ? ? ? // 要打包的文件夾
? ? ? ? ? ? File file = new File(srcFilePath);
? ? ? ? ? ? if (file.isDirectory()) {
? ? ? ? ? ? ? ? zipFile.addFolder(file, parameters);
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? zipFile.addFile(file, parameters);
? ? ? ? ? ? }
? ? ? ? ? ? return true;
? ? ? ? } catch (ZipException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? log.error("壓縮文件【"+srcFilePath+"】到路徑【"+zipPathFileName+"】失敗:\n"+e.getMessage());
? ? ? ? ? ? return false;
? ? ? ? }
? ? }? ? /**
? ? ?* ?@param zipFileFullName zip文件所在的路徑名
? ? ?* @param filePath 解壓到的路徑
? ? ?* @param password 需要解壓的密碼
? ? ?* @return
? ? ?*/
? ? public static boolean unZipFile(String zipFileFullName, String filePath, String password) {
? ? ? ? try {
? ? ? ? ? ? ZipFile zipFile = new ZipFile(zipFileFullName);
? ? ? ? ? ? // 如果解壓需要密碼
? ? ? ? ? ? if(StringUtils.isNotEmpty(password) && zipFile.isEncrypted()) {
? ? ? ? ? ? ? ? zipFile.setPassword(password.toCharArray());
? ? ? ? ? ? }
? ? ? ? ? ? zipFile.extractAll(filePath);
? ? ? ? ? ? return true;
? ? ? ? } catch (ZipException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? log.error("解壓文件【"+zipFileFullName+"】到路徑【"+filePath+"】失敗:\n"+e.getMessage());
? ? ? ? ? ? return false;
? ? ? ? }
? ? }? ? /**
? ? ?* 添加文件到壓縮文件中
? ? ?* @param zipFullFileName zip文件所在路徑及全名
? ? ?* @param fullFileNameList 待添加的文件全路徑集合
? ? ?* @param rootFolderInZip 在壓縮文件里的文件夾名
? ? ?* @return
? ? ?*/
? ? public static boolean addFilesToZip(String zipFullFileName, List<String> fullFileNameList, String rootFolderInZip) {
? ? ? ? try {
? ? ? ? ? ? ZipFile zipFile = new ZipFile(zipFullFileName);
? ? ? ? ? ? ArrayList<File> addFiles = new ArrayList<>();
? ? ? ? ? ? for (String fileName : fullFileNameList) {
? ? ? ? ? ? ? ? addFiles.add(new File(fileName));
? ? ? ? ? ? }? ? ? ? ? ? ZipParameters parameters = new ZipParameters();
? ? ? ? ? ? parameters.setCompressionMethod(CompressionMethod.DEFLATE);
? ? ? ? ? ? parameters.setCompressionLevel(CompressionLevel.NORMAL);
? ? ? ? ? ? if(StringUtils.isNotEmpty(rootFolderInZip)){
? ? ? ? ? ? ? ? if(!rootFolderInZip.endsWith("/")){
? ? ? ? ? ? ? ? ? ? rootFolderInZip = rootFolderInZip+"/";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? parameters.setRootFolderNameInZip(rootFolderInZip);
? ? ? ? ? ? }
? ? ? ? ? ? zipFile.addFiles(addFiles, parameters);
? ? ? ? ? ? return true;
? ? ? ? } catch (ZipException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? log.error("添加文件失敗:\n"+e.getMessage());
? ? ? ? ? ? return false;
? ? ? ? }
? ? }? ? /**
? ? ?* 從壓縮文件中刪除路徑
? ? ?* @param zipFullFileName
? ? ?* @param fileName
? ? ?* @return
? ? ?*/
? ? public static boolean deleteFileInZip(String zipFullFileName, String fileName) {
? ? ? ? try {
? ? ? ? ? ? ZipFile zipFile = new ZipFile(zipFullFileName);
? ? ? ? ? ? zipFile.removeFile(fileName);
? ? ? ? ? ? return true;
? ? ? ? } catch (ZipException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? log.error("刪除文件失敗:\n"+e.getMessage());
? ? ? ? ? ? return false;
? ? ? ? }
? ? }? ? public static void main(String[] args) throws ZipException {
? ? ? ? String srcFilePath = "C:\\Users\\test\\Desktop\\project\\video";
? ? ? ? String desFilePath = "C:\\Users\\test\\Desktop\\project\\file";
? ? ? ? String zipPathFileName = "C:\\Users\\test\\Desktop\\project\\video.zip";
? ? ? ? String password = "";
? ? ? ? long begin = System.currentTimeMillis();
? ? ? ? boolean zipResult = zipFile(srcFilePath, zipPathFileName, password);
? ? ? ? long end = System.currentTimeMillis();
? ? ? ? System.out.println("壓縮結果:" + zipResult + ",耗時:" + (end - begin) + "ms");? ? ? ? boolean unZipResult = unZipFile(zipPathFileName, desFilePath, password);
? ? ? ? long end2 = System.currentTimeMillis();
? ? ? ? System.out.println("解壓結果:" + unZipResult + ",耗時:" + (end2 - end) + "ms");
? ? ? ? System.out.println(unZipResult);
? ? }
}
3、測試結果
壓縮結果:true,耗時:3281ms
解壓結果:true,耗時:590ms