C#xml的壓縮與解壓還原(使用系統自帶的壓縮與解壓)(源碼分享)


在網上搜索了很多關於xml的壓縮與解壓的問題,解決方案比較多的是采用開源或者別的組件來實現xml的壓縮與解壓的,但卻找不到系統自身的最簡單的實現方式。
其實原理很簡單,把xml轉成string,然后對string進行壓縮。解壓就是其逆向的過程。

功能不復雜,下面不多說,直接代碼了: 

using System;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace 努力偷懶.Commonds
{
     ///   <summary>
    
///  使用系統默認壓縮流的方法進行壓縮並保存成文件,
    
///  約定1:文件前面8個字節保存的是long類型,存儲的是字符串壓縮前的字節長度。
    
///   </summary>
     public  class ZipFileHelper
    {
         public  static  long WriteString( string fileName,  string data)
        {
             long lResult =  0;
             byte[] bs = Encoding.UTF8.GetBytes(data);
            MemoryStream ms =  new MemoryStream();
             // 創建文件流
            FileStream fs =  new FileStream(fileName, FileMode.Create);
             try
            {
                DeflateStream compressedzipStream =  null;
                 try
                {
                    compressedzipStream =  new DeflateStream(ms, CompressionMode.Compress,  true);
                    compressedzipStream.Write(bs,  0, bs.Length); // 把bs中的數據進行二進制壓縮后寫入到ms中。
                    lResult = ms.Length;
                }
                 finally
                {
                     if ( null != compressedzipStream)
                        compressedzipStream.Close();
                }
                 byte[] bl = BitConverter.GetBytes(( long)bs.Length); // 把沒有壓縮的數據長度保存下來,以在還原時使用。
                fs.Write(bl,  0, bl.Length);
                ms.WriteTo(fs);
                fs.Flush();
            }
             finally
            {
                fs.Close();
                ms.Close();
            }
             return lResult;
        }
         ///   <summary>
        
///  解壓,返回byte數組
        
///   </summary>
        
///   <param name="fileName"></param>
        
///   <returns></returns>
         public  static  byte[] LoadBytes( string fileName)
        {
             // 源數據長度的byte數據
             byte[] bs;
             long nSize; // bs轉換后的實際值
            
// 結果
             byte[] decompressBuffer;
            FileStream fs =  new FileStream(fileName, FileMode.Open);
             try
            {
                 // 讀入源數據的字節長度
                 byte[] bl =  new  byte[ 8];
                fs.Read(bl,  08);
                nSize = BitConverter.ToInt64(bl,  0);
                 // 把文件流中字符的二進制數據寫入到bs數組中
                bs =  new  byte[fs.Length -  8];
                fs.Read(bs,  0, ( int)fs.Length -  8);
            }
             finally
            {
                fs.Close();
            }

             // bs數據寫入到ms流中
            MemoryStream ms =  new MemoryStream();
            DeflateStream DecompressedzipStream= null;
             try
            {
                ms.Write(bs,  0, bs.Length);
                ms.Position =  0;
                 // 解壓
                DecompressedzipStream =  new DeflateStream(ms, CompressionMode.Decompress);
                DecompressedzipStream.Flush();
                 // 解壓后的數據
                decompressBuffer =  new  byte[nSize];
                DecompressedzipStream.Read(decompressBuffer,  0, decompressBuffer.Length);
            }
             finally
            {
                 if ( null != DecompressedzipStream)
                    DecompressedzipStream.Close();
                 if ( null!=ms )
                    ms.Close();
            }
             return decompressBuffer;
        }
    }

 

原創作品出自努力偷懶,轉載請說明 文章出處 http://blog.csdn.net/kfarvid 或  http://www.cnblogs.com/kfarvid/ 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM