java - 在 java 中解压扩展名为 .7z 的文件

标签 java 7zip compression

有人可以建议(举例)任何适当且易于理解的方式来提取基于 InputStream 的一个或多个扩展名为 .7z 的文件。我已经检查了 XZ for java api,但没有成功。等待任何建议。

最佳答案

此代码可能对您有所帮助。

    import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;

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

public class unzip {
    public static void main(String[] args) {

        RandomAccessFile randomAccessFile = null;
        ISevenZipInArchive inArchive = null;

        try {
            randomAccessFile = new RandomAccessFile("oclHashcat-plus-0.14.7z", "r");
            inArchive = SevenZip.openInArchive(null, // autodetect archive type
                    new RandomAccessFileInStream(randomAccessFile));

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

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

            for (final 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 {

                            //Write to file
                            FileOutputStream fos;
                            try {
                                File file = new File(item.getPath());
                                file.getParentFile().mkdirs();
                                fos = new FileOutputStream(file);
                                fos.write(data);
                                fos.close();

                            } catch (FileNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                            hash[0] ^= Arrays.hashCode(data); // Consume data
                            sizeArray[0] += data.length;
                            return data.length; // Return amount of consumed data
                        }
                    });
                    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 (Exception e) {
            System.err.println("Error occurs: " + e);
            System.exit(1);
        } finally {
            if (inArchive != null) {
                try {
                    inArchive.close();
                } catch (SevenZipException e) {
                    System.err.println("Error closing archive: " + e);
                }
            }
            if (randomAccessFile != null) {
                try {
                    randomAccessFile.close();
                } catch (IOException e) {
                    System.err.println("Error closing file: " + e);
                }
            }
        }
    }
}

关于java - 在 java 中解压扩展名为 .7z 的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19403829/

相关文章:

java - ListView 所有元素在正确文本旁边显示 "false"文本?自定义适配器可能出现故障?

command-line - 使用 7zip 命令行实用程序解密加密的 7zip 存档文件

python - 我有大量相同性质的小文件。我可以在它们的基础上构建字典,但单独压缩每个文件吗?

java - 在Java中的单个节点上模拟多个节点?

contentPanel 内的 Java Swing 调整大小面板

c# - 解码 LZMA 流 c#

linux - 这是 7z 9.20 for linux 中的错误(密码中的 $ 字符)吗?

Java 算术编码 - 查找字符范围

node.js - NodeJS ssh2 fastGet 在解压时挂起

java - Guice:如何动态更新模块中的定义而不丢失预先存在的单例