1

I am using sevenzipsharp library: http://sevenzipsharp.codeplex.com/

I made a similar question in witch i was compressing and decompressing a stream at the "same time" using threads, witch i since have deleted due to realizing that, that cannot be done. However my question still stands. How do i decompress/extract a compressed stream, not to a file, but to another stream. I have searched the examples provided by the sevenzipsharp creators in : http://sevenzipsharp.codeplex.com/SourceControl/latest#SevenZipTest/Program.cs , sadly i have found no valid example to what i am trying to achieve.

I have compressed the stream with this method:

            SevenZip.SevenZipCompressor compressor = new SevenZip.SevenZipCompressor();
            compressor.CompressionMethod = SevenZip.CompressionMethod.Lzma2;
            compressor.CompressionLevel = SevenZip.CompressionLevel.Ultra;
            compressor.CompressStream(stream,output_compressed);

I tried with this:

            using (var tmp = new SevenZipExtractor(compressed))
            {
                tmp.ExtractFile(1, File.Create(@"D:\lel.txt"));
            }
meme
  • 597
  • 1
  • 10
  • 23

1 Answers1

2

I found the solution while i was writing this question, so i will answer it myself for other people that might come across this problem.

The 'ExtractFile' is expecting 2 parameters, number 1 is the index of the file inside the archive, number 2 is the output stream or file.,

but, when declaring SevenZipExtractor you provide it with not an archive but a compressed stream that is already in memory like i did, and there is nothing but just one file, the 1st parameter(index) must be 0.

The final code should look like this:

            using (var tmp = new SevenZipExtractor((stream_to_compress)))
            {
                tmp.ExtractFile(0,output_stream ));
            }
meme
  • 597
  • 1
  • 10
  • 23
  • I had found the same solution myself, but the extraction failed. When I saw this solution, I knew that mine was right, too, and the failure must be something else. Finally, I found that in my test I compressed with password and tried to extract without one. ;-) – Tobias Knauss Nov 26 '21 at 12:41