lihang
2023-04-25 3fade6d3b27f5666672bb3af610020367f790bda
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package com.vci.ubcs.starter.util;
 
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
 
import com.vci.ubcs.starter.exception.VciBaseException;
import com.vci.ubcs.starter.web.util.VciBaseUtil;
import org.apache.commons.io.IOUtils;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.boot.system.ApplicationHome;
 
public class LocalFileUtil {
    public static Class mainClass = null;
 
    public LocalFileUtil() {
    }
 
    public static void deleteFile(File file) {
        if (file != null && file.exists()) {
            if (file.isDirectory()) {
                File[] files = file.listFiles();
 
                for(int i = 0; i < files.length; ++i) {
                    deleteFile(files[i]);
                }
            }
 
            file.delete();
        }
    }
 
    public static void copyFile(File source, File target) {
        FileInputStream is = null;
        FileOutputStream os = null;
 
        try {
            is = new FileInputStream(source);
            os = new FileOutputStream(target);
            FileChannel sourceChannel = is.getChannel();
            FileChannel targetChannel = os.getChannel();
            int i = 0;
 
            for(int length = 2097152; sourceChannel.position() != sourceChannel.size(); ++i) {
                if (sourceChannel.size() - sourceChannel.position() < 20971520L) {
                    length = (int)(sourceChannel.size() - sourceChannel.position());
                } else {
                    length = 20971520;
                }
 
                sourceChannel.transferTo(sourceChannel.position(), (long)length, targetChannel);
                sourceChannel.position(sourceChannel.position() + (long)length);
            }
 
            sourceChannel.close();
            targetChannel.close();
        } catch (Throwable var11) {
            throw new VciBaseException("拷贝文件出错", new String[0], var11);
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(os);
        }
    }
 
    public static String getDefaultTempFolder()  {
        String path = getProjectFolder();
        path = path + File.separator + "tempFolder" + File.separator + UUID.randomUUID().toString();
        File folder = new File(path);
        if (!folder.exists()) {
            folder.mkdirs();
        }
 
        return path;
    }
 
    public static String getProjectFolder()  {
        return getProjectFolder(LocalFileUtil.class);
    }
 
    public static String getProjectFolder(Class classObj) {
        String path = "";
 
        try {
            ApplicationHome h = new ApplicationHome(classObj == null ? (mainClass == null ? LocalFileUtil.class : mainClass) : classObj);
            File jarF = h.getSource();
            if (jarF == null) {
                jarF = h.getDir();
            }
 
            path = jarF.getParentFile().toString();
            if (path.contains("!")) {
                path = (new File(path)).getParentFile().getParent();
            }
        } catch (Throwable var4) {
            var4.printStackTrace();
            throw new VciBaseException("获取当前服务所在的文件夹出现了错误", new String[0], var4);
        }
 
        if (path.startsWith("file:\\")) {
            path = path.substring(6);
        }
 
        if (System.getProperty("os.name").toLowerCase().startsWith("win") && path.startsWith("/")) {
            path = path.substring(path.indexOf("/") + 1);
        }
 
        System.out.println("项目的路径是:" + path);
        return path;
    }
 
    public static void deleteTempFile(File tempFile, boolean deleteRandomFolder) {
        if (tempFile != null) {
            tempFile.delete();
            if (deleteRandomFolder) {
                tempFile.getParentFile().delete();
            }
        }
 
    }
 
    public static void alertNotNull(Object... s)  {
        if (s != null && s.length > 0) {
            for(int i = 0; i < s.length; ++i) {
                Object obj = s[i];
                if (obj == null || StringUtil.isBlank(obj.toString())) {
                    String param = "";
 
                    try {
                        ++i;
                        param = s[i].toString();
                    } catch (Exception var5) {
                    }
 
                    throw new VciBaseException("参数[" + param + "]不能为空");
                }
            }
        }
 
    }
 
    public static String getFileExtension(File file) {
        return file != null ? getFileExtensionByName(file.getName()) : "";
    }
 
    public static String getFileExtensionByName(String name) {
        return StringUtil.isNotBlank(name) && name.contains(".") ? name.substring(name.lastIndexOf(".") + 1) : "";
    }
 
