之前找了很多demo代码在压缩中遇到各种问题,中文乱码、文本内容丢失等。

以下教程是本人亲自测试过,通过java.util.zip包实现java代码的文件zip压缩和解压:

1、压缩:

1)压缩一个文件
public static void main(String[] args) throws IOException {String sourceFile = "test1.txt";FileOutputStream fos = new FileOutputStream("compressed.zip");ZipOutputStream zipOut = new ZipOutputStream(fos);File fileToZip = new File(sourceFile);FileInputStream fis = new FileInputStream(fileToZip);ZipEntry zipEntry = new ZipEntry(fileToZip.getName());zipOut.putNextEntry(zipEntry);byte[] bytes = new byte[1024];int length;while((length = fis.read(bytes)) >= 0) {zipOut.write(bytes, 0, length);}zipOut.close();fis.close();fos.close();}
2)压缩多个文件
public static void main(String[] args) throws IOException {List<String> srcFiles = Arrays.asList("test1.txt", "test2.txt");FileOutputStream fos = new FileOutputStream("multiCompressed.zip");ZipOutputStream zipOut = new ZipOutputStream(fos);for (String srcFile : srcFiles) {File fileToZip = new File(srcFile);FileInputStream fis = new FileInputStream(fileToZip);ZipEntry zipEntry = new ZipEntry(fileToZip.getName());zipOut.putNextEntry(zipEntry);byte[] bytes = new byte[1024];int length;while((length = fis.read(bytes)) >= 0) {zipOut.write(bytes, 0, length);}fis.close();}zipOut.close();fos.close();}
3)压缩文件夹
public static void main(String[] args) throws IOException {String sourceFile = "zipTest";FileOutputStream fos = new FileOutputStream("dirCompressed.zip");ZipOutputStream zipOut = new ZipOutputStream(fos);File fileToZip = new File(sourceFile);zipFile(fileToZip, fileToZip.getName(), zipOut);zipOut.close();fos.close();}private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {if (fileToZip.isHidden()) {return;}if (fileToZip.isDirectory()) {if (fileName.endsWith("/")) {zipOut.putNextEntry(new ZipEntry(fileName));zipOut.closeEntry();} else {zipOut.putNextEntry(new ZipEntry(fileName + "/"));zipOut.closeEntry();}File[] children = fileToZip.listFiles();for (File childFile : children) {zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);}return;}FileInputStream fis = new FileInputStream(fileToZip);ZipEntry zipEntry = new ZipEntry(fileName);zipOut.putNextEntry(zipEntry);byte[] bytes = new byte[1024];int length;while ((length = fis.read(bytes)) >= 0) {zipOut.write(bytes, 0, length);}fis.close();}

2、解压:

public static void main(String[] args) throws IOException {String fileZip = "src/main/resources/unzipTest/compressed.zip";File destDir = new File("src/main/resources/unzipTest");byte[] buffer = new byte[1024];ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));ZipEntry zipEntry = zis.getNextEntry();while (zipEntry != null) {File newFile = newFile(destDir, zipEntry);FileOutputStream fos = new FileOutputStream(newFile);int len;while ((len = zis.read(buffer)) > 0) {fos.write(buffer, 0, len);}fos.close();zipEntry = zis.getNextEntry();}zis.closeEntry();zis.close();}public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {File destFile = new File(destinationDir, zipEntry.getName());String destDirPath = destinationDir.getCanonicalPath();String destFilePath = destFile.getCanonicalPath();if (!destFilePath.startsWith(destDirPath + File.separator)) {throw new IOException("Entry is outside of the target dir: " + zipEntry.getName());}return destFile;}

个人学习记录,上述方法摘自:
https://www.baeldung.com/java-compress-and-uncompress

