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 list(String path) { File file = new File(path); return list(file, TrueFilter.TRUE); } public static List 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 list(String path, FileFilter filter) { File file = new File(path); return list(file, filter); } public static List list(File file) { List fileList = new ArrayList<>(); return list(file, fileList, TrueFilter.TRUE); } public static List list(File file, final String fileNamePattern) { List fileList = new ArrayList<>(); return list(file, fileList, pathname -> { String fileName = pathname.getName(); return PatternMatchUtils.simpleMatch(fileNamePattern, fileName); }); } public static List list(File file, FileFilter filter) { List fileList = new ArrayList<>(); return list(file, fileList, filter); } private static List list(File file, List 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; } } }