Reading files from the built-in ZIP archive

I have a ZIP archive that is embedded in a larger file. I know that the initial shift of the archive in the large file and its length.

Are there Java libraries that will allow me to directly read the files contained in the archive? I am thinking along the lines of ZipFile.getInputStream() . Unfortunately, ZipFile does not work for this use case, as its designers require a standalone ZIP file.

For performance reasons, I cannot copy a ZIP file to a separate file before opening it.

edit: Just to be clear, I have random access to the file.

+7
source share
7 answers

I came up with a quick hack (which needs sanitation here and there), but it reads the contents of files from a ZIP archive that is built into TAR. It uses Java6, FileInputStream, ZipEntry and ZipInputStream. "Works on my local machine":

 final FileInputStream ins = new FileInputStream("archive.tar"); // Zip starts at 0x1f6400, size is not needed long toSkip = 0x1f6400; // Safe skipping while(toSkip > 0) toSkip -= ins.skip(toSkip); final ZipInputStream zipin = new ZipInputStream(ins); ZipEntry ze; while((ze = zipin.getNextEntry()) != null) { final byte[] content = new byte[(int)ze.getSize()]; int offset = 0; while(offset < content.length) { final int read = zipin.read(content, offset, content.length - offset); if(read == -1) break; offset += read; } // DEBUG: print out ZIP entry name and filesize System.out.println(ze + ": " + offset); } zipin.close(); 
+6
source

1.create FileInputStream fis = new FileInputStream (..);

  • place it at the beginning of the built-in zip file: fis.skip (offset);

  • open ZipInputStream (fis)

+1
source

I suggest using TrueZIP , it provides the file system with access to many types of archives. In the past, this worked well.

+1
source

If you use Java SE 7, it provides a zip fie system that allows you to directly read / write files to zip: http://docs.oracle.com/javase/7/docs/technotes/guides/io/fsp/zipfilesystemprovider. html

0
source

I think apache compression may help you.

There is an org.apache.commons.compress.archivers.zip.ZipArchiveEntry class that inherits java.util.zip.ZipEntry .

It has a getDataOffset() method, which can get the offset of the data stream in the archive.

0
source

7-zip-JavaBinding is the Java wrapper for the 7-zip C ++ Library.

The code snippets in particular have some nice examples, including printing a list of items in an archive, extracting a single file and opening a multi-line file, parts archives.

-one
source

Check if zip4j helps you.

You can try PartInputStream to read the zip file according to your use case.

I think it is better to create a temporary zip file and then get it.

-one
source

All Articles