完美java.util.zip使用,实现zip压缩和解压相关推荐

  1. 解压缩后点击解压.html,如何使用压缩、解压缩软件(WINRAR、ZIP)进行文件的压缩和解压...

    首先,我们来了解一下啥是压缩文件和解压缩文件! 压缩文件:简单的讲,就是被压缩过的文件! 解压文件:对被压缩过的文件进行解压,让其恢复到原来没被压缩的状态! 为什么要压缩文件? 这与现实生活就有点相悖 ...

  2. linux 压缩 日期,Linux 时间日期类、搜索查找类、 压缩和解压类指令

    l 时间日期类 date指令-显示当前日期 基本语法 1) date (功能描述:显示当前时间) 2) date +%Y (功能描述:显示当前年份) 3) date +%m (功能描述:显示当前月份) ...

  3. JAVA 7z Seven Zip 压缩和解压文件

    JAVA 7z Seven Zip 压缩和解压文件 7-Zip是基于GNU LGPL协议发布的软件,通过全新算法使压缩比率大幅提升 本文主要讲解通过JAVA方式把文件压缩成7z文件和对7z文件进行解压 ...

  4. zip压缩文件处理方案(Zip4j压缩和解压)

    主要特性 Create, Add, Extract, Update, Remove files from a Zip file针对ZIP压缩文件创建.添加.抽出.更新和移除文件 Read/Write ...

  5. ZIP文件流压缩和解压

    前面写了一篇文章 "ZIP文件压缩和解压", 介绍了" SharpZipLib.Zip " 的使用, 最近的项目中,在使用的过程中, 遇到一些问题. 比如, 现 ...

  6. linux jar和zip,Linux命令———zip和jar文件压缩解压

    Linux命令---zip和jar文件压缩解压 (1)ubuntu 使用unzip和zip压缩文件 1.功能作用:解压缩zip文件 2.位置:/usr/bin/unzip 3.格式用法:unzip [ ...

  7. Linux的zip压缩文件压缩和解压

    Linux的zip压缩文件压缩和解压 <Linux一线运维实战>清华大学出版社(即将出版) 通常,系统中的文件的可以被直接打开的,这就难免文件的内容被查看.另外,对于要归档保存的(压缩)文 ...

  8. Qt-qrc资源文件-rcc打包-程序调用-ZIP压缩和解压-安装程序制作参考

    文章目录 1.qrc文件编辑 2.将qrc文件转位rcc 3.资源使用 4.ZIP压缩和解压 4.1.解压 4.2.压缩 5.错误 6.作者答疑 将程序的资源文件打包在程序的应用程序内,能够使程序保持 ...

  9. powershell 压缩和解压zip

    项目场景: 前端项目发布到windows环境需要需要先压缩传输后再解压 问题描述 简单的压缩和解压zip在windows下,视窗情况下,右键就可以实现,但是如果是在命令下,windows不自带unzi ...

最新文章

  1. CSP浏览器安全策略备忘
  2. 分析Unicode和UTF-8
  3. LeetCode Surrounded Regions(floodfill算法)
  4. 常用 API 函数(3): 文件处理函数
  5. Spring定时任务的几种实现方式
  6. NET问答: 为什么仅有 getter 的属性,还可以在构造函数中赋值 ?
  7. 计算机网络技术人员介绍,计算机网络技术专业介绍2021 计算机网络技术专业就业方向及前景...
  8. WindowsXamlHost:在 WPF 中使用 UWP 的控件(Windows Community Toolkit)
  9. 关于鸿蒙DevEco Studio学习
  10. Scala学习之Option类
  11. linux service start|stop|restart
  12. 【大云制造】大云PaaS平台BC-PaaS V2.4—— 治理微服务,纳管中间件,平台能力更全面!...
  13. 【狂神说Redis】2Redis入门 2-3测试性能
  14. 三菱plc编程软件gx+developer安装教程
  15. 第一章 基于STM32核心板的电路设计与制作流程
  16. ZigBee串口发送字符串函数
  17. 平台型时间信号强度曲线_MR动态增强扫描时间-信号强度曲线在骨骼肌肉系统肿瘤定性诊断中的价值...
  18. 前端多线程之Worker
  19. 【图像分类】华为云·垃圾分类亚军方案分享
  20. electron 主进程与渲染进程通讯

热门文章

  1. 【问题已解决】Caused by: java.lang.IllegalStateException
  2. 论文怎么防止查重率过高 有哪些避免方式
  3. sql 找到上一次_干货 | 想成为商业分析师/数据分析师, SQL竟成硬门槛?
  4. 个人云服务器部署leanote(蚂蚁笔记)
  5. 5款轻量易上手的团队目标管理软件(推荐收藏)
  6. html 空心字 以及部分艺术字
  7. LINK1104:无法打开文件“shell32.lib”
  8. python 从大到小循环_python-小进阶之循环语句
  9. 蓝桥杯:求一个5位数的最大循环素数
  10. Python3.7版---双人联机雷霆战机(2D特效+音效+道具+Linux系统)