ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package com.vci.web.util.file;
 
import com.vci.properties.VciFileTransProperties;
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.util.LangBaseUtil;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
 
/**
 * @author ludc
 * @date 2024/7/16 14:27
 */
@Component
public class VciZipUtil {
    @Autowired(
            required = false
    )
    private VciFileTransProperties transProperties;
 
    public VciZipUtil() {
    }
 
    private String getZipCharset() {
        return this.transProperties != null && !StringUtils.isBlank(this.transProperties.getZipCharset()) ? this.transProperties.getZipCharset() : "GBK";
    }
 
    public void folderToZipFile(String folder, String zipFileName) throws VciBaseException {
        this.folderToZipFile(folder, zipFileName, this.getZipCharset());
    }
 
    public void folderToZipFile(String folder, String zipFileName, String zipEncode) throws VciBaseException {
        File sourceFile = new File(folder);
        if (!sourceFile.exists()) {
            throw new VciBaseException("fileNotFound", new String[]{folder});
        } else {
            ZipOutputStream zos = null;
 
            try {
                zos = new ZipOutputStream(new FileOutputStream(zipFileName));
                zos.setEncoding(zipEncode);
                this.addFileToZip(zos, (new File(folder)).listFiles(), folder + File.separator);
            } catch (Throwable var14) {
                throw new VciBaseException(LangBaseUtil.getErrorMsg(var14), new String[0]);
            } finally {
                try {
                    if (zos != null) {
                        zos.close();
                    }
                } catch (Exception var13) {
                }
 
            }
 
        }
    }
 
    private void addFileToZip(ZipOutputStream zos, File[] files, String rootPath) throws VciBaseException {
        if (files != null && files.length > 0) {
            File[] var4 = files;
            int var5 = files.length;
 
            for(int var6 = 0; var6 < var5; ++var6) {
                File file = var4[var6];
                if (StringUtils.isBlank(rootPath)) {
                    try {
                        rootPath = file.getParent();
                        if (rootPath == null) {
                            rootPath = file.getPath();
                        }
                    } catch (Throwable var21) {
                        rootPath = file.getPath();
                    }
                }
 
                if (!file.exists()) {
                    throw new VciBaseException("fileNotFound", new String[]{file.getAbsolutePath()});
                }
 
                if (file.isDirectory()) {
                    this.addFileToZip(zos, file.listFiles(), rootPath);
                } else {
                    FileInputStream is = null;
 
                    try {
                        is = new FileInputStream(file);
                        String entryName = file.getAbsolutePath().replace(rootPath, "");
                        if (entryName.startsWith("\\")) {
                            entryName = entryName.substring(1);
                        }
 
                        ZipEntry zipEntry = new ZipEntry(entryName);
                        zos.putNextEntry(zipEntry);
                        byte[] b = new byte[100];
                        boolean var12 = false;
 
                        int length;
                        while((length = is.read(b)) != -1) {
                            zos.write(b, 0, length);
                        }
                    } catch (Exception var22) {
                        throw new VciBaseException("zipFileError", new String[]{file.getAbsolutePath()}, var22);
                    } finally {
                        try {
                            IOUtils.closeQuietly(is);
                        } catch (Exception var20) {
                        }
 
                    }
                }
            }
        }
 
    }
 
    public void addFileToZip(File file, String zipFile) throws VciBaseException {
        if (file != null && file.exists()) {
            if (StringUtils.isBlank(zipFile)) {
                throw new VciBaseException("zipFileNameNull", new String[0]);
            } else {
                this.addFileToZip(new File[]{file}, zipFile);
            }
        } else {
            throw new VciBaseException("fileNotFound", new String[]{file.getAbsolutePath()});
        }
    }
 
    public void addFileToZip(File[] files, String zipFile) throws VciBaseException {
        this.addFileToZip(files, zipFile, this.getZipCharset());
    }
 
    public void addFileToZip(File[] files, String zipFile, String zipEncode) throws VciBaseException {
        if (files != null && files.length != 0) {
            if (StringUtils.isBlank(zipFile)) {
                throw new VciBaseException("zipFileNameNull", new String[0]);
            } else {
                ZipOutputStream zos = null;
 
                try {
                    zos = new ZipOutputStream(new FileOutputStream(zipFile));
                    zos.setEncoding(zipEncode);
                    this.addFileToZip(zos, files, "");
                } catch (Throwable var13) {
                    throw new VciBaseException(LangBaseUtil.getErrorMsg(var13), new String[0], var13);
                } finally {
                    try {
                        if (zos != null) {
                            zos.close();
                        }
                    } catch (Exception var12) {
                    }
 
                }
 
            }
        } else {
            throw new VciBaseException("fileNull", new String[0]);
        }
    }
}