如题我要压缩一个很大的字符串

解决方案 »

  1.   

    gzip,参考以下文章
    http://msdn2.microsoft.com/zh-cn/library/system.io.compression.gzipstream(VS.80).aspx
      

  2.   

     public static string CompressString(string unCompressedString)
            {
                byte[] bytData = System.Text.Encoding.UTF8.GetBytes(unCompressedString);
                MemoryStream ms = new MemoryStream();
    Stream s = null;
    try
    {
    s = new GZipStream(ms, CompressionMode.Compress);
    int totalRead = 0;
    int bufLen = 4096;
    int thisRead = 0;
    while (true)
    {
    if (totalRead < bytData.Length)
    {
    thisRead = bytData.Length - totalRead;
    if (thisRead > bufLen)
    thisRead = bufLen;
    s.Write(bytData, totalRead, thisRead);
    totalRead = totalRead + thisRead;
    }
    else
    break;
    }
    }
    catch (Exception e)
    {
    LogUtil.LogError("Compress String Error:" + e.Message);
    }
    finally
    {
    if (s != null)
    {
    s.Close();
    s.Dispose();
                        s = null;
    }
    }
                byte[] compressedData = (byte[])ms.ToArray();
    ms.Close();
    ms.Dispose();
                return System.Convert.ToBase64String(compressedData, 0, compressedData.Length);        }