Bundling zip files in memory with Java

825 Views Asked by At

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

2

There are 2 best solutions below

0
On BEST ANSWER

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

0
On

What about this one:

  1. For given zip file do find all file entries;
  2. Read each entry to byte[];
  3. Here you have a Map;
  4. Define unique key for the zip file;
  5. Create Map> that holds in memory all read zip files.