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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21897286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:19:13  来源:igfitidea点击:

how to extract files from a 7-zip stream in Java without store it on hard disk?

java7zipcompression

提问by user3330817

I want to extract some files from a 7-zip byte stream,it can't be stored on hard disk,so I can't use RandomAccessFile class,I have read sevenzipjbindingsource code,it also uncompresses the file with some closed source things like lib7-Zip-JBinding.sowhich wrote by other language.And the method of the official package SevenZip

我想从一个 7-zip 字节流中提取一些文件,它不能存储在硬盘上,所以我不能使用 RandomAccessFile 类,我已经阅读了Sevenzipjbinding源代码,它还用一些闭源的东西解压了文件像lib7-Zip-JBinding.so其中写道其他language.And官方包的方法SevenZip

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

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

can only uncompress a single file.

只能解压缩单个文件。

So how could I uncompress a 7-zip byte stream with pure Java?

那么我怎么能用纯 Java 解压缩一个 7-zip 字节流呢?

Any guys have solution?

有大佬有解决办法吗?

Sorry for my poor English and I'm waiting for your answers online.

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

回答by SANN3

Commons compress 1.6 and above has support for manipulating 7z format. Try it.

Commons compress 1.6 及以上版本支持操作 7z 格式。尝试一下。

Reference :

参考 :

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

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

Sample :

样本 :

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

回答by Pr0methean

Since 7z decompression requires random access, you'll have to read the whole stream into a byte[]and use new SevenZFile(new SeekableInMemoryByteChannel(bytes)). (If it's longer than Integer.MAX_VALUEbytes, you'll have to create a custom SeekableByteChannelimplementation.) Once the instance is constructed, the process is the same as in SANN3's answer.

由于 7z 解压需要随机访问,因此您必须将整个流读入 abyte[]并使用new SevenZFile(new SeekableInMemoryByteChannel(bytes)). (如果它比Integer.MAX_VALUE字节长,则必须创建自定义SeekableByteChannel实现。)构造实例后,该过程与 SANN3 的答案相同。

If it won't fit in memory andyou can't write it to a temporary file, then 7zip isn't a suitable compression algorithm given its need for random access.

如果它不适合内存并且您无法将其写入临时文件,那么 7zip 就不是一种合适的压缩算法,因为它需要随机访问。