【问题标题】:Replacing files in multilevel zip folders替换多级 zip 文件夹中的文件
【发布时间】:2016-09-07 00:24:11
【问题描述】:

我的要求是替换 zip 文件中的几个文件。

该 zip 文件又包含多个 zip 文件和文件夹。它可以达到 4 个或更多级别的 zip 文件。

我在不同的源目录中有一组文件。我想复制这些文件并在 zip 文件中替换,方法是将源目录中的文件名与 zip 文件中的文件名匹配。

请有人帮忙。

谢谢, 深渊

【问题讨论】:

标签: java replace zip hierarchical multi-level


【解决方案1】:

一种方法是使用临时文件来存储 ZIP 并将它们作为文件系统打开,因此您不需要提取所有文件并重新压缩所有内容

    FileSystem firstZip = FileSystems.newFileSystem(URI.create("jar:file:/root/firstZip.zip"),
            new HashMap<>(), null);

    //Get a zip inside the first one
    Path path = firstZip.getPath("FOLDER/ZIP_NAME.zip");
    //Get the second zip input stream  and store the zip in a temp file
    InputStream in  = firstZip.provider().newInputStream(path);
    Path secondPath = Paths.get("/root/temp/tempFile.tmp");
    Files.copy(in, secondPath);
    //open the second zip
    FileSystem secondZip = FileSystems.newFileSystem(new URI("jar:" + secondPath.toUri().toString()),
            new HashMap<>(), null);
    //iterate files in the second zip
    DirectoryStream<Path> ds = Files.newDirectoryStream(secondPath);
    for(Path p: ds){
        //something
    }
    //delete or update files inside the second zip
    //Files.delete(seconZip.getPath("aFile.txt"));
    //Files.copy(anInputStream, secondZip.getPath("destFoldet/aFile.txt"));

    //clse the seconzip
    secondZip.close();
    //update the second zip inside first one
    Files.copy(secondPath, firstZip.provider().newOutputStream(path));
    //delete temp file
    Files.delete(secondPath);
    //close first zip
    firstZip.close();

其他想法是jboss-vfs。从未尝试过,但我认为这会有所帮助。

【讨论】:

    猜你喜欢
    • 2014-06-01
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2021-06-01
    • 2015-01-18
    • 1970-01-01
    相关资源
    最近更新 更多