Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 sevenzipjbinding source code,it also uncompresses the file with some closed source things like lib7-Zip-JBinding.so which wrote by other language.And the method of the official package SevenZip

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?

Any guys have solution?

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

like image 260
user3330817 Avatar asked Feb 20 '14 03:02

user3330817


2 Answers

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

Reference :

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();
like image 61
SANN3 Avatar answered Oct 19 '22 02:10

SANN3


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_VALUE bytes, you'll have to create a custom SeekableByteChannel implementation.) Once the instance is constructed, the process is the same as in SANN3's answer.

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

like image 21
Pr0methean Avatar answered Oct 19 '22 02:10

Pr0methean