使用SevenZipSharp将流解压缩为字符串 [英] Decompress Stream to String using SevenZipSharp

查看:312
本文介绍了使用SevenZipSharp将流解压缩为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用SevenZipSharp压缩字符串,并使用以下代码将C#控制台应用程序(我是C#的新手)拼凑在一起(其中的某些部分来自于SO上类似的问题).

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/45861659/3451115

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

感谢任何帮助,理想情况下,控制台将显示压缩的字符串,然后显示解压缩的字符串.

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

谢谢:)

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

推荐答案

压缩的结果是二进制数据-它不是字符串.如果您尝试将其读取为字符串,则只会看到垃圾.这是意料之中的-您不应该将其视为字符串.

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.

下一个问题是,您尝试从compStream读取两次,而不先倒带".您从流的结尾开始,这意味着没有数据可以解压缩.如果您仅添加:

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;

在创建提取器之前,您可能会发现它可以立即工作.您可能还需要先倒带decompStream,然后再读取它.因此,您将拥有如下代码:

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;

这篇关于使用SevenZipSharp将流解压缩为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