解压缩后无法删除zip文件?

Joe*_*ery 5 java unzip fileutils

我正面临一个不寻常的问题.我正在建造一个计划每5分钟运行一次的工具.它将从特定目录中获取zip文件,并将文件(取决于文件名)提取到目标.我zipentry用来获取zip文件中的每个文件名然后根据需要提取然后我将它们(zip文件,一旦我完成zip中的所有文件)备份到特定目录然后删除zip文件.但有时(并非总是)zip文件不会被删除.因为我正在使用fileutils.forcedelete().我收到一个例外:无法删除文件.所以我改为代码使用fileutils.forcedeleteonexit()仍然有些文件保留在源代码中.

以下是我的代码示例:

sourceFile=new file(zipfile);
zipFile = new ZipFile(sourceFile);
zEnum = (Enumeration<ZipEntry>) zipFile.entries();
for (int a = 0; a < zipFile.size(); a++)
{
  ZipEntry zE = zEnum.nextElement();
  //Function uses zip4j for extracting. No streams used.
  extract(String sourceZipFile, String fileNameToExtract, String outputFolder);
}
//I tried it with finally either
zipFile.close();

//Using fileutils to copy. No streams used.
copyFile(sourceFile, backup);

FileUtils.forceDeleteOnExit(sourceFile);
Run Code Online (Sandbox Code Playgroud)

没有使用流,但我有时(不总是)锁定文件.什么似乎是这里的错误?是zip4j提取导致问题还是其他什么?我使用的是zip4j 1.3.1.

Chr*_*ris 0

使用 apache-commons IO 的FileDeleteStrategy。就像是:

FileDeleteStrategy.FORCE.delete(file);
Run Code Online (Sandbox Code Playgroud)

更新

这应该是您的应用程序中处理 IO 的方式。我编写了简单的代码,它将 zip 文件复制到临时 zip,压缩临时 zip,几秒钟后将其删除。干得好:

public class ZipTest {

private static String dirPath = "/home/ubuntuuser/Desktop/";

public static void main(String[] args) throws Exception {

    File myzip = new File(dirPath + "content.zip");
    String tempFileStr = dirPath + "content_temp.zip";
    File tempFile = new File(tempFileStr);
    String unzipFolderStr = dirPath + "unzip";


    copyUsingChannels(myzip, tempFile);

    // copyUsingStreams(myzip, tempFile);

    unZip(tempFileStr, unzipFolderStr);

    Thread.sleep(3000);

    tempFile.delete();


}

private static void copyUsingStreams(File myzip, File tempFile)
        throws IOException, FileNotFoundException {
    byte[] barray = new byte[1024];

    if (!tempFile.exists())
    {
        tempFile.createNewFile();
    }

    FileOutputStream fos = new FileOutputStream(tempFile);
    FileInputStream fis = new FileInputStream(myzip);

    int length = 0;

    while ((length = fis.read(barray)) != -1)
    {
        fos.write(barray, 0, length);
    }

    fis.close();
    fos.close();
}

public static void copyUsingChannels(final File srcFile, final File destFile) throws Exception
{
    if (!destFile.exists())
    {
        destFile.createNewFile();
    }

    FileChannel source = new FileInputStream(srcFile).getChannel();
    FileChannel destination = new FileOutputStream(destFile).getChannel();

    source.transferTo(0, source.size(), destination);

    source.close();
    destination.close();
}

private static void unZip(String zipFile, String outputFolder) throws Exception {

    byte[] buffer = new byte[1024];

    File folder = new File(outputFolder);
    if (!folder.exists()) {
        folder.mkdir();
    }

    ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {

        String fileName = ze.getName();

        File newFile = new File(outputFolder + File.separator + fileName);

        System.out.println("file unzip : " + newFile.getAbsoluteFile());

        new File(newFile.getParent()).mkdirs();

        if (ze.isDirectory())
        {
            newFile.mkdir();
            ze = zis.getNextEntry();
            continue;
        }

        FileOutputStream fos = new FileOutputStream(newFile);

        int len;
        while ((len = zis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
        }

        fos.close();
        ze = zis.getNextEntry();
    }

    zis.closeEntry();
    zis.close();
}

}
Run Code Online (Sandbox Code Playgroud)