簡體   English   中英

使用 Java FileSystems 刪除 zip 中的多個文件

[英]Deleting multiple files in a zip using Java FileSystems

public static void deleteFilesInZip(
    String zipStringPath,
    List<String> paths
) throws IOException
{
    Map<String, String> env = new HashMap<>();
    env.put("create", "false");
    Path zipPath = Paths.get(zipStringPath);
    URI uri = URI.create("jar:" + zipPath.toUri());
    try (FileSystem fs = FileSystems.newFileSystem(uri, env))
    {
        for(String path: paths)
        {
            Path filePath = fs.getPath(path);
            Files.delete(filePath);
        }
    }
}

上述邏輯在 MAC OS 中運行良好,但在 Windows 中引發以下錯誤:

錯誤:java.nio.file.FileSystemException:C:\\Users\\XSXDXX\\Downloads\\Test.zip:進程無法訪問該文件,因為它正被另一個進程使用。

當我知道需要事先刪除的文件的路徑時,刪除 Zip 文件的最佳方法是什么?

你應該記住 Windows 中的路徑斜線是反向的: c:\\ 你能舉一個你的路徑的例子嗎?

想通了這個問題。 上面的代碼沒有問題,結果是使用了一個 Zip Reader 服務,它不會關閉 zip 條目。 想知道為什么這不會在 MAC OS 中引起問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM