Decompressing a file like 7zip with Java
Asked Answered
B

3

6

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

Bobolink answered 29/6, 2016 at 17:2 Comment(4)
hi. I think the 7zip java bindings are probably what you are looking for: sevenzipjbind.sourceforge.net/index.htmlBleacher
Thank you, i'm going to take a look.Bobolink
I have found with the 7zip's file properties that the file was a zip, so I use Zip4j. I could deceompress the file following the directions of this post: #11175351 Thank you all for your help.Bobolink
Possible duplicate of How to use LZMA SDK to compress/decompress in JavaPneumonectomy
M
6

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

}
Mender answered 29/6, 2016 at 21:30 Comment(0)
L
6

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

Lorica answered 30/6, 2016 at 18:43 Comment(0)
B
0

Below function will extract your 7zip file and return the list of files which are extracted.

public static List<String> unSevenZipFile(String zipFilePath, String unzipDestination, String attachmentPassword) {

    List<String> extractedFilesList=new ArrayList<String>();

    // Get 7zip file
    try (SevenZFile sevenZFile = new SevenZFile(new File(zipFilePath),attachmentPassword.toCharArray())) {

        SevenZArchiveEntry entry;
        while ((entry = sevenZFile.getNextEntry()) != null) {

            File file = new File(unzipDestination+ entry.getName());
            System.out.println("Un seven zipping - " + file);
            byte[] content = new byte[(int) entry.getSize()];
            sevenZFile.read(content);
            Files.write(file.toPath(), content);
            extractedFilesList.add(unzipDestination+ entry.getName());

        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return extractedFilesList;
}
Bemire answered 10/2, 2022 at 9:26 Comment(1)
You have not explained where the class "SevenZFile" comes from. This code will not work in 100% off all cases.Coahuila

© 2022 - 2024 — McMap. All rights reserved.