Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
FileUtil.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2012-2015 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.coreutils;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.logging.Level;
24 import org.openide.filesystems.FileObject;
25 
29  public class FileUtil {
30 
31  private static final Logger logger = Logger.getLogger(FileUtil.class.getName());
32 
40  public static boolean deleteDir(File dirPath) {
41  if (dirPath.isDirectory() == false || dirPath.exists() == false) {
42  logger.log(Level.WARNING, "deleteDir passed in a non-directory: {0}", dirPath.getPath()); //NON-NLS
43  return false;
44  }
45 
46  File[] files = dirPath.listFiles();
47  boolean hadErrors = false;
48  if (files != null) {
49  for (File file : files) {
50  if (file.isDirectory()) {
51  if (deleteDir(file) == false) {
52  // message was already logged
53  hadErrors = true;
54  }
55  } else {
56  if (file.delete() == false) {
57  logger.log(Level.WARNING, "Failed to delete file {0}", file.getPath()); //NON-NLS
58  hadErrors = true;
59  }
60  }
61  }
62  }
63  if (dirPath.delete() == false) {
64  logger.log(Level.WARNING, "Failed to delete the empty directory at {0}", dirPath.getPath()); //NON-NLS
65  hadErrors = true;
66  }
67 
68  return hadErrors;
69  }
70 
78  public static boolean deleteFileDir(File path) {
79  boolean sucess = true;
80  if (path.isFile()) { // If it's a file
81  if (!path.delete()) {
82  sucess = false;
83  logger.log(Level.WARNING, "Failed to delete file {0}", path.getPath()); //NON-NLS
84  }
85  } else { // If it's a directory
86  sucess = deleteDir(path);
87  }
88  return sucess;
89  }
90 
103  public static String copyFile(String source, String destFolder, String newName, String ext, boolean overwrite)
104  throws IOException {
105 
106 
107  final String destFileName = destFolder + File.separator + newName + ext;
108  final File destFile = new File(destFileName);
109  if (destFile.exists()) {
110  if (overwrite) {
111  destFile.delete();
112  } else {
113  return null;
114  }
115  }
116 
117 
118  final FileObject sourceFileObj = org.openide.filesystems.FileUtil.createData(new File(source));
119  final FileObject destFolderObj = org.openide.filesystems.FileUtil.createData(new File(destFolder));
120 
121  // org.openide.filesystems.FileUtil.copyFile requires an extension without the "." e.g. "java"
122  FileObject created = org.openide.filesystems.FileUtil.copyFile(sourceFileObj, destFolderObj, newName, ext.substring(1));
123 
124  return created.getPath();
125 
126  }
127 
138  public static String copyFolder(String source, String path, String folderName) throws IOException {
139  String destFolder = path + File.separator + folderName;
140  org.openide.filesystems.FileUtil.createFolder(new File(destFolder));
141 
142  final FileObject sourceFileObj = org.openide.filesystems.FileUtil.createData(new File(source));
143  final FileObject destFolderObj = org.openide.filesystems.FileUtil.createData(new File(destFolder));
144 
145  FileObject created = org.openide.filesystems.FileUtil.copyFile(sourceFileObj, destFolderObj, sourceFileObj.getName(), sourceFileObj.getExt());
146 
147  return created.getPath();
148  }
149 
155  public static String escapeFileName(String fileName) {
156  //for now escaping / (not valid in file name, at least on Windows)
157  //with underscores. Windows/Java seem to ignore \\/ and \\\\/ escapings
158  return fileName.replaceAll("/", "_");
159  }
160 }
static String copyFile(String source, String destFolder, String newName, String ext, boolean overwrite)
Definition: FileUtil.java:103
static boolean deleteFileDir(File path)
Definition: FileUtil.java:78
static String copyFolder(String source, String path, String folderName)
Definition: FileUtil.java:138
static String escapeFileName(String fileName)
Definition: FileUtil.java:155
static boolean deleteDir(File dirPath)
Definition: FileUtil.java:40
static Logger getLogger(String name)
Definition: Logger.java:131

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.