Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read from a Winzip self-extracting (exe) zip file in Java?

Tags:

java

zip

Is there an existing method or will I need to manually parse and skip the exe block before passing the data to ZipInputStream?

like image 818
James Allman Avatar asked Oct 28 '11 03:10

James Allman


2 Answers

After reviewing the EXE file format and the ZIP file format and testing various options it appears the easiest solution is to just ignore any preamble up to the first zip local file header.

Zip file layout

Zip local file header

I wrote an input stream filter to bypass the preamble and it works perfectly:

ZipInputStream zis = new ZipInputStream(
    new WinZipInputStream(
    new FileInputStream("test.exe")));
while ((ze = zis.getNextEntry()) != null) {
    . . .
    zis.closeEntry();
}
zis.close();

WinZipInputStream.java

import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.IOException;

public class WinZipInputStream extends FilterInputStream {
    public static final byte[] ZIP_LOCAL = { 0x50, 0x4b, 0x03, 0x04 };
    protected int ip;
    protected int op;

    public WinZipInputStream(InputStream is) {
        super(is);
    }

    public int read() throws IOException {
        while(ip < ZIP_LOCAL.length) {
            int c = super.read();
            if (c == ZIP_LOCAL[ip]) {
                ip++;
            }
            else ip = 0;
        }

        if (op < ZIP_LOCAL.length)
            return ZIP_LOCAL[op++];
        else
            return super.read();
    }

    public int read(byte[] b, int off, int len) throws IOException {
        if (op == ZIP_LOCAL.length) return super.read(b, off, len);
        int l = 0;
        while (l < Math.min(len, ZIP_LOCAL.length)) {
            b[l++] = (byte)read();
        }
        return l;
    }
}
like image 132
James Allman Avatar answered Oct 11 '22 19:10

James Allman


The nice thing about ZIP files is their sequential structure: Every entry is a independent bunch of bytes, and at the end is a Central Directory Index that lists all entries and their offsets in the file.

The bad thing is, the java.util.zip.* classes ignore that index and just start reading into the file and expect the first entry to be a Local File Header block, which isn't the case for self-extracting ZIP archives (these start with the EXE part).

Some years ago, I wrote a custom ZIP parser to extract individual ZIP entries (LFH + data) that relied on the CDI to find where these entries where in the file. I just checked and it can actually list the entries of a self-extracing ZIP archive without further ado and give you the offsets -- so you could either:

  1. use that code to find the first LFH after the EXE part, and copy everything after that offset to a different File, then feed that new File to java.util.zip.ZipFile:

    Edit: Just skipping the EXE part doesn't seem to work, ZipFile still won't read it and my native ZIP program complains that the new ZIP file is damaged and exactly the number of bytes I skipped are given as "missing" (so it actually reads the CDI). I guess some headers would need to be rewritten, so the second approach given below looks more promising -- or

  2. use that code for the full ZIP extraction (it's similar to java.util.zip); this would require some additional plumbing because the code originally wasn't intended as replacement ZIP library but had a very specific use case (differential updating of ZIP files over HTTP)

The code is hosted at SourceForge (project page, website) and licensed under Apache License 2.0, so commercial use is fine -- AFAIK there's a commercial game using it as updater for their game assets.

The interesting parts to get the offsets from a ZIP file are in Indexer.parseZipFile which returns a LinkedHashMap<Resource, Long> (so the first map entry has the lowest offset in the file). Here's the code I used to list the entries of a self-extracting ZIP archive (created with the WinZIP SE creator with Wine on Ubuntu from an acra release file):

public static void main(String[] args) throws Exception {
    File archive = new File("/home/phil/downloads", "acra-4.2.3.exe");
    Map<Resource, Long> resources = parseZipFile(archive);
    for (Entry<Resource, Long> resource : resources.entrySet()) {
        System.out.println(resource.getKey() + ": " + resource.getValue());
    }
}

You can probably rip out most of the code except for the Indexer class and zip package that contains all the header parsing classes.

like image 45
Philipp Reichart Avatar answered Oct 11 '22 18:10

Philipp Reichart