多单位版国产化地质资料管理系统
zhai
2025-09-17 7a8a84577894b766a95c6cbfa5ac7b51e54ac242
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
package com.zbooksoft.gdmis.common;
 
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.*;
import java.util.zip.ZipEntry;
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);
    }
}