【问题标题】:Add file to a folder that is inside a zip file java将文件添加到 zip 文件 java 中的文件夹中
【发布时间】:2015-10-19 05:24:49
【问题描述】:

我正在尝试将 txt 文件添加到 zip 文件中的文件夹中。 首先,我提取了 zip 文件的所有内容,然后添加了 txt 文件,然后再压缩回来。 然后我读到了 nio 方法,我可以修改 zip 而不提取它。使用这种方法,我可以将 txt 文件添加到 zip 的主文件夹中,但我不能更深入。

testing.zip 文件中有 res 文件夹。

这是我的代码:

        Path txtFilePath = Paths.get("\\test\\prefs.txt");
        Path zipFilePath = Paths.get("\\test\\testing.zip");
        FileSystem fs;
        try {
            fs = FileSystems.newFileSystem(zipFilePath, null);
            Path fileInsideZipPath = fs.getPath("res/prefs.txt");  //when I remover "res/" code works.
            Files.copy(txtFilePath, fileInsideZipPath);
            fs.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

我得到以下异常:

java.nio.file.NoSuchFileException: res/

【问题讨论】:

    标签: java zip nio


    【解决方案1】:

    (编辑给出实际答案)

    做:

    fs.getPath("res").resolve("prefs.txt")
    

    代替:

    fs.getPath("res/prefs.txt")
    

    .resolve() 方法将在文件分隔符等方面做正确的事情。

    【讨论】:

    • 试试fs.getPath("res").resolve("prefs.txt"),然后,而不是fs.getPath("res/prefs.txt")
    【解决方案2】:

    fs.getPath("res/prefs.txt") 肯定可以工作,并且您不需要按照批准的答案将其拆分为 fs.getPath("res").resolve("prefs.txt")

    java.nio.file.NoSuchFileException: res/ 异常有点令人困惑,因为它提到了文件,但实际上缺少目录。

    我遇到了类似的问题,我所要做的就是:

    if (fileInsideZipPath.getParent() != null)
       Files.createDirectories(fileInsideZipPath.getParent());
    

    查看完整示例:

    @Test
    public void testAddFileToArchive() throws Exception {
        Path fileToAdd1 = rootTestFolder.resolve("notes1.txt");
        addFileToArchive(archiveFile, "notes1.txt", fileToAdd1);
    
        Path fileToAdd2 = rootTestFolder.resolve("notes2.txt");
        addFileToArchive(archiveFile, "foo/bar/notes2.txt", fileToAdd2);
    
        . . . 
    }
    
    
    public void addFileToArchive(Path archiveFile, String pathInArchive, Path srcFile) throws Exception {        
        FileSystem fs = FileSystems.newFileSystem(archiveFile, null);
        Path fileInsideZipPath = fs.getPath(pathInArchive);
        if (fileInsideZipPath.getParent() != null) Files.createDirectories(fileInsideZipPath.getParent());
        Files.copy(srcFile, fileInsideZipPath, StandardCopyOption.REPLACE_EXISTING);
        fs.close();
    }
    

    如果我删除 Files.createDirectories() 位,并确保从清除测试目录开始,我得到:

    java.nio.file.NoSuchFileException: foo/bar/
    at com.sun.nio.zipfs.ZipFileSystem.checkParents(ZipFileSystem.java:863)
    at com.sun.nio.zipfs.ZipFileSystem.newOutputStream(ZipFileSystem.java:528)
    at com.sun.nio.zipfs.ZipPath.newOutputStream(ZipPath.java:792)
    at com.sun.nio.zipfs.ZipFileSystemProvider.newOutputStream(ZipFileSystemProvider.java:285)
    at java.nio.file.Files.newOutputStream(Files.java:216)
    at java.nio.file.Files.copy(Files.java:3016)
    at java.nio.file.CopyMoveHelper.copyToForeignTarget(CopyMoveHelper.java:126)
    at java.nio.file.Files.copy(Files.java:1277)
    at my.home.test.zipfs.TestBasicOperations.addFileToArchive(TestBasicOperations.java:111)
    at my.home.test.zipfs.TestBasicOperations.testAddFileToArchive(TestBasicOperations.java:51)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-04
      • 1970-01-01
      • 2022-11-07
      • 2023-01-14
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多