Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decompressing a file like 7zip with Java

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

like image 976
Juan Enrique Riquelme Avatar asked Jun 29 '16 17:06

Juan Enrique Riquelme


People also ask

Does 7-Zip use Java?

The 7-Zip-JBinding consists of two parts: the java part and the native part. The java part introduces a cross-platform java interface of the 7-Zip library.

How do I unzip a Zip file in Java?

To unzip a zip file, we need to read the zip file with ZipInputStream and then read all the ZipEntry one by one. Then use FileOutputStream to write them to file system. We also need to create the output directory if it doesn't exists and any nested directories present in the zip file.

How can I zip a file in Java?

Creating a zip archive for a single file is very easy, we need to create a ZipOutputStream object from the FileOutputStream object of destination file. Then we add a new ZipEntry to the ZipOutputStream and use FileInputStream to read the source file to ZipOutputStream object.

How do I decompress a 7z file?

Right-click the file, point to the “7-Zip” submenu, and then click the “Open Archive” command. This opens 7-zip and displays the contents of the archive. From here, you can extract the contents to another location on your hard drive using the “Extract” button at the top.


2 Answers

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();
        }
    }

}
like image 166
Shankar P S Avatar answered Oct 11 '22 17:10

Shankar P S


If you look for a pure java solution, you can use Apache Commons Compress, which also supports reading encrypted files.

like image 38
T. Neidhart Avatar answered Oct 11 '22 19:10

T. Neidhart