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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Zip4j 压缩包加密压缩与解压

發布時間:2023/12/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Zip4j 压缩包加密压缩与解压 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

引入依賴:zip4j

<dependency><groupId>net.lingala.zip4j</groupId><artifactId>zip4j</artifactId><version>1.3.2</version> </dependency>

一、壓縮

不多說直接上圖,如下圖為我測試的文件路徑。
桌面上的“測試加密壓縮”文件夾下,一個為文件,一個為文件夾。

public class Test {public static void main(String[] args) throws ZipException {compressedFileWithPassword("C:/Users/12495/Desktop/測試加密壓縮","C:/Users/12495/Desktop/測試加密壓縮","123456");}/*** 壓縮包加密** @param resourcesPath 需要壓縮的源文件路徑* @param targetPath 壓縮后的目標路徑* @param password 壓縮密碼* @throws ZipException*/public static void compressedFileWithPassword(String resourcesPath, String targetPath, String password) throws ZipException {File resourcesFile = new File(resourcesPath);// 沒有目標目錄為父路徑if (StringUtils.isBlank(targetPath)) {targetPath = resourcesFile.getParent();}File targetFile = new File(targetPath);//如果目的路徑不存在,則新建if (!targetFile.exists()) {targetFile.mkdirs();}ZipFile zipFile = new ZipFile(resourcesPath + ".zip");//設置編碼zipFile.setFileNameCharset("GBK");//設置參數ZipParameters parameters = new ZipParameters();//設置壓縮方式.parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);//設置壓縮級別,默認為0(即不壓縮)parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);//設置壓縮密碼(至少3步)//設置加密文件parameters.setEncryptFiles(true);//設置加密方式parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);if (StringUtils.isNotBlank(password)) {parameters.setPassword(password);}File[] fs = resourcesFile.listFiles();for (File f : fs) {if (f.isDirectory()) {//添加文件夾zipFile.addFolder(f, parameters);} else {//添加文件zipFile.addFile(f, parameters);}}} }

壓縮后效果圖:


成功加密壓縮。

二、解壓

解壓就比較簡單了,路徑對就沒問題了

public class Test {public static void main(String[] args) throws Exception { // compressedFileWithPassword("C:/Users/12495/Desktop/測試加密壓縮", // "C:/Users/12495/Desktop/測試加密壓縮","123456");zipUncompressPassword("C:/Users/12495/Desktop/測試加密壓縮.zip", "123456");}/*** 解壓縮* @param inputFile 壓縮包的路徑* @throws Exception*/public static void zipUncompressPassword(String inputFile, String password) throws Exception {File file = new File(inputFile);ZipFile zipFile = new ZipFile(file);//設置文件編碼,根據實際場景zipFile.setFileNameCharset("GBK");if (zipFile.isEncrypted()) {zipFile.setPassword(password.toCharArray());}zipFile.extractAll(inputFile.replace(".zip", ""));} }

以上就是解壓的過程。

總結

以上是生活随笔為你收集整理的Zip4j 压缩包加密压缩与解压的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。