zoukankan      html  css  js  c++  java
  • C#解压缩

    C#解压缩字符串(代码)的两种方法需要第一中是使用.NET本身的类库,第二种方法使用第三方组件 ICSharpCode.SharpZipLib.dll(参考:http://www.cnblogs.com/kevinxtq/articles/454814.html)
    代码详细列表:

    using System;

    using System.IO;

    using System.IO.Compression;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Text;

    using ICSharpCode.SharpZipLib.BZip2;

    using ICSharpCode.SharpZipLib.Checksums;

    using ICSharpCode.SharpZipLib.Zip;

    using ICSharpCode.SharpZipLib.GZip;

     

    namespace CompressClass

    {

        public static class CompressDeCompress

        {

            //压缩SharpZip

            public static string SharpZipCompress(string uncompressedString)

            {

                byte[] bytData = System.Text.Encoding.Unicode.GetBytes(uncompressedString);

                MemoryStream ms = new MemoryStream();

                Stream s = new BZip2OutputStream(ms);

     

                s.Write(bytData, 0, bytData.Length);

                s.Close();

                byte[] compressedData = (byte[])ms.ToArray();

                return System.Convert.ToBase64String(compressedData, 0, compressedData.Length);

            }

            //解压SharpZip

            public static string SharpZipDeCompress(string compressedString)

            {

                System.Text.StringBuilder uncompressedString = new System.Text.StringBuilder();

                int totalLength = 0;

                byte[] bytInput = System.Convert.FromBase64String(compressedString); ;

                byte[] writeData = new byte[4096];

                Stream s2 = new BZip2InputStream(new MemoryStream(bytInput));

                while (true)

                {

                    int size = s2.Read(writeData, 0, writeData.Length);

                    if (size > 0)

                    {

                        totalLength += size;

                        uncompressedString.Append(System.Text.Encoding.Unicode.GetString(writeData, 0, size));

                    }

                    else

                    {

                        break;

                    }

                }

                s2.Close();

                return uncompressedString.ToString();

            }

            //压缩Gzip

            public static string GzipCompress(string uncompressedString)

            {

                byte[] bytData = System.Text.Encoding.Unicode.GetBytes(uncompressedString);

                MemoryStream ms = new MemoryStream();

                Stream s = new GZipStream(ms, CompressionMode.Compress);

                s.Write(bytData, 0, bytData.Length);

                s.Close();

                byte[] compressedData = (byte[])ms.ToArray();

                return System.Convert.ToBase64String(compressedData, 0, compressedData.Length);

            }

            //解压Gzip

            public static string GzipDeCompress(string compressedString)

            {

                System.Text.StringBuilder uncompressedString = new System.Text.StringBuilder();

                int totalLength = 0;

                byte[] bytInput = System.Convert.FromBase64String(compressedString); ;

                byte[] writeData = new byte[4096];

                Stream s2 = new GZipStream(new MemoryStream(bytInput), CompressionMode.Decompress);

                while (true)

                {

                    int size = s2.Read(writeData, 0, writeData.Length);

                    if (size > 0)

                    {

                        totalLength += size;

                        uncompressedString.Append(System.Text.Encoding.Unicode.GetString(writeData, 0, size));

                    }

                    else

                    {

                        break;

                    }

                }

                s2.Close();

                return uncompressedString.ToString();

            }

        }

     

     

    }

  • 相关阅读:
    ExtJS4 带清除功能的文本框 triggerfield
    ExtJS 4 MVC 创建 Viewport
    Sql Server 查询重复记录
    oracle 备份数据
    sql server 日期模糊查询
    SQL Server 日期转换成字符串
    Kurento应用开发指南(以Kurento 5.0为模板) 之中的一个:简单介绍,安装与卸载
    magento getCarriers 分析
    用两个小样例来解释单例模式中的“双重锁定”
    POJ 3592--Instantaneous Transference【SCC缩点新建图 && SPFA求最长路 && 经典】
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1523428.html
Copyright © 2011-2022 走看看