提交 | 用户 | 时间
ce910c 1 package com.iailab.module.data.dev.hikvision.utils;
H 2
3 import java.io.*;
4 import java.nio.charset.Charset;
5 import java.util.Enumeration;
6 import java.util.zip.ZipEntry;
7 import java.util.zip.ZipFile;
8
9 /**
10  * @author PanZhibao
11  * @Description
12  * @createTime 2023年05月25日 15:28:00
13  */
14 public class UnzipUtils {
15     /**
16      * 解压zip压缩文件到指定目录
17      *
18      * @param zipPath zip压缩文件绝对路径
19      * @param descDir 指定的解压目录
20      */
21     public static void unzipFile(String zipPath, String descDir) throws IOException {
22         try {
23             File zipFile = new File(zipPath);
24             if (!zipFile.exists()) {
25                 throw new IOException("要解压的压缩文件不存在");
26             }
27             File pathFile = new File(descDir);
28             if (!pathFile.exists()) {
29                 pathFile.mkdirs();
30             }
31             unzipWithStream(zipPath, descDir);
32         } catch (Exception e) {
33             throw new IOException(e);
34         }
35     }
36
37     /**
38      * 解压
39      *
40      * @param zipPath
41      * @param descDir
42      */
43     public static void unzipWithStream(String zipPath, String descDir) {
44         try (ZipFile zipFile = new ZipFile(zipPath, Charset.forName("GBK"))) {
45             Enumeration e = zipFile.entries();
46             while (e.hasMoreElements()) {
47                 ZipEntry zipEntry = (ZipEntry)e.nextElement();
48                 String zipEntryNameStr = zipEntry.getName();
49                 String zipEntryName = zipEntryNameStr;
50                 /*if (zipEntryNameStr.contains("/")) {
51                     String str1 = zipEntryNameStr.substring(0, zipEntryNameStr.indexOf("/"));
52                     zipEntryName = zipEntryNameStr.substring(str1.length() + 1);
53                 }*/
54                 String outPath = (descDir + zipEntryName).replace("\\\\", "/");
55                 File outFile = new File(outPath.substring(0, outPath.lastIndexOf('/')));
56                 if (!outFile.exists()) {
57                     outFile.mkdirs();
58                 }
59                 if (new File(outPath).isDirectory()) {
60                     continue;
61                 }
62                 writeFile(outPath, zipFile.getInputStream(zipEntry));
63             }
64             System.out.println("======解压成功=======");
65         } catch (IOException e) {
66             System.out.println("压缩包处理异常,异常信息{}" + e);
67         }
68     }
69
70     /**
71      * 将流写到文件中
72      *
73      * @param filePath
74      * @param zipInputStream
75      */
76     public static void writeFile(String filePath, InputStream zipInputStream) throws IOException {
77         try (OutputStream outputStream = new FileOutputStream(filePath)) {
78             byte[] bytes = new byte[4096];
79             int len;
80             while ((len = zipInputStream.read(bytes)) != -1) {
81                 outputStream.write(bytes, 0, len);
82             }
83         } catch (IOException ex) {
84             System.out.println("解压文件时,写出到文件出错");
85         } finally {
86             zipInputStream.close();
87         }
88     }
89
90     /**
91      * 测试方法
92      *
93      * @param args
94      * @throws IOException
95      */
96     public static void test(String[] args) throws IOException {
97
98         String zipPath = "D:/test/测试文件.zip";
99         String descDir = "D:/test/解压/";
100
101         unzipFile(zipPath, descDir);
102     }
103 }