ludc
2023-06-15 55517e16da5e7205770bf61fc27c3d06b7d189b5
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
package com.vci.ubcs.resource.utils;
 
import com.vci.ubcs.starter.exception.VciBaseException;
import com.vci.ubcs.starter.web.util.LangBaseUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
/**
 * zip相关的工具类
 * @author wanghong
 * @date 2021-02-02
 */
@Component
public class ZipUtil {
 
    /**
     * 获取zip的字符集
     * @return 默认为GBK,否则需要配置
     */
    private String getZipCharset(){
        return (File.separatorChar=='\\')?"GBK":"UTF-8";
    }
 
    /**
     * 将文件夹压缩成压缩包
     * @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("文件{0}没有找到",new String[]{folder});
        }
        try( ZipOutputStream zos =new ZipOutputStream(new FileOutputStream(zipFileName), StringUtils.hasLength(zipEncode)?Charset.forName(zipEncode): Charset.forName(getZipCharset()));) {
            //默认是UTF-8格式的
            addFileToZip(zos, new File(folder).listFiles(),folder + File.separator);
        }catch (Throwable e){
            throw new VciBaseException(LangBaseUtil.getErrorMsg(e),new String[]{});
        }
    }
 
    /**
     * 将文件夹压缩成压缩包
     * @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.hasLength(rootPath)){
                    try {
                        rootPath = file.getParent();
                        if (rootPath == null) {
                            rootPath = file.getPath();
                        }
                    }catch (Throwable e){
                        rootPath = file.getPath();
                    }
                }
                if (!file.exists()) {
                    throw new VciBaseException("文件{0}没有找到",new String[]{file.getAbsolutePath()});
                }
                if (file.isDirectory()) {
                    addFileToZip(zos, file.listFiles(),rootPath);
                }else{
                    try (InputStream 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("ZIP压缩出错{0}",new String[]{file.getAbsolutePath()},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("文件{0}没有找到" ,new String[]{file.getAbsolutePath()});
        }
        if(!StringUtils.hasLength(zipFile)){
            throw new VciBaseException("zip文件{0}为空",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("文件{0}内容是空",new String[0]);
        }
        if(!StringUtils.hasLength(zipFile)){
            throw new VciBaseException("zip文件{0}是空",new String[0]);
        }
        try (ZipOutputStream zos =new ZipOutputStream(new FileOutputStream(zipFile), StringUtils.hasLength(zipEncode)?Charset.forName(zipEncode): Charset.forName(getZipCharset()));){
            addFileToZip(zos, files,"");
        }catch (Throwable e){
            throw new VciBaseException(LangBaseUtil.getErrorMsg(e),new String[]{},e);
        }
    }
}