欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#自定義字符串壓縮和解壓縮的方法

 更新時間:2015年04月01日 10:32:24   作者:lele  
這篇文章主要介紹了C#自定義字符串壓縮和解壓縮的方法,通過自定義C#字符串操作類實現(xiàn)對字符串的壓縮與解壓的功能,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了C#自定義字符串壓縮和解壓縮的方法。分享給大家供大家參考。具體如下:

class ZipLib
{
 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();
 }
}

代碼使用方法:

string str_org="aaaaaaaaaabbbbbbbbbbbbcccccccccdddddddd";
string str_comp = ZipLib.Zip(str_org);
Console.WriteLine("str_comp:" + str_comp);
string str_uncomp = ZipLib.UnZip(str_comp);
Console.WriteLine("str_uncomp:" + str_uncomp);
Console.ReadLine();

希望本文所述對大家的C#程序設(shè)計有所幫助。

相關(guān)文章

  • 淺談static a[n*m]={0};中static的作用

    淺談static a[n*m]={0};中static的作用

    下面小編就為大家?guī)硪黄獪\談static a[n*m]={0};中static的作用。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • c# Linq查詢詳解

    c# Linq查詢詳解

    這篇文章主要介紹了c# Linq查詢的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04
  • c# 二分查找算法

    c# 二分查找算法

    折半搜索,也稱二分查找算法、二分搜索,是一種在有序數(shù)組中查找某一特定元素的搜索算法
    2013-10-10
  • c#線程同步使用詳解示例

    c#線程同步使用詳解示例

    這篇文章主要介紹了c#線程同步使用方法,介紹幾種常用的C#進(jìn)行線程同步的方式,需要的朋友可以參考下
    2014-02-02
  • C#.NET中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫中

    C#.NET中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫中

    這篇文章主要給大家介紹C#.net中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫中,本文涉及到C#.net中批量插入數(shù)據(jù)到數(shù)據(jù)庫中方面的內(nèi)容,對C#.net批量插入數(shù)據(jù)到數(shù)據(jù)庫中感興趣的朋友可以參考下本篇文章
    2015-10-10
  • c#通過app.manifest使程序以管理員身份運(yùn)行

    c#通過app.manifest使程序以管理員身份運(yùn)行

    通常我們使用c#編寫的程序不會彈出這個提示,也就無法以管理員身分運(yùn)行。微軟的操作系統(tǒng)使用微軟的產(chǎn)品方法當(dāng)然是有的,通過app.manifest配置可以使程序打開的時候,彈出UAC提示需要得到允許才可以繼續(xù),這樣就獲得了管理員的權(quán)限來執(zhí)行程序
    2015-01-01
  • c# list部分操作實現(xiàn)代碼

    c# list部分操作實現(xiàn)代碼

    這篇文章主要介紹了c# list部分操作,需要的朋友可以參考下
    2013-09-09
  • C#實現(xiàn)數(shù)據(jù)導(dǎo)出任一Word圖表的通用呈現(xiàn)方法

    C#實現(xiàn)數(shù)據(jù)導(dǎo)出任一Word圖表的通用呈現(xiàn)方法

    應(yīng)人才測評產(chǎn)品的需求,導(dǎo)出測評報告是其中一個重要的環(huán)節(jié),報告的文件類型也多種多樣,其中WORD輸出也扮演了一個重要的角色,本文給大家介紹了C#實現(xiàn)數(shù)據(jù)導(dǎo)出任一Word圖表的通用呈現(xiàn)方法及一些體會,需要的朋友可以參考下
    2023-10-10
  • C#調(diào)用第三方工具完成FTP操作

    C#調(diào)用第三方工具完成FTP操作

    這篇文章介紹了C#調(diào)用第三方工具完成FTP操作的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • Unity5.6大規(guī)模地形資源創(chuàng)建方法

    Unity5.6大規(guī)模地形資源創(chuàng)建方法

    這篇文章主要為大家詳細(xì)介紹了Unity5.6大規(guī)模地形資源創(chuàng)建方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-02-02

最新評論