zoukankan      html  css  js  c++  java
  • gzip优化网络传输量提高传输效率[转]

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.IO.Compression;
    using System.Data;

    namespace Lis2013HISWSTest
    {
    public class ZipHelper
    {
    /// <summary>
    /// 解压
    /// </summary>
    /// <param name="Value"></param>
    /// <returns></returns>
    public static DataSet GetDatasetByString(string Value)
    {
    DataSet ds = new DataSet();
    string CC = GZipDecompressString(Value);
    System.IO.StringReader Sr = new StringReader(CC);
    ds.ReadXml(Sr);
    return ds;
    }
    /// <summary>
    /// 根据DATASET压缩字符串
    /// </summary>
    /// <param name="ds"></param>
    /// <returns></returns>
    public static string GetStringByDataset(string ds)
    {
    return GZipCompressString(ds);
    }
    /// <summary>
    /// 将传入字符串以GZip算法压缩后,返回Base64编码字符
    /// </summary>
    /// <param name="rawString">需要压缩的字符串</param>
    /// <returns>压缩后的Base64编码的字符串</returns>
    public static string GZipCompressString(string rawString)
    {
    if (string.IsNullOrEmpty(rawString) || rawString.Length == 0)
    {
    return "";
    }
    else
    {
    byte[] rawData = System.Text.Encoding.UTF8.GetBytes(rawString.ToString());
    byte[] zippedData = Compress(rawData);
    return (string)(Convert.ToBase64String(zippedData));
    }

    }
    /// <summary>
    /// GZip压缩
    /// </summary>
    /// <param name="rawData"></param>
    /// <returns></returns>
    public static byte[] Compress(byte[] rawData)
    {
    MemoryStream ms = new MemoryStream();
    GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress, true);
    compressedzipStream.Write(rawData, 0, rawData.Length);
    compressedzipStream.Close();
    return ms.ToArray();
    }
    /// <summary>
    /// 将传入的二进制字符串资料以GZip算法解压缩
    /// </summary>
    /// <param name="zippedString">经GZip压缩后的二进制字符串</param>
    /// <returns>原始未压缩字符串</returns>
    public static string GZipDecompressString(string zippedString)
    {
    if (string.IsNullOrEmpty(zippedString) || zippedString.Length == 0)
    {
    return "";
    }
    else
    {
    byte[] zippedData = Convert.FromBase64String(zippedString.ToString());
    return (string)(System.Text.Encoding.UTF8.GetString(Decompress(zippedData)));
    }
    }
    /// <summary>
    /// ZIP解压
    /// </summary>
    /// <param name="zippedData"></param>
    /// <returns></returns>
    public static byte[] Decompress(byte[] zippedData)
    {
    MemoryStream ms = new MemoryStream(zippedData);
    GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Decompress);
    MemoryStream outBuffer = new MemoryStream();
    byte[] block = new byte[1024];
    while (true)
    {
    int bytesRead = compressedzipStream.Read(block, 0, block.Length);
    if (bytesRead <= 0)
    break;
    else
    outBuffer.Write(block, 0, bytesRead);
    }
    compressedzipStream.Close();
    return outBuffer.ToArray();
    }
    }
    }

  • 相关阅读:
    一次与客户端合作的走坑之旅!
    ecplise打不开提示Eclipse中...No java virtual machine was found...
    eclipse配置tomcat,让java web项目运行起来!
    Tomcat v9.0 Could not publish to the server. java.lang.IndexOutOfBoundsException
    Certbot让网站拥有免费https证书
    Nginx访问权限配置
    hexo博客pure主题解决不蒜子计数不显示的问题
    Mono.Cecil 修改目标.NET的IL代码保存时报异常的处理。
    [转载]斐讯K2 A2版免TTL刷BREED不死Bootloader
    各种UserAgent的列表
  • 原文地址:https://www.cnblogs.com/fx2008/p/3779276.html
Copyright © 2011-2022 走看看