好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C#xml的压缩与解压还原(使用系统自带的压缩与解压)(源码分享)

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,  0 ,  8 );
                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/  

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于C#xml的压缩与解压还原(使用系统自带的压缩与解压)(源码分享)的详细内容...

  阅读:55次