.NET GZipStream compress and decompress

前端 未结 4 1824
无人及你
无人及你 2020-12-08 22:55

What is wrong with this code below. I always get FALSE, meaning after compression, decompressed data does not match original value.

public static bool Test()         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 23:10

    Try this code:

    public static bool Test()
            {
                string sample = "This is a compression test of microsoft .net gzip compression method and decompression methods";
    
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
    
                byte[] data = encoding.GetBytes(sample);
                bool result = false;
    
                // Compress
                MemoryStream cmpStream = new MemoryStream();
    
                GZipStream hgs = new GZipStream(cmpStream, CompressionMode.Compress);
    
                hgs.Write(data, 0, data.Length);
    
                byte[] cmpData = cmpStream.ToArray();
    
                MemoryStream decomStream = new MemoryStream(cmpData);
    
                hgs = new GZipStream(decomStream, CompressionMode.Decompress);
                hgs.Read(data, 0, data.Length);
    
                string sampleOut = encoding.GetString(data);
    
                result = String.Equals(sample, sampleOut);
                return result;
            }
    

    The problem what that you were not using the ASCIIEncoder to get the string back for sampleData.

    EDIT: Here's a cleaned up version of the code to help with Closing/Disposing:

    public static bool Test()
            {
                string sample = "This is a compression test of microsoft .net gzip compression method and decompression methods";
    
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
    
                byte[] data = encoding.GetBytes(sample);
    
                // Compress.
                GZipStream hgs;
                byte[] cmpData;
    
                using(MemoryStream cmpStream = new MemoryStream())
                using(hgs = new GZipStream(cmpStream, CompressionMode.Compress))
                {
                    hgs.Write(data, 0, data.Length);
                    hgs.Close()
    
                    // Do this AFTER the stream is closed which sounds counter intuitive 
                    // but if you do it before the stream will not be flushed
                    // (even if you call flush which has a null implementation).
                    cmpData = cmpStream.ToArray();
                }  
    
                using(MemoryStream decomStream = new MemoryStream(cmpData))
                using(hgs = new GZipStream(decomStream, CompressionMode.Decompress))
                {
                    hgs.Read(data, 0, data.Length);
                }
    
                string sampleOut = encoding.GetString(data);
    
                bool result = String.Equals(sample, sampleOut);
                return result;
            }
    

提交回复
热议问题