javazipjimfs

Bundling zip files in memory with Java


I need to bundle several zip files (jars). I've tried the following solutions:

  1. Pour from ZipInputStream to ZipOutputStream by going over each entry of the input-stream and copying it to the output stream (from what I understand this is mostly IO intensive operation happening on the hard drive) using the code as in this stack-overflow question

  2. Create in memory file system, extract both zips to the same dir and then compress again. I'm using jimfs - https://github.com/google/jimfs

Option 2 took about 5 times less for me.

I was looking an out of the box solution for a zip in memory fs but didn't find any. Also - the jimfs can't be combined with the Java 7 nio zipfs (there isn't an API to use the zipfs with underline jimfs).

My solution doesn't seem clean/using the best practices so I'm looking for advice/out of the box solution/library that will help me achieve in memory performance without developing & maintaining a library


Solution

  • Apperatnly there is a way to bundle ZipFS with JIMFS - that is, to treat a file (which is stored in memory - by JIFS) as a Zip file system.

      FileSystems.newFileSystem(
          URI.create("jar:" + pathToZipFile.toUri()),
          Collections.singletonMap("create", "true"));
    

    where pathToZipFile is the jar file stored in JIMFS