日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java 使用 zip4j 进行基本的压缩、解压、设置密码操作(version zip4j-2.6.4)

發(fā)布時間:2024/9/27 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java 使用 zip4j 进行基本的压缩、解压、设置密码操作(version zip4j-2.6.4) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

先看工具類

package space.util;import java.io.File; import java.util.List;import net.lingala.zip4j.ZipFile; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.model.enums.AesKeyStrength; import net.lingala.zip4j.model.enums.EncryptionMethod;/*** 文件打包工具類* * @author SPACE* @log Jun 14, 2020 9:23:19 PM*/ public class ZipUtil {/*** 打包指定的文件* * @param file 待打包的文件* @param zipFilePath 存儲壓縮包的路徑,包含文件名* @throws Exception* * @author by SPACE* @log create on 2020年6月15日下午2:46:49*/public static void packageZip(File file, String zipFilePath) throws Exception {validationIsNull(file, zipFilePath);new ZipFile(zipFilePath).addFile(file);}/*** 打包指定的文件* * @param file 待打包的文件* @param zipFilePath 存儲壓縮包的路徑,包含文件名* @param password 壓縮包密碼* @throws Exception* * @author by SPACE* @log create on 2020-6-15 15:09:27*/public static void packageZip(File file, String zipFilePath, String password) throws Exception {validationIsNull(file, zipFilePath, password);ZipFile zipFile = new ZipFile(zipFilePath, password.toCharArray());zipFile.addFile(file, getZipParameters());}/*** 對指定的一些文件進行打包* * @param fileList 待打包的文件 list(不接收目錄)* @param zipFilePath 存儲壓縮包的路徑,包含文件名* @throws Exception* * @author by SPACE* @log create on 2020年6月15日15:09:37*/public static void packageZip(List<File> fileList, String zipFilePath) throws Exception {validationIsNull(fileList, zipFilePath);new ZipFile(zipFilePath).addFiles(fileList);}/*** 對指定的一些文件進行打包* * @param fileList 待打包的文件 list* @param zipFilePath 存儲壓縮包的路徑,包含文件名* @param password 壓縮包密碼* * @author by SPACE* @throws Exception* @log create on 2020年6月15日下午2:41:23*/public static void packageZip(List<File> fileList, String zipFilePath, String password) throws Exception {validationIsNull(fileList, zipFilePath, password);ZipFile zipFile = new ZipFile(zipFilePath, password.toCharArray());zipFile.addFiles(fileList, getZipParameters());}/*** 打包指定的目錄* * @param catalogPath 待打包的目錄* @param zipFilePath 存儲壓縮包的路徑,包含文件名* @throws Exception 如 catalogPath 非目錄,則拋出此異常* * @author by SPACE* @log create on 2020年6月15日下午2:30:10*/public static void packageZip(String catalogPath, String zipFilePath) throws Exception {validationIsNull(catalogPath, zipFilePath);new ZipFile(zipFilePath).addFolder(new File(catalogPath));}/*** 打包指定的目錄* * @param catalogPath 待打包的目錄* @param zipFilePath 存儲壓縮包的路徑,包含文件名* @param password 壓縮包密碼* @throws Exception 如 catalogPath 非目錄,則拋出此異常* * @author by SPACE* @log create on 2020年6月15日下午2:33:33*/public static void packageZip(String catalogPath, String zipFilePath, String password) throws Exception {validationIsNull(catalogPath, zipFilePath, password);ZipFile zipFile = new ZipFile(zipFilePath, password.toCharArray());zipFile.addFolder(new File(catalogPath), getZipParameters());}/*** 解壓壓縮包* * @param zipFilePath 待解壓的壓縮包絕對路徑* @param unzipCatalog 解壓后的目錄* @throws Exception* * @author by SPACE* @log create on 2020年6月15日下午3:51:07*/public static void unzipAll(String zipFilePath, String unzipCatalog) throws Exception {validationIsNull(zipFilePath, unzipCatalog);new ZipFile(zipFilePath).extractAll(unzipCatalog);}/*** 解壓帶密碼的壓縮包* * @param zipFilePath 待解壓的壓縮包絕對路徑* @param unzipCatalog 解壓后的目錄* @param password 壓縮包密碼* @throws Exception* * @author by SPACE* @log create on 2020年6月15日下午3:51:45*/public static void unzipAll(String zipFilePath, String unzipCatalog, String password) throws Exception {validationIsNull(zipFilePath, unzipCatalog);new ZipFile(zipFilePath, password.toCharArray()).extractAll(unzipCatalog);}/*** 解壓指定的文件* * @param zipFilePath 待解壓的壓縮包絕對路徑* @param targetFilePath 目標文件相對目錄,基于壓縮包根目錄* @param unzipCatalog 解壓后的目錄* @throws Exception* * @author by SPACE* @log create on 2020年6月15日下午3:56:15*/public static void unzipTargetFile(String zipFilePath, String targetFilePath, String unzipCatalog)throws Exception {new ZipFile(zipFilePath).extractFile(targetFilePath, unzipCatalog);}/*** 從設置了密碼的壓縮包中解壓指定的文件* * @param zipFilePath 待解壓的壓縮包絕對路徑* @param targetFilePath 目標文件相對目錄,基于壓縮包根目錄,* <span style="color:red">例如 msg/success/msg.txt</span>* @param unzipCatalog 解壓后的目錄* @param password 壓縮包密碼* @throws Exception* * @author by SPACE* @log create on 2020年6月15日下午3:54:36*/public static void unzipTargetFile(String zipFilePath, String targetFilePath, String unzipCatalog, String password)throws Exception {new ZipFile(zipFilePath, password.toCharArray()).extractFile(targetFilePath, unzipCatalog);}/*** 校驗參數(shù)是否為空* * @param objects 待校驗的參數(shù)數(shù)組* @throws NullPointerException* * @author by SPACE* @log create on 2020年6月15日下午3:06:20*/static void validationIsNull(Object... objects) throws NullPointerException {for (int i = 0; i < objects.length; i++) {if (StringUtil.isNull(objects[i])) {throw new NullPointerException("param is null");}}}/*** get ZipParameters* * @return ZipParameters* * @author by SPACE* @log create on 2020年6月15日下午3:05:24*/static ZipParameters getZipParameters() {ZipParameters zipParameters = new ZipParameters();zipParameters.setEncryptFiles(true);zipParameters.setEncryptionMethod(EncryptionMethod.AES);zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);return zipParameters;}}

