0

I'd like to extract a zip/jar entry into memory so I can close the zip stream/FS and keep the file in the JVM without dealing with temporary copies.

One option is to use Files.readAllLines(Path pathToZipFSentry), but it seems it used a buffered reader which can penalize a lot for big files.

So I am researching how to do it in Java NIO2 and it seems the way is ending with a MappedByteBuffer through FileChannels.

I cannot use the RandomAccessFile.getChannel() as I come from a Path of a virtual FileSystem, not a literal File. I cannot use Files.newByteChannel(rscPath, StandardOpenOption.READ) and then (fileChannel.)map() because it returns a SeekableByteChannel which doesn't got map in the interface...

Is thee any one/two high level liners for this by means of Path(s)/File(s)/FileSystem(s)/FileChannel(s) in Java8? I would expect something like InMemoryFile file = Files.loadIntoMem(Path) I've been 1 hour looking for a close option...

Whimusical
  • 6,401
  • 11
  • 62
  • 105
  • Have you tried and `FileChannel.open()`? But anyway, this will not solve the problem that you cannot directly mmap() from a zip entry in any case; there _will_ be a temporary copy somewhere – fge Feb 15 '15 at 12:48
  • That's precisely the approach I'm going through now and the exact fear I am getting while I am advancing, but I feel ilike it must be an easier option somwehere – Whimusical Feb 15 '15 at 12:51
  • By the way, confirmed, com.sun.nio.zipfs.ZipFileSystem.map() returns a NotSupportedoperationException(); – Whimusical Feb 15 '15 at 13:43

1 Answers1

1

You cannot really load this directly into memory.

You do have FileChannel.open() from which you can then .map() but that will create a temporary entry on your disk anyway.

There is also memoryfilesystem, but it will not handle files big enough for it to be useful, unfortunately.

The best solution I see is to Files.copy() into a temporary file and mmap() that; then copy back to the zip file when you're done with the modifications.

But basically, this is what you already do, so...

fge
  • 119,121
  • 33
  • 254
  • 329
  • I am interested in the "that will create a temporary entry" on your disk... Shouldn't map from 0 to size() do a 100% in-memory loading on a MappedByteBuffer? – Whimusical Feb 15 '15 at 12:54
  • That is unlikely for the simple reason that a zip entry is sequential by definition; for `*Stream`s, that is a different story however. But mappings are, well, random access. – fge Feb 15 '15 at 13:03
  • Do you mean that the temporal file is needed BEFORE the complete loading or that it is needed after load operation? – Whimusical Feb 15 '15 at 13:25