此方法可以直接解压缩zip中的文件及文件夹至指定目录,zip中文件名称不能包含繁体字符。


import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;/*** @description: 解壓縮文件* @author: libie* @createTime: 2022/6/10 上午 08:50**/
public class ZipFileUtil {/*** @description: 解壓縮文件* @author:  H2103424* @dateTime: 2022/6/10 下午 01:50** @param file 需要解壓縮的zip文件* @param uncompressPath 解壓縮目的目錄* @return 壓縮包中包含的文件及文件夾* @throws IOException*/public static List<String> uncompress(File file, String uncompressPath) throws IOException {List<String> filePaths = new ArrayList<>();File path = new File(uncompressPath);if (!path.exists()){if (!path.mkdirs())throw new IOException("解壓路徑創建失敗:"+uncompressPath);System.out.println("解壓路徑創建成功:"+uncompressPath);}BufferedOutputStream os = null;BufferedInputStream is = null;ZipEntry entry;ZipFile zipfile = new ZipFile(file);Enumeration ele = zipfile.entries();while (ele.hasMoreElements()) {try {entry = (ZipEntry) ele.nextElement();} catch (IllegalArgumentException e){System.err.println("文件夾或文件名不合法,請不要包含繁體漢字及特殊字符:"+e);continue;}if( entry.isDirectory()){System.out.println("創建文件夾 "+entry.getName());String name = entry.getName();name = name.substring(0, name.length() - 1);File fileObject = new File(uncompressPath + name);if (!fileObject.exists() && !fileObject.mkdir()){System.err.println("文件夾創建失敗:"+fileObject.getName());}}else{System.out.println("解壓文件   "+entry.getName());is = new BufferedInputStream(zipfile.getInputStream(entry));int count;int buffer = 1024;byte[] dataByte = new byte[buffer];FileOutputStream fos = new FileOutputStream(uncompressPath+entry.getName());os = new BufferedOutputStream(fos, buffer);while ((count = is.read(dataByte, 0, buffer)) != -1) {os.write(dataByte, 0, count);}os.flush();os.close();is.close();}filePaths.add(uncompressPath+entry.getName());}zipfile.close();return filePaths;}public static void main(String[] args) throws IOException {File file = new File("C:\\Users\\H2103424\\Desktop\\Pictures.zip");System.out.println(uncompress(file, "C:\\Users\\H2103424\\Desktop\\testZip\\"));}
}

压缩包:

解压效果:

方法输出及返回值:


解壓路徑創建成功:C:\Users\H2103424\Desktop\testZip\
創建文件夾 Pictures/
解壓文件   Pictures/1.bmp
解壓文件   Pictures/1.jpeg
解壓文件   Pictures/1.jpg
創建文件夾 Pictures/1a/
解壓文件   Pictures/1a/1.jpeg
解壓文件   Pictures/1a/1.jpg
創建文件夾 Pictures/1a/3c/
解壓文件   Pictures/1a/3c/2.jpg
解壓文件   Pictures/1a/3c/3.jpg
創建文件夾 Pictures/1a/4d/
解壓文件   Pictures/2.jpg
創建文件夾 Pictures/2b/
解壓文件   Pictures/2b/2.jpg
解壓文件   Pictures/3.jpg
解壓文件   Pictures/3241_45235_342.3ds
解壓文件   Pictures/4.jpg
創建文件夾 Pictures/5e/
解壓文件   Pictures/aaa.jpg
解壓文件   Pictures/desktop.ini
解壓文件   Pictures/m03_j03_sa.dwg
解壓文件   Pictures/rewq_rewq.dwg
[C:\Users\H2103424\Desktop\testZip\Pictures/, C:\Users\H2103424\Desktop\testZip\Pictures/1.bmp, C:\Users\H2103424\Desktop\testZip\Pictures/1.jpeg, C:\Users\H2103424\Desktop\testZip\Pictures/1.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/1a/, C:\Users\H2103424\Desktop\testZip\Pictures/1a/1.jpeg, C:\Users\H2103424\Desktop\testZip\Pictures/1a/1.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/1a/3c/, C:\Users\H2103424\Desktop\testZip\Pictures/1a/3c/2.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/1a/3c/3.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/1a/4d/, C:\Users\H2103424\Desktop\testZip\Pictures/2.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/2b/, C:\Users\H2103424\Desktop\testZip\Pictures/2b/2.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/3.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/3241_45235_342.3ds, C:\Users\H2103424\Desktop\testZip\Pictures/4.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/5e/, C:\Users\H2103424\Desktop\testZip\Pictures/aaa.jpg, C:\Users\H2103424\Desktop\testZip\Pictures/desktop.ini, C:\Users\H2103424\Desktop\testZip\Pictures/m03_j03_sa.dwg, C:\Users\H2103424\Desktop\testZip\Pictures/rewq_rewq.dwg]Process finished with exit code 0

