小编dbo*_*oth的帖子

使用GZipStream对MemoryStream进行编程压缩/解压缩

我构建(基于CodeProject文章)一个包装类(C#)来使用a GZipStream来压缩a MemoryStream.它压缩很好但不会减压.我看了很多其他有相同问题的例子,我觉得我跟着说的是什么,但是当我解压缩时仍然没什么.这是压缩和解压缩方法:

public static byte[] Compress(byte[] bSource)
{

    using (MemoryStream ms = new MemoryStream())
    {
        using (GZipStream gzip = new GZipStream(ms, CompressionMode.Compress, true))
        {
            gzip.Write(bSource, 0, bSource.Length);
            gzip.Close();
        }

        return ms.ToArray();
    }
}


public static byte[] Decompress(byte[] bSource)
{

    try
    {
        using (MemoryStream ms = new MemoryStream())
        {
            using (GZipStream gzip = new GZipStream(ms, CompressionMode.Decompress, true))
            {
                gzip.Read(bSource, 0, bSource.Length);
                gzip.Close();
            }

            return ms.ToArray();
        }
    }
    catch (Exception ex)
    {
        throw new Exception("Error decompressing byte array", ex); …
Run Code Online (Sandbox Code Playgroud)

.net c# gzip c#-3.0 c#-4.0

4
推荐指数
1
解决办法
6139
查看次数

标签 统计

.net ×1

c# ×1

c#-3.0 ×1

c#-4.0 ×1

gzip ×1