使用Java解压缩7zip等文件

Jua*_*lme 5 java compression 7zip

我需要打开一个compresed文件(zml,我无法找到有关该扩展的信息),就像7zip用java做的那样.

我有一个zml文件,如果我用7zip打开它要求我输入密码,然后我输入密码并打开文件.

我需要对java做同样的事情,有人能给我一个建议吗?

最好的祝福.

胡安

Sha*_*P S 5

根据@trooper 评论,我能够提取受密码保护的 .7z 文件。试试下面的代码。您需要使用 7-Zip-JBinding ( http://sevenzipjbind.sourceforge.net/index.html )设置您的类路径。此代码是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();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)


T. *_*art 5

如果您正在寻找纯Java解决方案,则可以使用Apache Commons Compress,它也支持读取加密文件。