<kbd id="5sdj3"></kbd>
<th id="5sdj3"></th>

  • <dd id="5sdj3"><form id="5sdj3"></form></dd>
    <td id="5sdj3"><form id="5sdj3"><big id="5sdj3"></big></form></td><del id="5sdj3"></del>

  • <dd id="5sdj3"></dd>
    <dfn id="5sdj3"></dfn>
  • <th id="5sdj3"></th>
    <tfoot id="5sdj3"><menuitem id="5sdj3"></menuitem></tfoot>

  • <td id="5sdj3"><form id="5sdj3"><menu id="5sdj3"></menu></form></td>
  • <kbd id="5sdj3"><form id="5sdj3"></form></kbd>

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

    共 7432字,需瀏覽 15分鐘

     ·

    2021-03-02 09:36

    今天來給大家講解一下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)行討論!


    往期推薦
    3人2周上線,2人1周上線!開發(fā)周期不能用搬磚模式計(jì)算...
    想接私活時(shí)薪再翻一倍,推薦這幾個(gè)開源的SpringBoot項(xiàng)目
    銀行科技到底怎么樣?我曾經(jīng)的四年告訴你 !
    這代碼怕是隊(duì)友下的毒...
    Java中sin、log、tan等復(fù)雜數(shù)學(xué)運(yùn)算怎么搞?


    如果你喜歡本文,歡迎關(guān)注我,訂閱更多精彩內(nèi)容
    關(guān)注我回復(fù)「加群」,加入Spring技術(shù)交流群

    這些葵花寶典,無須自宮,免費(fèi)領(lǐng)取  ?

    大佬的算法刷題筆記PDF

    超級秘籍籍:設(shè)計(jì)模式PDF學(xué)習(xí)筆記!

    Java基礎(chǔ)核心知識大總結(jié).pdf

    上次誰說要簡歷模板來著?來!


    喜歡的這里報(bào)道

    ↘↘↘

    瀏覽 56
    點(diǎn)贊
    評論
    收藏
    分享

    手機(jī)掃一掃分享

    分享
    舉報(bào)
    評論
    圖片
    表情
    推薦
    點(diǎn)贊
    評論
    收藏
    分享

    手機(jī)掃一掃分享

    分享
    舉報(bào)

    <kbd id="5sdj3"></kbd>
    <th id="5sdj3"></th>

  • <dd id="5sdj3"><form id="5sdj3"></form></dd>
    <td id="5sdj3"><form id="5sdj3"><big id="5sdj3"></big></form></td><del id="5sdj3"></del>

  • <dd id="5sdj3"></dd>
    <dfn id="5sdj3"></dfn>
  • <th id="5sdj3"></th>
    <tfoot id="5sdj3"><menuitem id="5sdj3"></menuitem></tfoot>

  • <td id="5sdj3"><form id="5sdj3"><menu id="5sdj3"></menu></form></td>
  • <kbd id="5sdj3"><form id="5sdj3"></form></kbd>
    日本不卡无码视频 | 日本黄色一级大片免费看 | 欧美三级视频在线播放 | 男人天堂无码视频 | 91久久精品日日躁夜夜躁欧美又粗又大 |