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