19 package org.sleuthkit.autopsy.coreutils;
22 import java.io.IOException;
23 import java.nio.charset.Charset;
24 import java.nio.charset.StandardCharsets;
25 import java.util.logging.Level;
26 import org.openide.filesystems.FileObject;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
50 if (dirPath.isDirectory() ==
false || dirPath.exists() ==
false) {
51 logger.log(Level.WARNING,
"deleteDir passed in a non-directory: {0}", dirPath.getPath());
55 File[] files = dirPath.listFiles();
56 boolean hadErrors =
false;
58 for (File file : files) {
59 if (file.isDirectory()) {
65 if (file.delete() ==
false) {
66 logger.log(Level.WARNING,
"Failed to delete file {0}", file.getPath());
72 if (dirPath.delete() ==
false) {
73 logger.log(Level.WARNING,
"Failed to delete the empty directory at {0}", dirPath.getPath());
90 boolean sucess =
true;
94 logger.log(Level.WARNING,
"Failed to delete file {0}", path.getPath());
118 public static String
copyFile(String source, String destFolder, String newName, String ext,
boolean overwrite)
121 final String destFileName = destFolder + File.separator + newName + ext;
122 final File destFile =
new File(destFileName);
123 if (destFile.exists()) {
131 final FileObject sourceFileObj =
org.openide.filesystems.FileUtil.createData(
new File(source));
132 final FileObject destFolderObj =
org.openide.filesystems.FileUtil.createData(
new File(destFolder));
135 FileObject created =
org.openide.filesystems.FileUtil.copyFile(sourceFileObj, destFolderObj, newName, ext.substring(1));
137 return created.getPath();
152 public static String
copyFolder(String source, String path, String folderName)
throws IOException {
153 String destFolder = path + File.separator + folderName;
154 org.openide.filesystems.FileUtil.createFolder(
new File(destFolder));
156 final FileObject sourceFileObj =
org.openide.filesystems.FileUtil.createData(
new File(source));
157 final FileObject destFolderObj =
org.openide.filesystems.FileUtil.createData(
new File(destFolder));
159 FileObject created =
org.openide.filesystems.FileUtil.copyFile(sourceFileObj, destFolderObj, sourceFileObj.getName(), sourceFileObj.getExt());
161 return created.getPath();
174 return fileName.replaceAll(
"[\\p{Cntrl}/:\"*?<>|]+",
"_");
185 Charset charset = StandardCharsets.UTF_8;
186 return charset.decode(charset.encode(
escapeFileName(fileName))).toString();
200 p = Files.createTempFile(dirPath, TEMP_FILE_NAME, TEMP_FILE_EXT);
201 return (p.toFile().canRead() && p.toFile().canWrite());
202 }
catch (IOException ex) {
208 }
catch (Exception ignored) {
static final String TEMP_FILE_NAME
static String copyFile(String source, String destFolder, String newName, String ext, boolean overwrite)
static final Logger logger
static boolean deleteFileDir(File path)
static boolean hasReadWriteAccess(Path dirPath)
static final String TEMP_FILE_EXT
static String utf8SanitizeFileName(String fileName)
static String copyFolder(String source, String path, String folderName)
static String escapeFileName(String fileName)
synchronized static Logger getLogger(String name)
static boolean deleteDir(File dirPath)