Decompressing a file like 7zip with Java

Juan Enrique Riquelme picture Juan Enrique Riquelme · Jun 29, 2016 · Viewed 7.9k times · Source

I need to open a compresed file (zml, i don't couldn't find information about that extension) like 7zip does it with java.

I have a zml file, if I open it with 7zip it asks me for the password, then I put the password and can open the file.

I need to do the same with java, could anyone give me an advice for this?

Best regards.

Juan

Answer

Shankar P S picture Shankar P S · Jun 29, 2016

Based on @trooper comment, I was able to extract a .7z file that was password protected. Try the following code. You will need to setup your classpath with 7-Zip-JBinding (http://sevenzipjbind.sourceforge.net/index.html). This code is a modified version of code snippets found at http://sevenzipjbind.sourceforge.net/extraction_snippets.html

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.util.Arrays;

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

public class Extract {
    public static void main(String[] args) throws SevenZipException, FileNotFoundException {
        try {
            SevenZip.initSevenZipFromPlatformJAR();
            System.out.println("7-Zip-JBinding library was initialized");
            RandomAccessFile randomAccessFile = new RandomAccessFile("YOUR FILE NAME", "r");

            IInArchive inArchive = SevenZip.openInArchive(null, // Choose format
                                                                // automatically
                    new RandomAccessFileInStream(randomAccessFile));
            System.out.println(inArchive.getNumberOfItems());

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

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

            for (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 {
                            hash[0] ^= Arrays.hashCode(data); // Consume data
                            for (byte b : data) {
                                System.out.println((char) b);
                            }
                            sizeArray[0] += data.length;
                            return data.length; // Return amount of consumed
                                                // data
                        }
                    }, "YOUR PASSWORD HERE");

                    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 (SevenZipNativeInitializationException e) {
            e.printStackTrace();
        }
    }

}