PC
2024-03-31 608f20e0d5d8f95d9bbb917e95e2913682deb77d
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package com.qianwen.core.tool.utils;
 
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.util.ArrayList;
import java.util.List;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.PatternMatchUtils;
import org.springframework.web.multipart.MultipartFile;
 
/* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/utils/FileUtil.class */
public class FileUtil extends FileCopyUtils {
 
    /* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/utils/FileUtil$TrueFilter.class */
    public static class TrueFilter implements FileFilter, Serializable {
        private static final long serialVersionUID = -6420452043795072619L;
        public static final TrueFilter TRUE = new TrueFilter();
 
        @Override // java.io.FileFilter
        public boolean accept(File pathname) {
            return true;
        }
    }
 
    public static List<File> list(String path) {
        File file = new File(path);
        return list(file, TrueFilter.TRUE);
    }
 
    public static List<File> list(String path, final String fileNamePattern) {
        File file = new File(path);
        return list(file, pathname -> {
            String fileName = pathname.getName();
            return PatternMatchUtils.simpleMatch(fileNamePattern, fileName);
        });
    }
 
    public static List<File> list(String path, FileFilter filter) {
        File file = new File(path);
        return list(file, filter);
    }
 
    public static List<File> list(File file) {
        List<File> fileList = new ArrayList<>();
        return list(file, fileList, TrueFilter.TRUE);
    }
 
    public static List<File> list(File file, final String fileNamePattern) {
        List<File> fileList = new ArrayList<>();
        return list(file, fileList, pathname -> {
            String fileName = pathname.getName();
            return PatternMatchUtils.simpleMatch(fileNamePattern, fileName);
        });
    }
 
    public static List<File> list(File file, FileFilter filter) {
        List<File> fileList = new ArrayList<>();
        return list(file, fileList, filter);
    }
 
    private static List<File> list(File file, List<File> fileList, FileFilter filter) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    list(f, fileList, filter);
                }
            }
        } else {
            boolean accept = filter.accept(file);
            if (file.exists() && accept) {
                fileList.add(file);
            }
        }
        return fileList;
    }
 
    public static String getFileExtension(String fullName) {
        String fileName;
        int dotIndex;
        return (StringUtil.isBlank(fullName) || (dotIndex = (fileName = new File(fullName).getName()).lastIndexOf(46)) == -1) ? StringPool.EMPTY : fileName.substring(dotIndex + 1);
    }
 
    public static String getNameWithoutExtension(String fullName) {
        if (StringUtil.isBlank(fullName)) {
            return StringPool.EMPTY;
        }
        String fileName = new File(fullName).getName();
        int dotIndex = fileName.lastIndexOf(46);
        return dotIndex == -1 ? fileName : fileName.substring(0, dotIndex);
    }
 
    public static String getTempDirPath() {
        return System.getProperty("java.io.tmpdir");
    }
 
    public static File getTempDir() {
        return new File(getTempDirPath());
    }
 
    public static String readToString(final File file) {
        return readToString(file, Charsets.UTF_8);
    }
 
    public static String readToString(final File file, final Charset encoding) {
        try {
            InputStream in = Files.newInputStream(file.toPath(), new OpenOption[0]);
            String readToString = IoUtil.readToString(in, encoding);
            if (in != null) {
                if (0 != 0) {
                    in.close();
                } else {
                    in.close();
                }
            }
            return readToString;
        } catch (IOException e) {
            throw Exceptions.unchecked(e);
        }
    }
 
    public static byte[] readToByteArray(final File file) {
        try {
            InputStream in = Files.newInputStream(file.toPath(), new OpenOption[0]);
            byte[] readToByteArray = IoUtil.readToByteArray(in);
            if (in != null) {
                if (0 != 0) {
                    in.close();
                } else {
                    in.close();
                }
            }
            return readToByteArray;
        } catch (IOException e) {
            throw Exceptions.unchecked(e);
        }
    }
 
    public static void writeToFile(final File file, final String data) {
        writeToFile(file, data, Charsets.UTF_8, false);
    }
 
    public static void writeToFile(final File file, final String data, final boolean append) {
        writeToFile(file, data, Charsets.UTF_8, append);
    }
 
    public static void writeToFile(final File file, final String data, final Charset encoding) {
        writeToFile(file, data, encoding, false);
    }
 
    public static void writeToFile(final File file, final String data, final Charset encoding, final boolean append) {
        try {
            OutputStream out = new FileOutputStream(file, append);
            IoUtil.write(data, out, encoding);
            if (out != null) {
                if (0 != 0) {
                    out.close();
                } else {
                    out.close();
                }
            }
        } catch (IOException e) {
            throw Exceptions.unchecked(e);
        }
    }
 
    public static void toFile(MultipartFile multipartFile, final File file) {
        try {
            toFile(multipartFile.getInputStream(), file);
        } catch (IOException e) {
            throw Exceptions.unchecked(e);
        }
    }
 
    public static void toFile(InputStream in, final File file) {
        try {
            OutputStream out = new FileOutputStream(file);
            copy(in, out);
            if (out != null) {
                if (0 != 0) {
                    out.close();
                } else {
                    out.close();
                }
            }
        } catch (IOException e) {
            throw Exceptions.unchecked(e);
        }
    }
 
    public static void moveFile(final File srcFile, final File destFile) throws IOException {
        Assert.notNull(srcFile, "Source must not be null");
        Assert.notNull(destFile, "Destination must not be null");
        if (!srcFile.exists()) {
            throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
        }
        if (srcFile.isDirectory()) {
            throw new IOException("Source '" + srcFile + "' is a directory");
        }
        if (destFile.exists()) {
            throw new IOException("Destination '" + destFile + "' already exists");
        }
        if (destFile.isDirectory()) {
            throw new IOException("Destination '" + destFile + "' is a directory");
        }
        boolean rename = srcFile.renameTo(destFile);
        if (!rename) {
            copy(srcFile, destFile);
            if (!srcFile.delete()) {
                deleteQuietly(destFile);
                throw new IOException("Failed to delete original file '" + srcFile + "' after copy to '" + destFile + StringPool.SINGLE_QUOTE);
            }
        }
    }
 
    public static boolean deleteQuietly(@Nullable final File file) {
        if (file == null) {
            return false;
        }
        try {
            if (file.isDirectory()) {
                FileSystemUtils.deleteRecursively(file);
            }
        } catch (Exception e) {
        }
        try {
            return file.delete();
        } catch (Exception e2) {
            return false;
        }
    }
}