今天来给大家讲解一下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。

运行完之后,就能获取你想要的压缩文件了,是不是很简单?

赶紧试一下,有什么疑问或者心得的话,欢迎留言进行讨论!

往期推荐

3人2周上线,2人1周上线!开发周期不能用搬砖模式计算...

想接私活时薪再翻一倍,推荐这几个开源的SpringBoot项目

银行科技到底怎么样?我曾经的四年告诉你 !

这代码怕是队友下的毒...

Java中sin、log、tan等复杂数学运算怎么搞?

如果你喜欢本文,欢迎关注我,订阅更多精彩内容

关注我回复「加群」,加入Spring技术交流群

⚔ 这些葵花宝典,无须自宫,免费领取  

大佬的算法刷题笔记PDF

超级秘籍籍:设计模式PDF学习笔记!

Java基础核心知识大总结.pdf

上次谁说要简历模板来着?来!

喜欢的这里报道

↘↘↘

如何用Java创建ZIP文档?相关推荐

  1. 如何用java代码生成Word文档和签字以及导出pdf文件

    文章目录 前言 一.任务描述以及前期准备 1.任务描述 2.前期准备 二.编写java代码转成word文档 1.小服务代码 2.工具类代码 3.jsp页面代码 三.word签名和导出pdf 1.签名 ...

  2. Word处理控件Aspose.Words功能演示:用Java从Word文档中提取文本

    Aspose.Words For .NET是一种高级Word文档处理API,用于执行各种文档管理和操作任务.API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsof ...

  3. java流与文件——ZIP文档

    [0]README 0.1) 本文描述转自 core java volume 2, 旨在理解 java流与文件--ZIP文档 的相关知识: 0.2) for source code, please v ...

  4. Java 使用word模板创建word文档报告教程

    上面是java 利用word模板生成的一个word报告文档,利用的是第三方类库Poi-tl 是实现的. poi-tl是一个基于Apache POI的Word模板引擎,也是一个免费开源的Java类库,你 ...

  5. 太实用了!在Java的Word 文档中插入或读取艺术字

    太实用了!在Java的Word 文档中插入或读取艺术字 简直不能太实用! 扫码关注<Java学研大本营>,加入读者群,分享更多精彩 艺术字是一组文本样式,允许您向文本添加设计元素,例如填充 ...

  6. pdfbox创建pdf_PDFBox创建PDF文档

    现在让我们了解如何使用PDFBox库创建PDF文档. 创建一个空的PDF文档 可以通过实例化PDDocument类来创建一个空的PDF文档.使用这个类的Save()方法将文档保存在所需的位置. 以下是 ...

  7. java 界面艺术字,Java 在Word文档中添加艺术字

    与普通文字相比,艺术字更加美观有趣也更具有辨识度,常见于一些设计精美的杂志或宣传海报中.我们在日常工作中编辑Word文档时,也可以通过添加艺术字体来凸显文章的重点,美化页面排版.这篇文章将介绍如何使用 ...

  8. POI API 创建Excel 文档

    原文来自 http://www.blogjava.net/hezhi/articles/POI.html HSSF提供给用户使用的对象在org.apache.poi.hssf.usermodel包中, ...

  9. Java将Word文档转换为PDF的完美工具

    引用至:https://mp.weixin.qq.com/s/JIgo3f98HufGJx23mgtvag Java 将 Word 文档转换为 PDF 的完美工具 在日常工作中,PDF格式良好的视觉阅 ...

最新文章

  1. 基于Java的疫情防控管理系统
  2. 自动生成Mapper文件(基于Mybatis Maven插件)
  3. Dynamics CRM中跨域调用Web API 2
  4. mybatis返回null_面试官:你分析过mybatis工作原理吗?
  5. java学习(16):巩固练习
  6. 留个脚印,证明我来过~!
  7. 关于修改esp8266波特率sscom32串口调试窗口没反应解决方案
  8. 推荐一个js脚本的字体拟合模型
  9. matlab 平滑曲线连接_曲线拟合的一些想法
  10. 初级对于湘源辅助cad制作城市规划图纸的认识
  11. 对研发团队里技术分享的一些思考
  12. java 判断是否为英文字母_java 如何判断是否是26个英文字母
  13. 13.相机和图像——视场(Field of View),视场实战_4
  14. 实用Windows网速监控软件TrafficMonitor
  15. 基于Vue2.0的音乐播放器——歌手模块(拿不到数据)
  16. 使用golang链接达梦数据库
  17. mysql 自动截断_MySql中关于超长自动截断的案例详解
  18. 如何将Asp.net 2.0网站部署到服务器
  19. 阿里大文娱让中国传统文化融入年轻人的生活
  20. 统计学习方法——李航 笔记

热门文章

  1. golang for循环 使用多个变量
  2. 获取ip地理位置的 api接口 简介
  3. 内核程序中进程的pid,handle,eprocess之间相互转换的方法
  4. Linux ps命令详解与示例说明
  5. Android--屏幕方向的改变
  6. Java学习之do---while语句
  7. rpm包备份命令linux,RPM常用命令介绍 - Linux就该这么学的个人空间 - OSCHINA - 中文开源技术交流社区...
  8. python核心数据类型_Python核心数据类型-列表
  9. linux命令vgdisplay提示权限不足,linux常用命令总结
  10. 来宾可以删除服务器文件怎么设置密码,服务器来宾用用户设置密码