Search code examples
javaiofileutilsapache-commons-io

FileUtils.deleteDirectory attempts to delete directory ending in period


I have a directory where I programmatically (in Java) do recursive unzipping (which seems to work), but in the end I'm left with a directory that has a lot of subdirectories and files. Every time I run this method I want to start with a clean slate, so I always delete the folder and its left-over files and subdirectories present in the temp directory.

    root = new File(System.getProperty("java.io.tmpdir")+ File.separator + "ProductionTXOnlineCompletionDataPreProcessorRoot");
    if(root.exists()){
        try {
            FileUtils.deleteDirectory(root);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if(root.mkdir()){
        rawFile = createRawDataFile();
    }

I'm getting a really strange error from FileUtils.deleteDirectory though.

14:55:27,214 ERROR [stderr] (Thread-3 (HornetQ-client-global-threads-2098205981)) java.io.IOException: Unable to delete directory C:\Users\Admin\AppData\Local\Temp\ProductionTXOnlineCompletionDataPreProcessorRoot\ProductionTXOnlineCompletionDataPreProcessor8718674704286818303.

It seems to think that I have a period at the end of my directory (It doesn't, so it's no surprise that it can't delete it). Sometimes, this error appears on folders in the subdirectory. Has anyone seen this before?

I'm using the Commons IO 2.4 jar.

EDIT I've confirmed that the directories do not have periods, so unless they're invisible, I don't know why the method would think that there are periods. And the File's path that I give the method is set right before feeding it as an argument, and as anyone can see - it doesn't have a period at the end.

I'm running the program on Windows 7.

EDIT This is the code I used for recursively unzipping:

private void extractFolder(String zipFile) throws IOException 
{
    int BUFFER = 2048;
    File file = new File(zipFile);
    ZipFile zip = null;
    String newPath = zipFile.substring(0, zipFile.length() - 4);
    BufferedOutputStream dest = null;
    BufferedInputStream is = null;
    try{
        zip = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();

        while (zipFileEntries.hasMoreElements())
        {
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();
            File destFile = new File(newPath, currentEntry);

            File destinationParent = destFile.getParentFile();

            destinationParent.mkdirs();
            if (!entry.isDirectory())
            {
                is = new BufferedInputStream(zip
                .getInputStream(entry));
                int currentByte;

                byte data[] = new byte[BUFFER];

                FileOutputStream fos = new FileOutputStream(destFile);
                dest = new BufferedOutputStream(fos, BUFFER);

                // read and write until last byte is encountered
                while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, currentByte);
                }
                dest.flush();
            }

            if (currentEntry.endsWith(".zip")){
                // found a zip file, try to open
                extractFolder(destFile.getAbsolutePath());
            }
    }
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        if(dest!=null) {dest.close();}
        if(is!=null) {is.close();}
        zip.close();
    }
}

I put the original zip into the root directory, and recursively unzip from there.

This is the relevant code showing that:

downloadInputStreamToFileInRootDir(in, rawFile);
try {
        extractFolder(rawFile.getCanonicalPath());
    } catch (IOException e) {
        e.printStackTrace();
    }catch (Exception e){

    }

I just noticed that I use rawFile.getCanonicalPath() (rawFile is set in the first code excerpt) as the argument for extractFolder initially and then switch to destFile.getAbsolutePath() ... Maybe that has something to do with it. The problem with testing this is that the issue isn't deterministic. It sometimes happens and sometimes not.


Solution

  • The period is part of the error message. It's not trying to delete a file path with a period at the end. See the FileUtils source:

    if (!directory.delete()) {
        final String message =
                "Unable to delete directory " + directory + ".";
        throw new IOException(message);
    }