C#實(shí)現(xiàn)壓縮和解壓縮的方法示例【Gzip和Zip方式】
本文實(shí)例講述了C#實(shí)現(xiàn)壓縮和解壓縮的方法。分享給大家供大家參考,具體如下:
使用ICSharpCode.SharpZipLib.dll來壓縮/解壓(壓縮效率比GZip要高一點(diǎn))
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(); } }
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
- C#實(shí)現(xiàn)GZip壓縮和解壓縮入門實(shí)例
- C#實(shí)現(xiàn)rar壓縮與解壓縮文件的方法
- C#文件流進(jìn)行壓縮和解壓縮的方法
- C# 利用ICSharpCode.SharpZipLib實(shí)現(xiàn)在線壓縮和解壓縮
- asp.net C#實(shí)現(xiàn)解壓縮文件的方法
- C#實(shí)現(xiàn)文件壓縮與解壓的方法示例【ZIP格式】
- C#實(shí)現(xiàn)的文件壓縮和解壓縮類
- C#使用ICSharpCode.SharpZipLib.dll進(jìn)行文件的壓縮與解壓功能
- C#自定義字符串壓縮和解壓縮的方法
- C#壓縮或解壓rar、zip文件方法實(shí)例
相關(guān)文章
C# 多線程編程技術(shù)基礎(chǔ)知識(shí)入門
這篇文章主要介紹了C# 多線程編程技術(shù)基礎(chǔ)知識(shí),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2020-02-02C# 中類型轉(zhuǎn)換方式之顯式轉(zhuǎn)換和 as 運(yùn)算符
在 C# 中,有兩種常見的類型轉(zhuǎn)換方式:顯式轉(zhuǎn)換和as 運(yùn)算符,它們用于在不同類型之間進(jìn)行轉(zhuǎn)換,以下是對(duì)這兩種轉(zhuǎn)換方式的詳細(xì)解釋和示例說明,感興趣的朋友跟隨小編一起看看吧2024-05-05C#設(shè)計(jì)模式實(shí)現(xiàn)之生成器模式和責(zé)任鏈模式
學(xué)完設(shè)計(jì)模式之后,你就感覺它會(huì)慢慢地影響到你寫代碼的思維方式,下面這篇文章主要給大家介紹了關(guān)于C#設(shè)計(jì)模式實(shí)現(xiàn)之生成器模式和責(zé)任鏈模式的相關(guān)資料,需要的朋友可以參考下2021-08-08