簡體   English   中英

將字節數組中的壓縮內容解壓縮為字符串

[英]Compressed content in a byte array to uncompressed in a string

我們如何創建一個簡單的函數來從字節數組中獲取壓縮內容(使用 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/en-us/library/system.io.streamreader.readtoend(v=vs.110).aspx ),它應該在讀取和位置時發生已經在“流”的末尾(我對哪個流感到困惑),但是 StreamReader 沒有位置並且我無法更改 DeflateStream 的位置,因為它是一個沒有位置和長度的流,等。

在任何情況下,在復制之前將 compressStream 位置設置為 0 都會導致塊長度與其補碼不匹配

編輯:

當我使用 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 函數返回“塊長度與其補碼不匹配”錯誤,要么我消耗了兩個第一個字節,它實際上沒有帶來任何錯誤,但是仍然不復制任何內容,然后返回一個空字符串......

如果我嘗試解壓縮文件的 FileStream 而不是其字節數組,則會發生相同的情況。 我使用 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 "";
    }

同樣的“塊長度與其補碼不匹配”錯誤......

一件事是我的文件的擴展名不是在文件末尾添加的“.zip”,而是另一個擴展名(在這種情況下為 .23U)。 當我創建一個具有相同擴展名(.23U 而不是在這種情況下沒有擴展名)的新文件時,會出現同樣的問題。

您的解壓縮方法應如下所示。 不確定在讀取流之前您從哪里想到消耗前 2 個字節的想法。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM