使用Java解压缩7zip文件

6

我需要打开一个压缩文件(zml格式,我找不到有关该扩展名的信息),就像7zip在Java中所做的那样。

我有一个zml文件,如果我用7zip打开它,它会要求我输入密码,然后我输入密码就可以打开文件。

我需要在Java中实现相同的功能,请问有什么建议吗?

最好的问候。

Juan


2
嗨。我认为7zip的Java绑定可能是您正在寻找的:http://sevenzipjbind.sourceforge.net/index.html - trooper
谢谢,我会去看一下。 - Juan Enrique Riquelme
我在7zip的文件属性中发现该文件是一个zip文件,因此我使用了Zip4j。按照这篇文章的指示,我可以解压缩文件:https://dev59.com/NWgu5IYBdhLWcg3wpYnJ 谢谢大家的帮助。 - Juan Enrique Riquelme
3个回答

6
如果你正在寻找一个纯Java的解决方案,你可以使用Apache Commons Compress,它还支持读取加密文件。

6

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

}

0
以下函数将提取您的7zip文件并返回已提取文件的列表。
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;
}

你没有解释"SevenZFile"类来自哪里。这段代码不会在所有情况下都起作用。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接