字节数组中的压缩内容转换为字符串中的未压缩内容 [英] Compressed content in a byte array to uncompressed in a string

查看:131
本文介绍了字节数组中的压缩内容转换为字符串中的未压缩内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何创建一个简单的函数来从字节数组中提取压缩内容(使用Deflate方法压缩,ANSI编码),并以字符串形式表示?

我同意:

public string UnzipString3(byte[] byteArrayCompressedContent)
    {
        int compressedSize = byteArrayCompressedContent.Length;
        try
        {
            using (MemoryStream inputMemoryStream = new MemoryStream(byteArrayCompressedContent))
            {
                //I need to consume the first 2 bytes to be able read the stream
                //If I consume 0 or 4, I can't exploit it                        
                inputMemoryStream.Seek(2, SeekOrigin.Begin);
                using (DeflateStream deflateStream = new DeflateStream(inputMemoryStream, System.IO.Compression.CompressionMode.Decompress))
                {
                    //deflateStream.BaseStream.Position = 0;
                    using (StreamReader reader = new StreamReader(deflateStream, System.Text.Encoding.UTF8))
                    {
                        string uncompressedContent = reader.ReadToEnd();
                        reader.Close();
                        return uncompressedContent;
                    }
                }
            }
        }
        catch (Exception e)
        {
            return "";
        }
    }

如果愿意,我可以看到读取inputMemoryStream的压缩数据,但是 StreamReader.ReadToEnd()给出的uncompressedContent总是返回一个空字符串.根据MSDN( https ://msdn.microsoft.com/zh-CN/library/system.io.streamreader.readtoend(v = vs.110).aspx ),应该在读取且位置为已经在"Stream"的末尾(我对哪个流感到困惑),但是StreamReader没有位置,并且我无法更改DeflateStream的位置,因为它是没有位置和长度的流,等等.

I can see read the compressed data of inputMemoryStream if I want, but the uncompressedContent given by StreamReader.ReadToEnd() always return an empty string. According to MSDN (https://msdn.microsoft.com/en-us/library/system.io.streamreader.readtoend(v=vs.110).aspx), it is supposed to happen when it is read and the position is already at the end of the "Stream" (I get confused with which stream), but a StreamReader hasn't a position and I can't change the position of the DeflateStream, because it's a stream without a position and the length, etc.

在复制前将compressStream位置设置为0导致块长度与它的补码不匹配"错误在任何情况下.

Putting the compressStream position to 0 before to copy leads to a Block length does not match with its complement" error in anycase.

当我使用RaTruong的解决方案时:

When I use the solution of RaTruong :

public static string UnzipString3(byte[] byteArrayCompressedContent)
{
    using (var outputStream = new MemoryStream())
    {
        using (var compressStream = new MemoryStream(byteArrayCompressedContent))
        {
            using (var deflateStream = new DeflateStream(compressStream, CompressionMode.Decompress))
            {
                deflateStream.CopyTo(outputStream);
            }
        }
        return Encoding.UTF8.GetString(outputStream.ToArray());
    }
}

这会导致我一直遇到的难题:要么我喜欢这样,要么DeflateStream.CopyTo函数返回块长度与其补码不匹配"错误;或我先消耗了两个字节,实际上并没有带来任何错误,但是仍然不复制任何内容,然后返回空字符串 ...

It leads to the dilemma I always had : either I do like this and the DeflateStream.CopyTo function returns a "Block length does not match with its complement" error ; or I consume then two first bytes and it actually does not bring any error but still copy nothing and then returns an empty string...

如果我尝试解压缩File的FileStream而不是其字节数组,则会发生相同的情况. 我使用MSDN(

If I try to decompress a FileStream of a File instead of its array of bytes, the same situation happens. I use the decompress function given in MSDN (https://msdn.microsoft.com/en-us/library/system.io.compression.deflatestream(v=vs.110).aspx) :

public string ExtractContent()
    {
        try
        {
            FileInfo fileInfo = new FileInfo("22CC0001.23U");
            // Get the stream of the source file.
            using (FileStream inFile = fileInfo.OpenRead())
            {
                // Get original file extension, for example
                // "doc" from report.doc.gz.
                string curFile = fileInfo.FullName;
                string origName = curFile.Remove(curFile.Length -
                        fileInfo.Extension.Length);
                //Create the decompressed file.
                using (FileStream outFile = File.Create(origName))
                {
                    using (DeflateStream decompressDeflateStream = new DeflateStream(inFile,
                            CompressionMode.Decompress))
                    {
                        // Copy the decompression stream 
                        // into the output file.
                        decompressDeflateStream.CopyTo(outFile);

                        Console.WriteLine("Decompressed: {0}", fileInfo.Name);
                    }
                }
            }
        }
        catch (Exception e)
        {
            return "errorelole";
        }
        return "";
    }

相同的块长度与其补码不匹配"错误...

Same "Block length does not match with its complement" error...

有一件事是我文件的扩展名不是在文件末尾添加的".zip",而是另一扩展名(在这种情况下为.23U). 当我创建一个具有相同扩展名(.23U而不是那种情况下没有扩展名)的新文件时,会发生相同的问题.

One thing is that the extension of my file is not a ".zip" added at the end of a file, but another extension (.23U in that case). When I create a new file having the same extension (.23U instead of no extension in that case), the same problem occurs.

推荐答案

您的解压缩方法应如下所示.不确定在读取流之前先消耗掉前2个字节的想法.

Your unzip method should be something like below. Not sure where you got the idea of consuming the first 2 bytes before you can read the stream.

    public static string UnzipString3(byte[] byteArrayCompressedContent)
    {
        using (var outputStream = new MemoryStream())
        {
            using (var compressStream = new MemoryStream(byteArrayCompressedContent))
            {
                using (var deflateStream = new DeflateStream(compressStream, CompressionMode.Decompress))
                {
                    deflateStream.CopyTo(outputStream);
                }
            }

            return Encoding.UTF8.GetString(outputStream.ToArray());
        }
    }

这篇关于字节数组中的压缩内容转换为字符串中的未压缩内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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