如何用Java創(chuàng)建ZIP文檔?

今天來給大家講解一下ZIP壓縮文件,以及如何使用API將數(shù)據(jù)壓縮到可共享的加密或不加密ZIP存檔中。喜歡的小伙伴記得點(diǎn)贊關(guān)注喲~
現(xiàn)在每個(gè)人的日常工作中,ZIP文件已經(jīng)無處不在,可以說是對于處理大量數(shù)據(jù)、壓縮為方便共享格式的最佳方法之一。
但很多人肯定不知道,ZIP最早是在1989年被PKWARE公司開發(fā)的,隨后很快被其他一些大公司廣泛使用,例如微軟、蘋果等等。從那個(gè)時(shí)起,ZIP就逐漸成了壓縮文件的代名詞。
壓縮文件有著很多優(yōu)點(diǎn),最顯著的優(yōu)點(diǎn)在于可以將存儲空間最大化的利用。當(dāng)你有很多平時(shí)不常用的文件時(shí),比較好的一個(gè)做法就是將他們?nèi)即蛟谝粋€(gè)ZIP壓縮包文件里。
壓縮文件也可以方便用戶通過郵件傳遞一些附件,或者拷貝至移動硬盤等介質(zhì)。
好了,言歸正傳。下面就給大家介紹以下,如何Java中創(chuàng)建一個(gè)新的ZIP存檔文件,并且可以使用加密及不加密等不同的選項(xiàng)。
第一步:新增repository
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
然后增加對依賴項(xiàng)的引用
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.90</version>
</dependency>
</dependencies>
這之后,我們在程序頂部將其導(dǎo)入并配置API密鑰.(如果想要自己的密鑰,可以到https://cloudmersive.com/免費(fèi)獲取一個(gè))
ApiClient defaultClient = Configuration.getDefaultApiClient();
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
做完上述的步驟,我們就可以調(diào)用我們的API函數(shù)了。下面第一個(gè)API函數(shù)示例將生成一個(gè)簡單的、不加密的存檔文件,最多呢,允許壓縮10個(gè)文件。代碼如下:
ZipArchiveApi apiInstance = new ZipArchiveApi();
File inputFile1 = new File("/path/to/inputfile"); // File | First input file to perform the operation on.
File inputFile2 = new File("/path/to/inputfile"); // File | Second input file to perform the operation on.
File inputFile3 = new File("/path/to/inputfile"); // File | Third input file to perform the operation on.
File inputFile4 = new File("/path/to/inputfile"); // File | Fourth input file to perform the operation on.
File inputFile5 = new File("/path/to/inputfile"); // File | Fifth input file to perform the operation on.
File inputFile6 = new File("/path/to/inputfile"); // File | Sixth input file to perform the operation on.
File inputFile7 = new File("/path/to/inputfile"); // File | Seventh input file to perform the operation on.
File inputFile8 = new File("/path/to/inputfile"); // File | Eighth input file to perform the operation on.
File inputFile9 = new File("/path/to/inputfile"); // File | Ninth input file to perform the operation on.
File inputFile10 = new File("/path/to/inputfile"); // File | Tenth input file to perform the operation on.
try {
byte[] result = apiInstance.zipArchiveZipCreate(inputFile1, inputFile2, inputFile3, inputFile4, inputFile5, inputFile6, inputFile7, inputFile8, inputFile9, inputFile10);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ZipArchiveApi#zipArchiveZipCreate");
e.printStackTrace();
}
第二個(gè)示例,在第一個(gè)示例的基礎(chǔ)上,增加壓縮文件的加密保護(hù),代碼如下:
ZipArchiveApi apiInstance = new ZipArchiveApi();
String password = "password_example"; // String | Password to place on the Zip file; the longer the password, the more secure
File inputFile1 = new File("/path/to/inputfile"); // File | First input file to perform the operation on.
String encryptionAlgorithm = "encryptionAlgorithm_example"; // String | Encryption algorithm to use; possible values are AES-256 (recommended), AES-128, and PK-Zip (not recommended; legacy, weak encryption algorithm). Default is AES-256.
File inputFile2 = new File("/path/to/inputfile"); // File | Second input file to perform the operation on.
File inputFile3 = new File("/path/to/inputfile"); // File | Third input file to perform the operation on.
File inputFile4 = new File("/path/to/inputfile"); // File | Fourth input file to perform the operation on.
File inputFile5 = new File("/path/to/inputfile"); // File | Fifth input file to perform the operation on.
File inputFile6 = new File("/path/to/inputfile"); // File | Sixth input file to perform the operation on.
File inputFile7 = new File("/path/to/inputfile"); // File | Seventh input file to perform the operation on.
File inputFile8 = new File("/path/to/inputfile"); // File | Eighth input file to perform the operation on.
File inputFile9 = new File("/path/to/inputfile"); // File | Ninth input file to perform the operation on.
File inputFile10 = new File("/path/to/inputfile"); // File | Tenth input file to perform the operation on.
try {
byte[] result = apiInstance.zipArchiveZipCreateEncrypted(password, inputFile1, encryptionAlgorithm, inputFile2, inputFile3, inputFile4, inputFile5, inputFile6, inputFile7, inputFile8, inputFile9, inputFile10);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ZipArchiveApi#zipArchiveZipCreateEncrypted");
e.printStackTrace();
}
不過要注意一下,如果想讓加密函數(shù)順利運(yùn)行,需要一些額外的附加參數(shù)。
Password ,密碼要注意安全性。 encryptionAlgorithm,加密算法,可以是AES-256(推薦)、AES-128和PK Zip(不推薦;傳統(tǒng)的弱加密算法)。一般默認(rèn)值為AES-256。
運(yùn)行完之后,就能獲取你想要的壓縮文件了,是不是很簡單?
趕緊試一下,有什么疑問或者心得的話,歡迎留言進(jìn)行討論!
? 這些葵花寶典,無須自宮,免費(fèi)領(lǐng)取 ?
超級秘籍籍:設(shè)計(jì)模式PDF學(xué)習(xí)筆記!
喜歡的這里報(bào)道
↘↘↘
