多单位版国产化地质资料管理系统
zs
2026-02-04 fe02f176b512a9d6a4e12437d929e04e99bb7567
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
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.zbooksoft.gdmis.common;
 
import com.ruili.wcp.configsettting.SystemConfig;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.*;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
 
/**
 * @Description:
 * @Author: zhai
 * @Date: 2024/9/22
 **/
public class ZipUtil {
    private static final Logger logger = LoggerFactory.getLogger(ZipUtil.class);
 
    public static String zipFile(File path, String format) {
        String generatePath = "";
        if (!path.exists()) {
            path.mkdirs();
        }
        try {
            if (path.isDirectory()) {
                generatePath = path.getParent().endsWith("/") == false ? path.getParent() + File.separator + path.getName() + "." + format
                        : path.getParent() + path.getName() + "." + format;
            } else {
                generatePath = path.getParent().endsWith("/") == false ? path.getParent() + File.separator : path.getParent();
                generatePath += path.getName().substring(0, path.getName().lastIndexOf(".")) + "." + format;
            }
            // 输出流
            FileOutputStream outputStream = new FileOutputStream(generatePath);
            // 压缩输出流
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));
            zip(out, path, "");
            out.flush();
            out.close();
            return generatePath;
        } catch (Exception e) {
            return null;
        } finally {
            try {
                FileUtils.deleteDirectory(path);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
    private static void zip(ZipOutputStream output, File file, String childPath) throws Exception {
        FileInputStream input = null;
        try {
            // 文件为目录
            if (file.isDirectory()) {
                // 得到当前目录里面的文件列表
                File list[] = file.listFiles();
                childPath = childPath + (childPath.length() == 0 ? "" : "/") + file.getName();
                // 循环递归压缩每个文件
                for (File f : list) {
                    zip(output, f, childPath);
                }
            } else {
                // 压缩文件
                childPath = (childPath.length() == 0 ? "" : childPath + "/") + file.getName();
                output.putNextEntry(new ZipEntry(childPath));
                input = new FileInputStream(file);
                int readLen = 0;
                byte[] buffer = new byte[1024 * 8];
                while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1) {
                    output.write(buffer, 0, readLen);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info(ex.getMessage());
        } finally {
            // 关闭流
            if (input != null) {
                try {
                    input.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
 
    }
 
    /**
     * 复制文件
     *
     * @param srcPath 源文件绝对路径
     * @param
     * @return boolean
     */
    public static String copyFile(String srcPath, String destPath) {
        File srcFile = new File(srcPath);
        if (!srcFile.exists()) { // 源文件不存在
            return "文件不存在!";
        }
 
        // 获取待复制文件的文件名
        File file = new File(srcPath);
        if (destPath.equals(srcPath)) { // 源文件路径和目标文件路径重复
            return "文件路径重复!";
        }
 
        File destFile = new File(destPath);
        if (destFile.exists() && destFile.isFile()) { // 该路径下已经有一个同名文件
            return destPath + ",已经有一个同名文件!";
        }
 
        String destDir = destFile.getParentFile().getPath();
        File destFileDir = new File(destDir);
        destFileDir.mkdirs();
 
        try {
            FileUtils.copyFile(file, destFile);
            return "";
        } catch (IOException e) {
            logger.info(e.getMessage());
            return "文件归档出错!";
        }
 
    }
 
    public static void main(String[] args) {
        String paths = "D:\\RecordFile\\dataPack\\2024090006_20240927101254\\";
        File path = new File(paths);
        if (!path.exists()) {
            path.mkdirs();
        }
        boolean directory = path.isDirectory();
        System.out.println(directory);
    }
 
    /**
     * 解压zip文件
     *
     * @param multipartFile
     */
    private String unzip(MultipartFile multipartFile, String dataSource) throws Exception {
        try {
            //metaBusActivity
            String fileName = multipartFile.getOriginalFilename();
            SystemConfig config = SystemConfig.getInstance();
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
            SimpleDateFormat formatterYear = new SimpleDateFormat("yyyy");
            String strDate = formatter.format(date);
            String formatYear = formatterYear.format(date);
            // 待接收 /部门编号/年度/年度月份/
            String destDir = config.getAttachUploadPath() + "待接收" + File.separator + formatYear + File.separator + strDate + File.separator + dataSource;
 
            String filePath = destDir + File.separator + fileName.substring(0, fileName.indexOf('.'));
 
            File fileFolder = new File(destDir);
            // 存入文件夹
            if (!fileFolder.exists() && !fileFolder.isDirectory()) {
                fileFolder.mkdirs();
            }
 
            File targetFile = new File(fileFolder, fileName);
            multipartFile.transferTo(targetFile);
            ZipFile zp = null;
 
            //指定编码,否则压缩包里面不能有中文目录
            zp = new ZipFile(targetFile, Charset.forName("gbk"));
            //遍历里面的文件及文件夹
            Enumeration entries = zp.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                String zipEntryName = entry.getName();
                InputStream in = zp.getInputStream(entry);
                String outpath = (filePath + File.separator + zipEntryName).replace("/", File.separator);
                //判断路径是否存在,不存在则创建文件路径
                File file = new File(outpath.substring(0, outpath.lastIndexOf(File.separator)));
                if (!file.exists()) {
                    file.mkdirs();
                }
                //判断文件全路径是否为文件夹,如果是,不需要解压
                if (new File(outpath).isDirectory()) {
                    continue;
                }
                OutputStream out = new FileOutputStream(outpath);
                byte[] bf = new byte[2048];
                int len;
                while ((len = in.read(bf)) > 0) {
                    out.write(bf, 0, len);
                }
                in.close();
                out.close();
            }
            zp.close();
            return filePath;
        } catch (Exception e) {
            throw new Exception("解压失败");
        }
    }
}