日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

解压缩zip文件的工具类

發布時間:2024/3/13 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 解压缩zip文件的工具类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

此方法可以直接解壓縮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文件的工具类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。