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){

    }

最初に extractFolder の引数として rawFile.getCanonicalPath() (rawFile は最初のコードの抜粋で設定されます) を使用し、次に destFile.getAbsolutePath() に切り替えることに気付きました...おそらくそれと関係があります。これをテストする際の問題は、問題が決定論的ではないことです。起こることもあれば、起こらないこともあります。

4

1 に答える 1