Controller调用:

    @Overridepublic ResultObj uploadPhotos(MultipartFile file, WebSysUserEntity loginUser){String uncompressPath = "C:\\Users\\H2103424\\Desktop\\testZip\\";try(InputStream inputStream = file.getInputStream()){File photosZip = new File("photos.zip");FileUtils.copyInputStreamToFile(inputStream, photosZip);photosZip.delete();ZipFileUtil.uncompress(photosZip, uncompressPath);return null;} catch (IOException e) {e.printStackTrace();return null;}}

解压缩zip文件的工具类相关推荐

  1. 导出PDF和Zip文件的工具类

    一.导出pdf文件,使用itext框架: 1.引入pom文件: <dependency><groupId>com.itextpdf</groupId><art ...

  2. Java代码实现解压文件包和压缩文件的工具类

    最近开发任务比较多,这两天陆陆续续整理了一点资料上传一下,这个是前段时间用到的解压和压缩文件的工具类,网上找了一些,自己补充一下,现在先分享一下,希望对各位同学有所帮助! package com.as ...

  3. php zip解压原理,PHP ZipArchive实现解压缩zip文件

    PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有 ...

  4. 小米开源文件管理器MiCodeFileExplorer-源码研究(8)-文件排序工具类FileSortHelper

    FileSortHelper的核心功能就是,对文件集合FileInfo排序. FileInfo有若干字段,根据字段定义了4种比较器Comparator. 调用示例:Collections.sort(L ...

  5. 基于POI的读写Excel文件的工具类

    依赖的jar包: import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStrea ...

  6. php 对比两个压缩包内容,php实现的zip文件内容比较类

    本文实例讲述了php实现的zip文件内容比较类.是一个非常实用的PHP类文件.分享给大家供大家参考.具体分析如下: 该php zip文件比较类主要实现比较两个zip文件的内容,返回新增,删除,及相同的 ...

  7. Python写入文件的工具类

    主要分享一个写入文件的工具类,便于在开发中经常调试写入文件的情况 Python中删除文件时使用:os.remove("文件路径"),下面的工具类已经包含了这个功能 示例代码 # - ...

  8. java url类下载_Java根据url下载图片或文件的工具类-Fun言

    package cn.funyan.utils; import java.io.FileOutputStream; import java.io.IOException; import java.io ...

  9. java 文件解压缩zip_java解压缩zip文件

    /* 提供zip文件的解压缩接口: AdapterZipFile: 输入:zipFileName(zip文件的绝对路径),outputDirectory(zip文件解压缩后的存放路径) 输出: 说明: ...

最新文章

  1. python list 删除元素
  2. socket 服务器浏览器与服务器客户端实例
  3. mysql 提交_MySQL 事务提交过程
  4. [Oracle]Oracle 各产品的 生命周期
  5. java面试题_阿里大厂流出的数百道 Java 经典面试题
  6. 计算机科学计算方面分为,计算机方面的专业分为哪些类?【资讯与计算科学】和【电脑科学与技术专业】有什么不同?...
  7. html木桶布局,CSS3如何实现图片木桶布局?(附代码)
  8. spring-boot-maven-plugin多模块install问题解决办法
  9. yii2之ActiveRecord 模型
  10. HTML中标签的ref属性,itemref(属性) | itemref (attribute)
  11. 数据库学生管理系统课程设计
  12. 用php和mysql写一个注册登录页面
  13. 软件著作权申请时间是多久?
  14. Windows10指纹识别设置
  15. 富受贿数额为45437元
  16. oracle查看历史oracle database数据库版本并下载
  17. Android系统消息推送
  18. 【导数术】9.指对互化和指对同构
  19. 【C++内存管理侯捷】---学习笔记(下)malloc/free,loki allocator,other issue
  20. Apple Developer文档笔记(一)AppKit App Structure

热门文章

  1. SHERlocked93 的 2019 年终总结
  2. 干货来袭!腾讯T4大佬,十分钟教你用svg做出精美的动画
  3. 配置node服务器并且链接微信公众号接口配置(超详细)
  4. 背景运动补偿具体思路
  5. mysql优化 个人笔记 (mysql锁机制 ) 非礼勿扰 -m10
  6. java EE 第十二周 web前端开发的周总结 (含思维导图)
  7. 透明图片怎么发给别人_新手微商没生意咋办?微商怎么做如何推广?不放弃微信就是等死!...
  8. ubuntu查看本机ip命令
  9. 右键快捷菜单压缩文件的消失问题解决办法!
  10. 科学计算法(机器学习)----决策树定义以相关概念