1

我有一个目录,我以编程方式(在 Java 中)进行递归解压缩(这似乎可行),但最后我留下了一个包含很多子目录和文件的目录。每次运行此方法时,我都想从头开始,因此我总是删除临时目录中存在的文件夹及其剩余文件和子目录。

    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();
    }

不过,我从 FileUtils.deleteDirectory 收到了一个非常奇怪的错误。

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.

似乎认为我的目录末尾有一个句点(它没有,所以它不能删除它也就不足为奇了)。有时,此错误会出现在子目录中的文件夹上。有没有人见过这个?

我正在使用 Commons IO 2.4 jar。

编辑我已经确认目录没有句点,所以除非它们不可见,否则我不知道为什么该方法会认为有句点。我给该方法的文件路径是在将其作为参数提供之前设置的,并且任何人都可以看到 - 它最后没有句点。

我在 Windows 7 上运行该程序。

编辑这是我用于递归解压缩的代码:

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();
    }
}

我将原始 zip 放入根目录,然后从那里递归解压缩。

这是显示的相关代码:

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

    }

我只是注意到我最初使用 rawFile.getCanonicalPath() (rawFile 在第一个代码摘录中设置)作为 extractFolder 的参数,然后切换到 destFile.getAbsolutePath() ...也许与它有关。对此进行测试的问题在于问题不是确定性的。它有时会发生,有时不会。

4

1 回答 1

2

句点是错误消息的一部分。它不是试图删除末尾带有句点的文件路径。请参阅FileUtils源代码:

if (!directory.delete()) {
    final String message =
            "Unable to delete directory " + directory + ".";
    throw new IOException(message);
}
于 2017-02-07T20:05:03.287 回答