再看測試類

package space.main;import java.io.File; import java.util.ArrayList; import java.util.List;import net.lingala.zip4j.exception.ZipException; import space.util.ZipUtil;public class ZipTest {/*** 壓縮包密碼*/static final String ZIP_PASSWORD = "TAehvImD9zRZnaikoibnup7x6G4kt3fQ";public static void main(String[] args) {try {// 打包單個文件// packageFileZip();// 打包單個文件,并設置密碼 // packageFilePwdZip();// 打包多個文件 // packageFilesZip();// 打包多個文件,設置密碼 // packageFilesPwdZip();// 打包指定的目錄 // packageFileCatalogZip();// 打包指定的目錄,設置密碼 // packageFileCatalogPwdZip();// 解壓壓縮包 // unzipAll();// 解壓設置了密碼的壓縮包 // unzipPwdAll();// 解壓指定的文件 // unzipTargetFile();// 從設置了密碼的壓縮包內(nèi)解壓指定的文件 // unzipTargetPwdFile();System.out.println("OK");} catch (Exception e) {e.printStackTrace();}}/*** 打包單個文件* * @author by SPACE* @throws ZipException* @log create on 2020年6月15日下午2:50:38*/public static void packageFileZip() throws Exception {String filePath = "C:\\Users\\SPACE\\Downloads\\135700\\files\\detailedList.txt";String zipFilePath = "C:\\Users\\SPACE\\Downloads\\packageZipFile.zip";ZipUtil.packageZip(new File(filePath), zipFilePath);}/*** 打包單個文件,帶密碼* * @author by SPACE* @throws ZipException* @log create on 2020年6月15日下午2:50:38*/public static void packageFilePwdZip() throws Exception {String filePath = "C:\\Users\\SPACE\\Downloads\\135700\\files\\detailedList.txt";String zipFilePath = "C:\\Users\\SPACE\\Downloads\\packageZipFile.zip";ZipUtil.packageZip(new File(filePath), zipFilePath, ZIP_PASSWORD);}/*** 打包多個文件* * @throws Exception* * @author by SPACE* @log create on 2020年6月15日下午3:17:34*/public static void packageFilesZip() throws Exception {List<File> fileList = new ArrayList<File>();fileList.add(new File("C:\\Users\\SPACE\\Downloads\\135700\\files\\detailedList.txt"));fileList.add(new File("C:\\Users\\SPACE\\Downloads\\135700\\files"));fileList.add(new File("C:\\Users\\SPACE\\Downloads\\135700\\files\\subassembly\\10021\\1000310021_run.jsp"));String zipFilePath = "C:\\Users\\SPACE\\Downloads\\packageZipFile.zip";ZipUtil.packageZip(fileList, zipFilePath);}/*** 打包多個文件,設置密碼* * @throws Exception* * @author by SPACE* @log create on 2020年6月15日下午3:20:04*/public static void packageFilesPwdZip() throws Exception {List<File> fileList = new ArrayList<File>();fileList.add(new File("C:\\Users\\SPACE\\Downloads\\135700\\files\\detailedList.txt"));fileList.add(new File("C:\\Users\\SPACE\\Downloads\\135700\\files"));fileList.add(new File("C:\\Users\\SPACE\\Downloads\\135700\\files\\subassembly\\10021\\1000310021_run.jsp"));String zipFilePath = "C:\\Users\\SPACE\\Downloads\\packageZipFile.zip";ZipUtil.packageZip(fileList, zipFilePath, ZIP_PASSWORD);}/*** 打包指定的目錄* * @throws Exception* * @author by SPACE* @log create on 2020年6月15日下午3:22:25*/public static void packageFileCatalogZip() throws Exception {String catalogPath = "C:\\Users\\SPACE\\Downloads\\135700";String zipFilePath = "C:\\Users\\SPACE\\Downloads\\packageZipFile.zip";ZipUtil.packageZip(catalogPath, zipFilePath);}/*** 打包指定的目錄,設置密碼* * @throws Exception* * @author by SPACE* @log create on 2020年6月15日下午3:26:34*/public static void packageFileCatalogPwdZip() throws Exception {String catalogPath = "C:\\Users\\SPACE\\Downloads\\135700";String zipFilePath = "C:\\Users\\SPACE\\Downloads\\packageZipFile.zip";ZipUtil.packageZip(catalogPath, zipFilePath, ZIP_PASSWORD);}/*** 解壓* * @throws Exception* * @author by SPACE* @log create on 2020年6月15日下午4:00:10*/public static void unzipAll() throws Exception {String zipFilePath = "C:\\Users\\SPACE\\Downloads\\packageZipFile.zip";String unzipCatalog = "C:\\Users\\SPACE\\Downloads\\";ZipUtil.unzipAll(zipFilePath, unzipCatalog);}/*** 解壓設置了密碼的壓縮包* * @throws Exception* * @author by SPACE* @log create on 2020年6月15日下午4:04:57*/public static void unzipPwdAll() throws Exception {String zipFilePath = "C:\\Users\\SPACE\\Downloads\\packageZipFile.zip";String unzipCatalog = "C:\\Users\\SPACE\\Downloads\\";ZipUtil.unzipAll(zipFilePath, unzipCatalog, ZIP_PASSWORD);}/*** 解壓指定的文件* * @throws Exception* * @author by SPACE* @log create on 2020年6月15日下午4:07:48*/public static void unzipTargetFile() throws Exception {String zipFilePath = "C:\\Users\\SPACE\\Downloads\\packageZipFile.zip";String unzipCatalog = "C:\\Users\\SPACE\\Downloads\\";String targetFilePath = "135700\\files\\detailedList.txt";ZipUtil.unzipTargetFile(zipFilePath, targetFilePath, unzipCatalog);}/*** 從設置了密碼的壓縮包內(nèi)解壓指定的文件* * @throws Exception* * @author by SPACE* @log create on 2020年6月15日下午4:08:19*/public static void unzipTargetPwdFile() throws Exception {String zipFilePath = "C:\\Users\\SPACE\\Downloads\\packageZipFile.zip";String unzipCatalog = "C:\\Users\\SPACE\\Downloads\\";String targetFilePath = "135700\\files\\detailedList.txt";ZipUtil.unzipTargetFile(zipFilePath, targetFilePath, unzipCatalog, ZIP_PASSWORD);}}

總結

以上是生活随笔為你收集整理的Java 使用 zip4j 进行基本的压缩、解压、设置密码操作(version zip4j-2.6.4)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。