码迷,mamicode.com
首页 > 其他好文 > 详细

gzip

时间:2018-08-22 20:42:29      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:except   oba   returns   ret   字节数组   oar   int   compress   memory   

using System;
using System.IO;
using System.IO.Compression;
using System.Text;

public class gzip
{

    /// <summary>  
    /// 字节数组压缩  
    /// </summary>  
    /// <param name="strSource"></param>  
    /// <returns></returns>  
    public static byte[] Compress(byte[] data)
    {
        try
        {
            MemoryStream ms = new MemoryStream();
            GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
            zip.Write(data, 0, data.Length);
            zip.Close();
            byte[] buffer = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(buffer, 0, buffer.Length);
            ms.Close();
            return buffer;

        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
    }
    /// <summary>  
    /// 字节数组解压缩  
    /// </summary>  
    /// <param name="strSource"></param>  
    /// <returns></returns>  
    public static byte[] Decompress(byte[] data)
    {
        try
        {
            MemoryStream ms = new MemoryStream(data);
            GZipStream zip = new GZipStream(ms, CompressionMode.Decompress, true);
            MemoryStream msreader = new MemoryStream();
            byte[] buffer = new byte[0x1000];
            while (true)
            {
                int reader = zip.Read(buffer, 0, buffer.Length);
                if (reader <= 0)
                {
                    break;
                }
                msreader.Write(buffer, 0, reader);
            }
            zip.Close();
            ms.Close();
            msreader.Position = 0;
            buffer = msreader.ToArray();
            msreader.Close();
            return buffer;
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
    }

    /// <summary>
    /// 字符串压缩
    /// </summary>
    /// <returns>The string.</returns>
    /// <param name="str">String.</param>
    public static string CompressString(string str)
    {
        string compressString = "";
        byte[] compressBeforeByte = Encoding.UTF8.GetBytes(str);
        byte[] compressAfterByte = Compress(compressBeforeByte);
        //compressString = Encoding.GetEncoding("UTF-8").GetString(compressAfterByte);    
        compressString = Convert.ToBase64String(compressAfterByte);
        return compressString;
    }
    /// <summary>
    /// 字符串解压缩
    /// </summary>
    /// <returns>The string.</returns>
    /// <param name="str">String.</param>
    public static string DecompressString(string str)
    {
        string compressString = "";
        //byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(str);    
        byte[] compressBeforeByte = Convert.FromBase64String(str);
        byte[] compressAfterByte = Decompress(compressBeforeByte);
        compressString = Encoding.UTF8.GetString(compressAfterByte);
        return compressString;
    }
}

  

gzip

标签:except   oba   returns   ret   字节数组   oar   int   compress   memory   

原文地址:https://www.cnblogs.com/lsz3034/p/9519925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!