欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现压缩和解压缩的方法示例【Gzip和Zip方式】

程序员文章站 2023-12-10 08:32:40
本文实例讲述了c#实现压缩和解压缩的方法。分享给大家供大家参考,具体如下: 使用icsharpcode.sharpziplib.dll来压缩/解压(压缩效率比gzip要高...

本文实例讲述了c#实现压缩和解压缩的方法。分享给大家供大家参考,具体如下:

使用icsharpcode.sharpziplib.dll来压缩/解压(压缩效率比gzip要高一点)

public static class ziputil
{
    /// <summary>
    /// 压缩
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public static string compress(string param)
    {
      byte[] data = system.text.encoding.utf8.getbytes(param);
      //byte[] data = convert.frombase64string(param);
      memorystream ms = new memorystream();
      stream stream = new icsharpcode.sharpziplib.bzip2.bzip2outputstream(ms);
      try
      {
        stream.write(data, 0, data.length);
      }
      finally
      {
        stream.close();
        ms.close();
      }
      return convert.tobase64string(ms.toarray());
    }
    /// <summary>
    /// 解压
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public static string decompress(string param)
    {
      string commonstring = "";
      byte[] buffer = convert.frombase64string(param);
      memorystream ms = new memorystream(buffer);
      stream sm = new icsharpcode.sharpziplib.bzip2.bzip2inputstream(ms);
      //这里要指明要读入的格式,要不就有乱码
      streamreader reader = new streamreader(sm, system.text.encoding.utf8);
      try
      {
        commonstring = reader.readtoend();
      }
      finally
      {
        sm.close();
        ms.close();
      }
      return commonstring;
    }
}

使用gzip来压缩/解压缩(字符串)

public static class gziputil
{
    public static string zip(string value)
    {
      //transform string into byte[]
      byte[] bytearray = new byte[value.length];
      int indexba = 0;
      foreach (char item in value.tochararray())
      {
        bytearray[indexba++] = (byte)item;
      }
      //prepare for compress
      system.io.memorystream ms = new system.io.memorystream();
      system.io.compression.gzipstream sw = new system.io.compression.gzipstream(ms,
        system.io.compression.compressionmode.compress);
      //compress
      sw.write(bytearray, 0, bytearray.length);
      //close, do not flush cause bytes will go missing...
      sw.close();
      //transform byte[] zip data to string
      bytearray = ms.toarray();
      system.text.stringbuilder sb = new system.text.stringbuilder(bytearray.length);
      foreach (byte item in bytearray)
      {
        sb.append((char)item);
      }
      ms.close();
      sw.dispose();
      ms.dispose();
      return sb.tostring();
    }
    public static string unzip(string value)
    {
      //transform string into byte[]
      byte[] bytearray = new byte[value.length];
      int indexba = 0;
      foreach (char item in value.tochararray())
      {
        bytearray[indexba++] = (byte)item;
      }
      //prepare for decompress
      system.io.memorystream ms = new system.io.memorystream(bytearray);
      system.io.compression.gzipstream sr = new system.io.compression.gzipstream(ms,
        system.io.compression.compressionmode.decompress);
      //reset variable to collect uncompressed result
      bytearray = new byte[bytearray.length];
      //decompress
      int rbyte = sr.read(bytearray, 0, bytearray.length);
      //transform byte[] unzip data to string
      system.text.stringbuilder sb = new system.text.stringbuilder(rbyte);
      //read the number of bytes gzipstream red and do not a for each bytes in
      //resultbytearray;
      for (int i = 0; i < rbyte; i++)
      {
        sb.append((char)bytearray[i]);
      }
      sr.close();
      ms.close();
      sr.dispose();
      ms.dispose();
      return sb.tostring();
    }
}

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结

希望本文所述对大家c#程序设计有所帮助。