失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Java 使用 zip4j 进行基本的压缩 解压 设置密码操作(version zip4j-2.6.4)

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

时间:2024-06-30 11:57:19

相关推荐

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

先看工具类

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, 9:23:19 PM*/public class ZipUtil {/*** 打包指定的文件* * @param file 待打包的文件* @param zipFilePath 存储压缩包的路径,包含文件名* @throws Exception* * @author by SPACE* @log create on 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 -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 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 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 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 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 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 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 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 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);}/*** 校验参数是否为空* * @param objects 待校验的参数数组* @throws NullPointerException* * @author by SPACE* @log create on 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 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();// 从设置了密码的压缩包内解压指定的文件//unzipTargetPwdFile();System.out.println("OK");} catch (Exception e) {e.printStackTrace();}}/*** 打包单个文件* * @author by SPACE* @throws ZipException* @log create on 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 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 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 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 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 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 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 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 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);}/*** 从设置了密码的压缩包内解压指定的文件* * @throws Exception* * @author by SPACE* @log create on 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)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。