如何从Java中的7-zip流中提取文件而不将其存储在硬盘上?

use*_*817 12 java compression 7zip

我想从一个7-zip字节流中提取一些文件,它不能存储在硬盘上,所以我不能使用RandomAccessFile类,我已经阅读了sevenzipjbinding源代码,它还用一些封闭的源代码解压缩文件比如lib7-Zip-JBinding.so用其他语言写的.和官方包SevenZip的方法一样

SevenZip.Compression.LZMA.Decoder.Code(java.io.InputStream inStream,java.io.OutputStream outStream,long outSize,ICompressProgressInfo progress)

只能解压缩单个文件.

那么如何用纯Java解压缩7-zip字节流呢?

有人有解决方案吗?

抱歉我的英语不好,我在网上等你的答案.

SAN*_*NN3 11

Commons compress 1.6及以上版本支持操作7z格式.试试吧.

参考:

http://commons.apache.org/proper/commons-compress/index.html

样品:

    SevenZFile sevenZFile = new SevenZFile(new File("test-documents.7z"));
    SevenZArchiveEntry entry = sevenZFile.getNextEntry();
    while(entry!=null){
        System.out.println(entry.getName());
        FileOutputStream out = new FileOutputStream(entry.getName());
        byte[] content = new byte[(int) entry.getSize()];
        sevenZFile.read(content, 0, content.length);
        out.write(content);
        out.close();
        entry = sevenZFile.getNextEntry();
    }
    sevenZFile.close();
Run Code Online (Sandbox Code Playgroud)

  • 我在Android上尝试过Apache Commom-Compress,我想在这里分享我的经验.这个解决方案对我的目标来说是完全不可接受的,因为将单个文件从250 Mb解压缩到750 Mb需要3个小时(3个小时).看起来实现速度很慢而且没有优化.我的手机是Nexus 5. (3认同)
  • Commons Compress 库确实可以操作 7z 格式,但显然不能按照 OP 的要求从流中操作。 (2认同)