<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      壓縮文本、字節或者文件的壓縮輔助類-GZipHelper 歡迎收藏

      壓縮文本、字節或者文件的壓縮輔助類-GZipHelper 歡迎收藏

        下面為大家介紹一.NET下輔助公共類GZipHelper,該工具類主要作用是對文本、字符、文件等進行壓縮與解壓。該類主要使用命名空間:System.IO.Compression下的GZipStream類來實現。  此類表示 GZip 數據格式,它使用無損壓縮和解壓縮文件的行業標準算法。這種格式包括一個檢測數據損壞的循環冗余校驗值。GZip 數據格式使用的算法與 DeflateStream 類的算法相同,但它可以擴展以使用其他壓縮格式。這種格式可以通過不涉及專利使用權的方式輕松實現。gzip 的格式可以從 RFC 1952“GZIP file format specification 4.3(GZIP 文件格式規范 4.3)GZIP file format specification 4.3(GZIP 文件格式規范 4.3)”中獲得。此類不能用于壓縮大于 4 GB 的文件。

        一、屬性

      BaseStream       獲取對基礎流的引用。 
      CanRead        獲取一個值,該值指示流是否支持在解壓縮文件的過程中讀取文件。 (重寫 Stream..::.CanRead。) 
      CanSeek        獲取一個值,該值指示流是否支持查找。 (重寫 Stream..::.CanSeek。) 
      CanTimeout       獲取一個值,該值確定當前流是否可以超時。 (繼承自 Stream。) 
      CanWrite         獲取一個值,該值指示流是否支持寫入。 (重寫 Stream..::.CanWrite。) 
      Length          不支持,并且總是引發 NotSupportedException。 (重寫 Stream..::.Length。) 
      Position         不支持,并且總是引發 NotSupportedException。 (重寫 Stream..::.Position。) 
      ReadTimeout       獲取或設置一個值(以毫秒為單位),該值確定流在超時前嘗試讀取多長時間。 (繼承自 Stream。) 
      WriteTimeout      獲取或設置一個值(以毫秒為單位),該值確定流在超時前嘗試寫入多長時間。 (繼承自 Stream。)

      二、方法

      BeginRead         開始異步讀操作。 (重寫 Stream..::.BeginRead(array<Byte>[]()[], Int32, Int32, AsyncCallback, Object)。) 
      BeginWrite        開始異步寫操作。 (重寫 Stream..::.BeginWrite(array<Byte>[]()[], Int32, Int32, AsyncCallback, Object)。) 
      Close           關閉當前流并釋放與之關聯的所有資源(如套接字和文件句柄)。 (繼承自 Stream。) 
      CreateObjRef       創建一個對象,該對象包含生成用于與遠程對象進行通信的代理所需的全部相關信息。 (繼承自 MarshalByRefObject。) 
      Dispose           已重載。 
      EndRead           等待掛起的異步讀取完成。 (重寫 Stream..::.EndRead(IAsyncResult)。) 
      EndWrite          處理異步寫入的結束。 (重寫 Stream..::.EndWrite(IAsyncResult)。) 
      Flush            將當前 GZipStream 對象的內部緩沖區的內容刷新到基礎流。 (重寫 Stream..::.Flush()()()。) 
      GetHashCode        用作特定類型的哈希函數。 (繼承自 Object。) 
      GetLifetimeService     檢索控制此實例的生存期策略的當前生存期服務對象。 (繼承自 MarshalByRefObject。) 
      InitializeLifetimeService  獲取控制此實例的生存期策略的生存期服務對象。 (繼承自 MarshalByRefObject。) 
      MemberwiseClone      已重載。 
      Read             將若干解壓縮的字節讀入指定的字節數組。 (重寫 Stream..::.Read(array<Byte>[]()[], Int32, Int32)。) 
      ReadByte          從流中讀取一個字節,并將流內的位置向前推進一個字節,或者如果已到達流的末尾,則返回 -1。 (繼承自 Stream。) 
      Seek             此屬性不受支持,并且總是引發 NotSupportedException。 (重寫 Stream..::.Seek(Int64, SeekOrigin)。) 
      SetLength         此屬性不受支持,并且總是引發 NotSupportedException。 (重寫 Stream..::.SetLength(Int64)。) 
      Write            從指定的字節數組中將壓縮的字節寫入基礎流。 (重寫 Stream..::.Write(array<Byte>[]()[], Int32, Int32)。) 
      WriteByte         將一個字節寫入流內的當前位置,并將流內的位置向前推進一個字節。 (繼承自 Stream。) 

      使用原生的方法進行壓縮解壓文件實例代碼:

         /// <summary>
          /// 壓縮文件
          /// </summary>
          /// <param name="fileName">文件名(全路徑)</param>
          /// <param name="data">需要壓縮的字符串</param>
          public void CompressFile(string fileName, string data)
          {        
              FileStream fstream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
              GZipStream gstream = new GZipStream(fstream, CompressionMode.Compress);
              StreamWriter swriter = new StreamWriter(gstream);
              swriter.Write(data);
              swriter.Close();
              gstream.Close();
              fstream.Close();
          }
      /// <summary> /// 解壓縮 /// </summary> /// <param name="fileName">文件名(全路徑)</param> /// <returns></returns> public string DecompressFile(string fileName) { string cstring=""; FileStream fstream = new FileStream(fileName, FileMode.Open, FileAccess.Read); GZipStream gstream = new GZipStream(fstream, CompressionMode.Decompress); StreamReader reader = new StreamReader(gstream); cstring=reader.ReadToEnd(); reader.Close(); gstream.Close(); fstream.Close(); return cstring; }

        GZipHelper公共類就是以GZipStream類為基礎做的對常用解壓縮進行的封裝。GZipHelper類圖如下所示:

       GZipHelper公共類完整源碼:

      using System;
      using System.IO;
      using System.IO.Compression;
      using System.Text;
      
      namespace RDIFramework.Utilities
      {
          /// <summary>
          /// 壓縮文本、字節或者文件的壓縮輔助類
          /// </summary>
          public class GZipHelper
          {
              /// <summary>
              /// 壓縮字符串
              /// </summary>
              /// <param name="text"></param>
              /// <returns></returns>
              public static string Compress(string text)
              {
                  // convert text to bytes
                  byte[] buffer = Encoding.UTF8.GetBytes(text);
                  // get a stream
                  MemoryStream ms = new MemoryStream();
                  // get ready to zip up our stream
                  using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
                  {
                      // compress the data into our buffer
                      zip.Write(buffer, 0, buffer.Length);
                  }
                  // reset our position in compressed stream to the start
                  ms.Position = 0;
                  // get the compressed data
                  byte[] compressed = ms.ToArray();
                  ms.Read(compressed, 0, compressed.Length);
                  // prepare final data with header that indicates length
                  byte[] gzBuffer = new byte[compressed.Length + 4];
                  //copy compressed data 4 bytes from start of final header
                  System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
                  // copy header to first 4 bytes
                  System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
                  // convert back to string and return
                  return Convert.ToBase64String(gzBuffer);
              }
      
              /// <summary>
              /// 解壓字符串
              /// </summary>
              /// <param name="compressedText"></param>
              /// <returns></returns>
              public static string Uncompress(string compressedText)
              {
                  // get string as bytes
                  byte[] gzBuffer = Convert.FromBase64String(compressedText);
                  // prepare stream to do uncompression
                  MemoryStream ms = new MemoryStream();
                  // get the length of compressed data
                  int msgLength = BitConverter.ToInt32(gzBuffer, 0);
                  // uncompress everything besides the header
                  ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
                  // prepare final buffer for just uncompressed data
                  byte[] buffer = new byte[msgLength];
                  // reset our position in stream since we're starting over
                  ms.Position = 0;
                  // unzip the data through stream
                  GZipStream zip = new GZipStream(ms, CompressionMode.Decompress);
                  // do the unzip
                  zip.Read(buffer, 0, buffer.Length);
                  // convert back to string and return
                  return Encoding.UTF8.GetString(buffer);
              }
      
              public static T GZip<T>(Stream stream, CompressionMode mode) where T : Stream
              {
                  byte[] writeData = new byte[4096];
                  T ms = default(T);
                  using (Stream sg = new GZipStream(stream, mode))
                  {
                      while (true)
                      {
                          Array.Clear(writeData, 0, writeData.Length);
                          int size = sg.Read(writeData, 0, writeData.Length);
                          if (size > 0)
                          {
                              ms.Write(writeData, 0, size);
                          }
                          else
                          {
                              break;
                          }
                      }
                      return ms;
                  }
              }
      
              /// <summary>
              /// 壓縮字節
              /// </summary>
              /// <param name="bytData"></param>
              /// <returns></returns>
              public static byte[] Compress(byte[] bytData)
              {
                  using (MemoryStream stream = GZip<MemoryStream>(new MemoryStream(bytData), CompressionMode.Compress))
                  {
                      return stream.ToArray();
                  }
              }
      
              /// <summary>
              /// 解壓字節
              /// </summary>
              /// <param name="bytData"></param>
              /// <returns></returns>
              public static byte[] Decompress(byte[] bytData)
              {
                  using (MemoryStream stream = GZip<MemoryStream>(new MemoryStream(bytData), CompressionMode.Decompress))
                  {
                      return stream.ToArray();
                  }
              }
      
              /// <summary>
              /// 壓縮文件
              /// </summary>
              /// <param name="sourceFile">源文件</param>
              /// <param name="destinationFile">目標文件</param>
              public static void CompressFile(string sourceFile, string destinationFile)
              {
                  if (File.Exists(sourceFile) == false) //判斷文件是否存在
                      throw new FileNotFoundException();
                  if (File.Exists(destinationFile) == false) //判斷目標文件文件是否存在
                      FileHelper.FileDel(destinationFile);
                  //創建文件流和字節數組
                  byte[] buffer = null;
                  FileStream sourceStream = null;
                  FileStream destinationStream = null;
                  GZipStream compressedStream = null;
                  try
                  {
                      sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                      buffer = new byte[sourceStream.Length];
                      //把文件流存放到字節數組中
                      int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);
                      if (checkCounter != buffer.Length)
                      {
                          throw new ApplicationException();
                      }
                      destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write);
                      //創建GzipStream實例,寫入壓縮的文件流
                      compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true);
                      compressedStream.Write(buffer, 0, buffer.Length);
                  }
                  finally
                  {
                      // Make sure we allways close all streams
                      if (sourceStream != null)
                      { sourceStream.Close(); }
                      if (compressedStream != null)
                      { compressedStream.Close(); }
                      if (destinationStream != null)
                      { destinationStream.Close(); }
                  }
              }
      
              /// <summary>
              /// 解壓文件
              /// </summary>
              /// <param name="sourceFile">源文件</param>
              /// <param name="destinationFile">目標文件</param>
              public static void DecompressFile(string sourceFile, string destinationFile)
              {
                  if (!File.Exists(sourceFile))
                  {
                      throw new FileNotFoundException();
                  }
                  FileStream stream = null;
                  FileStream stream2 = null;
                  GZipStream stream3 = null;
                  byte[] buffer = null;
                  try
                  {
                      stream = new FileStream(sourceFile, FileMode.Open);
                      stream3 = new GZipStream(stream, CompressionMode.Decompress, true);
                      buffer = new byte[4];
                      int num = ((int)stream.Length) - 4;
                      stream.Position = num;
                      stream.Read(buffer, 0, 4);
                      stream.Position = 0L;
                      byte[] buffer2 = new byte[BitConverter.ToInt32(buffer, 0) + 100];
                      int offset = 0;
                      int count = 0;
                      while (true)
                      {
                          int num5 = stream3.Read(buffer2, offset, 100);
                          if (num5 == 0)
                          {
                              break;
                          }
                          offset += num5;
                          count += num5;
                      }
                      stream2 = new FileStream(destinationFile, FileMode.Create);
                      stream2.Write(buffer2, 0, count);
                      stream2.Flush();
                  }
                  finally
                  {
                      if (stream != null)
                      {
                          stream.Close();
                      }
                      if (stream3 != null)
                      {
                          stream3.Close();
                      }
                      if (stream2 != null)
                      {
                          stream2.Close();
                      }
                  }
              }
          }
      }
      

         

      參考資料

      MSDN GZipStream 類介紹

       

      posted @ 2015-06-15 13:54  .NET快速開發框架  閱讀(1789)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 日本成熟少妇喷浆视频| 无码毛片一区二区本码视频| 亚洲av永久无码精品天堂久久| 可以在线观看的亚洲视频| 国产精品久久欧美久久一区| 骚虎视频在线观看| 色综合色国产热无码一| 日韩精品一区二区三区蜜臀| 国产精品一区二区国产馆| 韩国一级毛片中文字幕| 激情综合五月网| www插插插无码视频网站| 激情综合网五月激情五月| 确山县| 色吊丝二区三区中文写幕| 日本精品一区二区不卡| 二区中文字幕在线观看| 中文字幕无码专区一VA亚洲V专| 亚洲综合一区二区国产精品| 91色老久久精品偷偷蜜臀| 国产精品自在自线视频| 精品国产久一区二区三区| 国产第一页浮力影院入口| 中文国产成人精品久久不卡| 久久se精品一区精品二区国产 | 亚洲中文字幕一区二区| 国产精品男女爽免费视频| 丝袜高潮流白浆潮喷在线播放| 国产精品99中文字幕| 伊人久久久av老熟妇色| 亚洲人成精品久久久久| 亚洲人成网站观看在线观看| 人人色在线视频播放| 亚洲黄日本午夜一区二区| 亚洲日韩性欧美中文字幕| 亚洲成在人线AV品善网好看| 亚洲国内精品一区二区| 大丰市| 久热久热中文字幕综合激情| 中文字幕av国产精品| 日韩精品中文字幕有码|