fjl
fujunling
2023-07-26 145aca4ce40cbdb14f940975da44db63eb5c497f
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.vci.ubcs.file.util;
 
 
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.vci.ubcs.file.constant.VciFileLangCodeConstant;
import com.vci.ubcs.file.properties.VciFileTransProperties;
import com.vci.ubcs.starter.exception.VciBaseException;
import com.vci.ubcs.starter.web.util.LangBaseUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
 
/**
 * zip相关的工具类
 * @author weidy
 * @date 2020/3/12
 */
@Component
public class VciZipUtil {
 
    /**
     * 文件传输配置
     */
    @Autowired(required = false)
    private VciFileTransProperties transProperties;
 
    /**
     * 获取zip的字符集
     * @return 默认为GBK,否则需要配置
     */
    private String getZipCharset(){
        return (transProperties == null || StringUtils.isBlank(transProperties.getZipCharset()))?"GBK":transProperties.getZipCharset();
    }
 
    /**
     * 将文件夹压缩成压缩包
     * @param folder 文件夹路径
     * @param zipFileName zip文件名
     * @throws VciBaseException 参数为空或者添加出错的会抛出异常
     */
    public void folderToZipFile(String folder, String zipFileName) throws VciBaseException {
        folderToZipFile(folder,zipFileName,getZipCharset());
    }
 
    /**
     * 将文件夹压缩成压缩包
     * @param folder 文件夹路径
     * @param zipFileName zip文件名
     * @param zipEncode zip的字符集,windows应该为GBK
     * @throws VciBaseException 参数为空或者添加出错的会抛出异常
     */
    public void folderToZipFile(String folder, String zipFileName,String zipEncode) throws VciBaseException{
        File sourceFile = new File(folder);
        if(!sourceFile.exists()){
            throw new VciBaseException(VciFileLangCodeConstant.FILE_NOT_FOUND,new String[]{folder});
        }
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(new FileOutputStream(zipFileName));
            zos.setEncoding(zipEncode);
            //默认是UTF-8格式的
            addFileToZip(zos, new File(folder).listFiles(),folder + File.separator);
        }catch (Throwable e){
            throw new VciBaseException(LangBaseUtil.getErrorMsg(e),new String[]{});
        }finally {
            try{
                if(zos!=null){
                    zos.close();
                }
            }catch (Exception e){
 
            }
        }
    }
 
    /**
     * 将文件夹压缩成压缩包
     * @param zos zip输出流
     * @param files 要压缩的文件
     * @param rootPath 压缩的文件中的顶层目录
     * @throws VciBaseException 文件不存在,或者内容出现错误会抛出异常
     */
    private void addFileToZip(ZipOutputStream zos,File[] files,String rootPath) throws VciBaseException{
        if(files!=null && files.length>0) {
            for (File file : files) {
                if(StringUtils.isBlank(rootPath)){
                    try {
                        rootPath = file.getParent();
                        if (rootPath == null) {
                            rootPath = file.getPath();
                        }
                    }catch (Throwable e){
                        rootPath = file.getPath();
                    }
                }
                if (!file.exists()) {
                    throw new VciBaseException(VciFileLangCodeConstant.FILE_NOT_FOUND,new String[]{file.getAbsolutePath()});
                }
                if (file.isDirectory()) {
                    addFileToZip(zos, file.listFiles(),rootPath);
                }else{
                    InputStream 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];
                        int length = 0;
                        while ((length = is.read(b)) != -1) {
                            zos.write(b, 0, length);
                        }
                    } catch (Exception e) {
                        throw new VciBaseException(VciFileLangCodeConstant.ZIP_FILE_ERROR,new String[]{file.getAbsolutePath()},e);
                    } finally {
                        try {
                            IOUtils.closeQuietly(is);
                        } catch (Exception e) {
 
                        }
                    }
                }
            }
        }
    }
 
    /**
     * 将单个文件添加到zip中
     * @param file 文件
     * @param zipFile zip文件
     * @throws VciBaseException 参数为空或者添加出错的会抛出异常
     */
    public void addFileToZip(File file, String zipFile) throws VciBaseException{
        if(file == null || !file.exists() ){
            throw new VciBaseException(VciFileLangCodeConstant.FILE_NOT_FOUND ,new String[]{file.getAbsolutePath()});
        }
        if(StringUtils.isBlank(zipFile)){
            throw new VciBaseException(VciFileLangCodeConstant.ZIP_FILE_NAME_NULL,new String[0]);
        }
        addFileToZip(new File[]{file},zipFile);
    }
 
 
    /**
     * 将多个个文件添加到zip中
     * @param files 文件
     * @param zipFile zip文件
     * @throws VciBaseException 参数为空或者添加出错的会抛出异常
     */
    public void addFileToZip(File[] files, String zipFile) throws VciBaseException{
        addFileToZip(files, zipFile,getZipCharset());
    }
 
    /**
     * 将多个个文件添加到zip中
     * @param files 文件
     * @param zipFile zip文件
     * @param zipEncode zip的字符集
     * @throws VciBaseException 参数为空或者添加出错的会抛出异常
     */
    public void addFileToZip(File[] files, String zipFile,String zipEncode) throws VciBaseException{
        if(files == null || files.length == 0){
            throw new VciBaseException(VciFileLangCodeConstant.FILES_NULL,new String[0]);
        }
        if(StringUtils.isBlank(zipFile)){
            throw new VciBaseException(VciFileLangCodeConstant.ZIP_FILE_NAME_NULL,new String[0]);
        }
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(new FileOutputStream(zipFile));
            zos.setEncoding(zipEncode);
            addFileToZip(zos, files,"");
        }catch (Throwable e){
            throw new VciBaseException(LangBaseUtil.getErrorMsg(e),new String[]{},e);
        }finally {
            try{
                if(zos!=null){
                    zos.close();
                }
            }catch (Exception e){
 
            }
        }
    }
}