Java – 7zip unzip example

Used one of the examples in the 7zip Java bindings documentation to make this (I just modified it a little bit to actually write the unzipped data to the correct files as well, instead of just listing them). My wife needed this example so it was just something quick we threw together for a little experiment, but I thought I could put it here in case someone finds it and needs it.

Example output

   Hash   |    Size    | Filename
----------+------------+---------
 CC27EAB9 |     199872 | oclHashcat-plus-0.14/cudaHashcat-plus32.bin
 B661FF80 |     202248 | oclHashcat-plus-0.14/cudaHashcat-plus64.bin
  BCFEDE0 |     224352 | oclHashcat-plus-0.14/oclHashcat-plus32.bin
 39C89963 |     218472 | oclHashcat-plus-0.14/oclHashcat-plus64.bin
 500CE011 |     222568 | oclHashcat-plus-0.14/vclHashcat-plus64.bin

Code

package unzip;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;

import net.sf.sevenzipjbinding.ExtractOperationResult;
import net.sf.sevenzipjbinding.ISequentialOutStream;
import net.sf.sevenzipjbinding.ISevenZipInArchive;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.SevenZipException;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
import net.sf.sevenzipjbinding.simple.ISimpleInArchive;
import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;

public class unzip {
	public static void main(String[] args) {

        RandomAccessFile randomAccessFile = null;
        ISevenZipInArchive inArchive = null;
        
        try {
            randomAccessFile = new RandomAccessFile("oclHashcat-plus-0.14.7z", "r");
            inArchive = SevenZip.openInArchive(null, // autodetect archive type
                    new RandomAccessFileInStream(randomAccessFile));

            // Getting simple interface of the archive inArchive
            ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();

            System.out.println("   Hash   |    Size    | Filename");
            System.out.println("----------+------------+---------");

            for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
                final int[] hash = new int[] { 0 };
                if (!item.isFolder()) {
                    ExtractOperationResult result;

                    final long[] sizeArray = new long[1];
                    result = item.extractSlow(new ISequentialOutStream() {
                        public int write(byte[] data) throws SevenZipException {
                        	
                        	//Write to file
                        	FileOutputStream fos;
							try {
								File file = new File(item.getPath());
	                        	file.getParentFile().mkdirs();
								fos = new FileOutputStream(file);
								fos.write(data);
	                        	fos.close();
	                        	
							} catch (FileNotFoundException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}

                            hash[0] ^= Arrays.hashCode(data); // Consume data
                            sizeArray[0] += data.length;
                            return data.length; // Return amount of consumed data
                        }
                    });
                    if (result == ExtractOperationResult.OK) {
                        System.out.println(String.format("%9X | %10s | %s", // 
                                hash[0], sizeArray[0], item.getPath()));
                    } else {
                        System.err.println("Error extracting item: " + result);
                    }
                }
            }
        } catch (Exception e) {
            System.err.println("Error occurs: " + e);
            System.exit(1);
        } finally {
            if (inArchive != null) {
                try {
                    inArchive.close();
                } catch (SevenZipException e) {
                    System.err.println("Error closing archive: " + e);
                }
            }
            if (randomAccessFile != null) {
                try {
                    randomAccessFile.close();
                } catch (IOException e) {
                    System.err.println("Error closing file: " + e);
                }
            }
        }
	}
}

2 thoughts on “Java – 7zip unzip example

  1. if you call item.extractSlow() with a password, new ISequentialOutStream().writw() will be called with data blocks of 32kB. In this case write() overrides the file each time and as a result only the last part is written into the file.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.