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/ 

 

转载于:https://www.cnblogs.com/kfarvid/archive/2012/02/21/2361720.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值