首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >替换多级zip文件夹中的文件

问替换多级zip文件夹中的文件
EN

Stack Overflow用户
提问于 2016-05-11 22:16:36
回答 1查看 136关注 0票数 0

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

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

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

有人能帮帮忙吗?

谢谢,德利普

EN

回答 1

Stack Overflow用户

发布于 2016-05-12 19:14:43

一种方法是使用临时文件来存储压缩文件,并将其作为FileSystems打开,这样您就不需要解压所有文件并重新压缩所有内容

代码语言:javascript
复制
    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。我从来没有试过,但我认为这可能会有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37165381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档