/*
 * Autopsy Forensic Browser
 *
 * Copyright 2012 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sleuthkit.autopsy.coreutils;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import org.openide.filesystems.FileObject;

/**
 * File and dir utilities
 */
public class FileUtil {

    private static final Logger logger = Logger.getLogger(FileUtil.class.getName());

    /**
     * Recursively delete a dir
     *
     * @param dirPath dir path of a dir to delete
     * @return true if the dir deleted, false otherwise
     */
    public static boolean deleteDir(File dirPath) {
        if (dirPath.exists()) {
            File[] files = dirPath.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDir(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }
        return (dirPath.delete());

    }

    /**
     * Check if given path is a file or directory, then delete it through
     * recursion with path leading to a file as the base case.
     *
     * @param path the path to the file or directory to delete
     * @return true if the File at path is deleted, false otherwise
     */
    public static boolean deleteFileDir(File path) {
        boolean sucess = true;
        if (path.isFile()) { // If it's a file
            if (!path.delete()) {
                sucess = false;
                logger.log(Level.WARNING, "Failed to delete file {0}", path.getPath());
            }
        } else { // If it's a directory
            if (path.list().length == 0) { // If the dir is empty
                if (!path.delete()) {
                    sucess = false;
                    logger.log(Level.WARNING, "Failed to delete the empty directory at {0}", path.getPath());
                }
            } else {
                String files[] = path.list();
                for (String s : files) {
                    File sub = new File(path, s);
                    sucess = deleteFileDir(sub);
                }
                if (path.list().length == 0) { // Delete the newly-empty dir
                    if (!path.delete()) {
                        sucess = false;
                        logger.log(Level.WARNING, "Failed to delete the empty directory at {0}", path.getPath());
                    }
                } else {
                    sucess = false;
                    logger.log(Level.WARNING, "Directory {0} did not recursivly delete sucessfully.", path.getPath());
                }
            }
        }
        return sucess;
    }

    /**
     * Copy a file to a new directory, potentially new file name, and overwrite old one if requested
     * 
     * @param source source file path
     * @param destFolder destination folder path
     * @param newName file name of the copied file, which can be different from original
     * @param overwrite if new file, already exists, overwrite it (delete it first)
     *
     * @return path to the created file, or null if file was not created
     * @throws IOException exception thrown if file copying failed
     */
    public static String copyFile(String source, String destFolder, String newName, boolean overwrite)
            throws IOException {


        final String destFileName = destFolder + File.separator + newName;
        final File destFile = new File(destFileName);
        if (destFile.exists()) {
            if (overwrite) {
                destFile.delete();
            } else {
                return null;
            }
        }


        final FileObject sourceFileObj = org.openide.filesystems.FileUtil.createData(new File(source));
        final FileObject destFolderObj = org.openide.filesystems.FileUtil.createData(new File(destFolder));

        FileObject created = org.openide.filesystems.FileUtil.copyFile(sourceFileObj, destFolderObj, newName);

        return created.getPath();

    }
}

@@ -3532,16 +3532,63 @@
 riginal%0A
+     * @param ext file extension, e.g. %22.java%22%0A
      * @
@@ -3901,16 +3901,28 @@
 newName,
+ String ext,
  boolean
@@ -4041,16 +4041,22 @@
  newName
+ + ext
 ;%0A      
@@ -4485,24 +4485,127 @@
 tFolder));%0A%0A
+        // org.openide.filesystems.FileUtil.copyFile requires an extension without the %22.%22 e.g. %22java%22%0A
         File
@@ -4700,16 +4700,34 @@
  newName
+, ext.substring(1)
 );%0A%0A    

Improve copy method