C#解壓縮

來源:互聯網
上載者:User
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();

        }

    }

 

 

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.