Decompress Stream to String using SevenZipSharp

时间秒杀一切 提交于 2019-12-13 18:17:41

问题


I'd like to compress a string using SevenZipSharp and have cobbled together a C# console application (I'm new to C#) using the following code, (bits and pieces of which came from similar questions here on SO).

The compress part seems to work (albeit I'm passing in a file instead of a string), output of the compressed string to the console looks like gibberish but I'm stuck on the decompress...

I'm trying to do the same thing as here (I think):

  • https://stackoverflow.com/a/4305399/3451115

  • https://stackoverflow.com/a/45861659/3451115

  • https://stackoverflow.com/a/36331690/3451115

Appreciate any help, ideally the console will display the compressed string followed by the decompressed string.

Thanks :)

using System;
using System.IO;
using SevenZip;

namespace _7ZipWrapper
{
    public class Program
    {
        public static void Main()
        {
            SevenZipCompressor.SetLibraryPath(@"C:\Temp\7za64.dll");
            SevenZipCompressor compressor = new SevenZipCompressor();
            compressor.CompressionMethod = CompressionMethod.Ppmd;
            compressor.CompressionLevel = SevenZip.CompressionLevel.Ultra;
            compressor.ScanOnlyWritable = true;

            var compStream = new MemoryStream();
            var decompStream = new MemoryStream();
            compressor.CompressFiles(compStream, @"C:\Temp\a.txt");

            StreamReader readerC = new StreamReader(compStream);
            Console.WriteLine(readerC.ReadToEnd());
            Console.ReadKey();

            // works up to here... below here output to consol is: ""
            SevenZipExtractor extractor = new SevenZip.SevenZipExtractor(compStream);
            extractor.ExtractFile(0, decompStream);

            StreamReader readerD = new StreamReader(decompStream);
            Console.WriteLine(readerD.ReadToEnd());

            Console.ReadKey();
        }
    }
}

回答1:


The result of compression is binary data - it isn't a string. If you try to read it as a string, you'll just see garbage. That's to be expected - you shouldn't be treating it as a string.

The next problem is that you're trying to read from compStream twice, without "rewinding" it first. You're starting from the end of the stream, which means there's no data for it to decompress. If you just add:

compStream.Position = 0;

before you create the extractor, you may well find it works immediately. You may also need to rewind the decompStream before reading from it. So you'd have code like this:

 // Rewind to the start of the stream before decompressing
 compStream.Position = 0;
 SevenZipExtractor extractor = new SevenZip.SevenZipExtractor(compStream);
 extractor.ExtractFile(0, decompStream);

 // Rewind to the start of the decompressed stream before reading
 decompStream.Position = 0;


来源:https://stackoverflow.com/questions/50422316/decompress-stream-to-string-using-sevenzipsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!