    public static String getFileNameForIE(String name) {
        if (StringUtil.isBlank(name)) {
            return name;
        } else if (name.contains(File.separator)) {
            return name.substring(name.lastIndexOf(File.separator) + 1);
        } else {
            return name.contains("\\") ? name.substring(name.lastIndexOf("\\") + 1) : name;
        }
    }
 
    public static String readContentForFileInJar(String fileName)  {
        if (StringUtil.isBlank(fileName)) {
            throw new VciBaseException("文件名为空");
        } else {
            if (!fileName.startsWith("/")) {
                fileName = "/" + fileName;
            }
 
            InputStream in = null;
            ByteArrayOutputStream os = null;
 
            String var3;
            try {
                in = LocalFileUtil.class.getResourceAsStream(fileName);
                if (in == null) {
                    in = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
                }
 
                os = new ByteArrayOutputStream();
                IOUtils.copy(in, os);
                var3 = new String(os.toByteArray(), "UTF-8");
            } catch (Throwable var7) {
                throw new VciBaseException("读取文件的内容出现了错误", new String[0], var7);
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(os);
            }
 
            return var3;
        }
    }
 
    public static String readContentForFile(String fileName)  {
        File file = new File(fileName);
        if (!file.exists()) {
            throw new VciBaseException("文件未找到,{0}", new String[]{fileName});
        } else {
            InputStream in = null;
            ByteArrayOutputStream os = null;
 
            String var4;
            try {
                in = new FileInputStream(file);
                os = new ByteArrayOutputStream();
                IOUtils.copy(in, os);
                var4 = new String(os.toByteArray(), "UTF-8");
            } catch (Throwable var8) {
                throw new VciBaseException("读取文件的内容出现了错误", new String[0], var8);
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(os);
            }
 
            return var4;
        }
    }
 
    public static void writeContentToFile(String content, String fileName)  {
        File file = new File(fileName);
        if (!file.exists()) {
            File parentFile = file.getParentFile();
            if (!parentFile.exists()) {
                parentFile.mkdirs();
            }
 
            try {
                file.createNewFile();
            } catch (IOException var11) {
                throw new VciBaseException("写入文件的内容时候出现了创建文件失败的问题,{}", new String[]{fileName}, var11);
            }
        }
 
        if (content == null) {
            content = "";
        }
 
        OutputStream os = null;
 
        try {
            os = new FileOutputStream(file);
            os.write(content.getBytes(StandardCharsets.UTF_8));
            os.flush();
        } catch (Throwable var9) {
            throw new VciBaseException("写入文件的内容时候出现了创错误,{}", new String[]{fileName}, var9);
        } finally {
            IOUtils.closeQuietly(os);
        }
 
    }
 
    public static void copyFileInJar(String fileInJar, String target)  {
        VciBaseUtil.alertNotNull(new Object[]{fileInJar, "源文件", target, "目标文件"});
        File targetFile = new File(target);
        File folder = targetFile.getParentFile();
        if (!folder.exists()) {
            folder.mkdirs();
        }
 
        if (!targetFile.exists()) {
            try {
                targetFile.createNewFile();
            } catch (IOException var11) {
                throw new VciBaseException("创建目标文件的出现了错误", new String[0], var11);
            }
        }
 
        if (!fileInJar.startsWith("/")) {
            fileInJar = "/" + fileInJar;
        }
 
        InputStream in = null;
        FileOutputStream os = null;
 
        try {
            in = LocalFileUtil.class.getResourceAsStream(fileInJar);
            if (in == null) {
                in = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileInJar);
            }
 
            os = new FileOutputStream(target);
            IOUtils.copy(in, os);
        } catch (Throwable var12) {
            throw new VciBaseException("拷贝文件的内容出现了错误.{0},{1}", new String[]{fileInJar, target}, var12);
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(os);
        }
 
    }
 
    public static List<String> fileToPatch(Collection<File> fileList) {
        VciBaseUtil.alertNotNull(new Object[]{"文件列表", fileList});
        List<String> filePath = new ArrayList();
        Iterator var2 = fileList.iterator();
 
        while(var2.hasNext()) {
            File file = (File)var2.next();
            if (!file.exists()) {
                throw new VciBaseException(file.getAbsolutePath() + "文件没有找到" + new String[0]);
            }
 
            filePath.add(file.getAbsolutePath());
        }
 
        return filePath;
    }
}