0

My requirement is to replace few files in a zip file.

The zip file in turn have multiple zip files and folder within it. It goes upto 4 or more levels of zip files.

I have a set of files in a different source directory. I would like to copy these files and replace within the zip file, by matching the file name in the source directory with file name inside the zip file.

Could someone help here please.

Thanks, Deleep

Deleep
  • 1
  • 1
  • if you are using Linux, this link might help you http://stackoverflow.com/questions/4799553/how-to-update-one-file-in-a-zip-archive – Y.Kaan Yılmaz May 11 '16 at 14:19

1 Answers1

0

One way is use temp files to store the ZIPs and open them as FileSystems, so you dont need to extrat all files and rezip everything

    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();

Other idea is jboss-vfs. Never tried it, but i think that could help.

fhofmann
  • 847
  • 7
  • 15