dcb
2025-06-18 4c4d0f591f94428ed7e5d2f4ae5df5c5087d8c26
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
package com.se.nsl.utils;
 
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ZipUtil;
 
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
 
/**
 * zip
 */
public class ZipUtils {
    // 解压zip格式
    public static void unzip(String zipFilePath, String destDir) {
        //所有的文件路径
        List<String> filePaths = new ArrayList<>();
 
        File destDirectory = new File(destDir);
        if (!destDirectory.exists()) {
            if (!destDirectory.mkdirs()) {
            }
        }
        try (ZipFile zipFile = new ZipFile(zipFilePath)) {
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
 
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                String filePath = destDir + File.separator + entry.getName();
                // 确保目标文件路径的父目录存在
                File entryDestination = new File(filePath);
                File parentDirectory = entryDestination.getParentFile();
                if (parentDirectory != null && !parentDirectory.exists()) {
                    parentDirectory.mkdirs();
                }
                if (!entry.isDirectory()) {
                    // 提取文件
                    try (InputStream is = zipFile.getInputStream(entry);
                         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) {
                        byte[] bytesIn = new byte[4096];
                        int read;
                        while ((read = is.read(bytesIn)) != -1) {
                            bos.write(bytesIn, 0, read);
                        }
                    }
                    // 打印文件路径
                    filePaths.add(filePath);
                } else {
                    // 如果是目录,则创建目录(注意:在上面的mkdirs调用中已经处理了)
                    // 这里也可以选择打印目录路径
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("GBK编码");
            try {
                File file = ZipUtil.unzip(zipFilePath, destDir, CharsetUtil.CHARSET_GBK);
                File[] file1 = file.listFiles();
                for (int i = 0; i < file1.length; i++) {
                    filePaths.add(file1[i].getAbsolutePath());
                }
            } catch (Exception ex) {
                e.printStackTrace();
            }
        }
    }
 
    public static void zip(String sourceDirPath, String zipFilePath) throws IOException {
        Path sourceDir = Paths.get(sourceDirPath);
        try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFilePath))) {
            Files.walk(sourceDir).forEach(path -> {
                try {
                    String zipEntryName = sourceDir.relativize(path).toString();
                    if (Files.isDirectory(path)) {
                        // 如果是目录,创建一个空的ZIP目录
                        if (!zipEntryName.isEmpty()) {
                            zipOutputStream.putNextEntry(new ZipEntry(zipEntryName + "/"));
                        }
                    } else {
                        // 如果是文件,写入ZIP文件
                        zipOutputStream.putNextEntry(new ZipEntry(zipEntryName));
                        Files.copy(path, zipOutputStream);
                    }
                    zipOutputStream.closeEntry();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }
    }
 
    public static void toZarr(String sourceDir, String zipFile) {
        try {
            zip(sourceDir, zipFile);
            System.out.println("文件夹已成功压缩为: " + zipFile);
            updateFileSuffix(zipFile, "zarr");
            deleteFile(sourceDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public static void updateFileSuffix(String filePath, String newSuffix) {
        // 创建File对象
        File file = new File(filePath);
 
        // 检查文件是否存在
        if (file.exists()) {
            // 获取文件名和后缀
            String fileName = file.getName();
            int dotIndex = fileName.lastIndexOf('.');
            if (dotIndex > 0) {
                // 分离文件名和后缀
                String name = fileName.substring(0, dotIndex);
                // 组合新的文件名
                String newFileName = name + '.' + newSuffix;
 
                // 重命名文件
                File newFile = new File(file.getParent(), newFileName);
                if (file.renameTo(newFile)) {
                    System.out.println("文件重命名成功:" + newFile.getName());
                } else {
                    System.out.println("文件重命名失败。");
                }
            } else {
                System.out.println("文件没有后缀。");
            }
        } else {
            System.out.println("文件不存在。");
        }
    }
 
    public static void deleteFile(String path) {
        // 目标文件夹的路径
        File directory = new File(path);
        // 检查文件夹是否存在
        if (!directory.exists()) {
            System.out.println("文件夹不存在!");
            return; // 文件夹不存在,退出程序
        }
        // 调用deleteDirectory方法删除文件夹及其内容
        if (deleteDirectory(directory)) {
            System.out.println("文件夹已成功删除!");
        } else {
            System.out.println("文件夹删除失败!");
        }
    }
 
    // 递归删除文件夹及其内容的方法
    public static boolean deleteDirectory(File directory) {
        if (directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    // 递归处理
                    deleteDirectory(file);
                }
            }
        }
        // 删除当前文件或文件夹
        return directory.delete();
    }
 
}