Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading files from an embedded ZIP archive

Tags:

java

zip

I have a ZIP archive that's embedded inside a larger file. I know the archive's starting offset within the larger file and its length.

Are there any Java libraries that would enable me to directly read the files contained within the archive? I am thinking along the lines of ZipFile.getInputStream(). Unfortunately, ZipFile doesn't work for this use case since its constructors require a standalone ZIP file.

For performance reasons, I cannot copy the ZIP achive into a separate file before opening it.

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

like image 363
NPE Avatar asked Mar 26 '12 12:03

NPE


People also ask

How do I view the contents of a zip file?

When you have a single file in the zip archive, you can use one of the following commands to read them: zcat, zless and zmore. These commands will not work if the zip archive contains more than one file. Use the zcat command to read the contents of the . zip file.

How can I read a zip file without unzipping it?

Using Vim. Vim command can also be used to view the contents of a ZIP archive without extracting it. It can work for both the archived files and folders. Along with ZIP, it can work with other extensions as well, such as tar.

How do I view the content of a zip file in Unix?

Lucky for you, the unzip command has the -l option that displays the contents of a zip file without extracting them. To view a ZIP file's contents, run the unzip command to list ( -l ) the zip file's ( newdir. zip ) contents without extracting them.

How do I open a zip file in Unix without unzipping it?

If you need to check the contents of a compressed text file on Linux, you don't have to uncompress it first. Instead, you can use a zcat or bzcat command to extract and display file contents while leaving the file intact. The "cat" in each command name tells you that the command's purpose is to display content.


3 Answers

I've come up with a quick hack (which needs to get sanitized here and there), but it reads the contents of files from a ZIP archive which is embedded inside a 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();
like image 196
Neet Avatar answered Nov 16 '22 01:11

Neet


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

  1. position it at the start of embedded zipfile: fis.skip(offset);

  2. open ZipInputStream(fis)

like image 38
Alexei Kaigorodov Avatar answered Nov 16 '22 00:11

Alexei Kaigorodov


I suggest using TrueZIP, it provides file system access to many kinds of archives. It worked well for me in the past.

like image 1
Sandro Avatar answered Nov 16 '22 02:11

Sandro