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 fileToPatch(Collection fileList) { VciBaseUtil.alertNotNull(new Object[]{"文件列表", fileList}); List 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; } }