如何用Java创建ZIP文档?
共 7432字,需浏览 15分钟
·
2021-03-02 09:36
今天来给大家讲解一下ZIP压缩文件,以及如何使用API将数据压缩到可共享的加密或不加密ZIP存档中。喜欢的小伙伴记得点赞关注哟~
现在每个人的日常工作中,ZIP文件已经无处不在,可以说是对于处理大量数据、压缩为方便共享格式的最佳方法之一。
但很多人肯定不知道,ZIP最早是在1989年被PKWARE公司开发的,随后很快被其他一些大公司广泛使用,例如微软、苹果等等。从那个时起,ZIP就逐渐成了压缩文件的代名词。
压缩文件有着很多优点,最显著的优点在于可以将存储空间最大化的利用。当你有很多平时不常用的文件时,比较好的一个做法就是将他们全都打在一个ZIP压缩包文件里。
压缩文件也可以方便用户通过邮件传递一些附件,或者拷贝至移动硬盘等介质。
好了,言归正传。下面就给大家介绍以下,如何Java中创建一个新的ZIP存档文件,并且可以使用加密及不加密等不同的选项。
第一步:新增repository
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
然后增加对依赖项的引用
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.90</version>
</dependency>
</dependencies>
这之后,我们在程序顶部将其导入并配置API密钥.(如果想要自己的密钥,可以到https://cloudmersive.com/免费获取一个)
ApiClient defaultClient = Configuration.getDefaultApiClient();
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
做完上述的步骤,我们就可以调用我们的API函数了。下面第一个API函数示例将生成一个简单的、不加密的存档文件,最多呢,允许压缩10个文件。代码如下:
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();
}
第二个示例,在第一个示例的基础上,增加压缩文件的加密保护,代码如下:
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();
}
不过要注意一下,如果想让加密函数顺利运行,需要一些额外的附加参数。
Password ,密码要注意安全性。 encryptionAlgorithm,加密算法,可以是AES-256(推荐)、AES-128和PK Zip(不推荐;传统的弱加密算法)。一般默认值为AES-256。
运行完之后,就能获取你想要的压缩文件了,是不是很简单?
赶紧试一下,有什么疑问或者心得的话,欢迎留言进行讨论!
⚔ 这些葵花宝典,无须自宫,免费领取 ⚔
喜欢的这里报道
↘↘↘