C# dataset 压缩,文件压缩

using System;

using System.Collections.Generic;

using System.IO.Compression;

using System.Text;

using System.Data;

using System.IO;

using System.Runtime.Serialization;

using System.Runtime.Serialization.Formatters.Binary;

using KCSvrOpr.Encrypt;

using System.Xml;



namespace KCSvrOpr.DataFormat

{

   
/// <summary>

   
/// 功    能:对数据的基本操作

   
/// 作    者:BQONE

   
/// 创建日期:2007年04月17日

   
/// 更新作者:

   
/// 更新日期:

   
/// 添加功能:

   
/// 描    述:包括数据的压缩,对象与byte[]之间的转换

   
/// 版    权:HN KC

   
/// </summary>

    public class KCDataFormatter

    {

       
public KCDataFormatter() { }

       
/// <summary>

       
/// 将DataSet格式化成字节数组byte[]

       
/// </summary>

       
/// <param name="dsOriginal">DataSet对象</param>

       
/// <returns>字节数组</returns>

        public static byte[] GetBinaryFormatData(DataSet dsOriginal)

        {

           
byte[] binaryDataResult = null;

            MemoryStream memStream
= new MemoryStream();

            IFormatter brFormatter
= new BinaryFormatter();

            dsOriginal.RemotingFormat
= SerializationFormat.Binary;

            brFormatter.Serialize(memStream, dsOriginal);

            binaryDataResult
= memStream.ToArray();

            memStream.Close();

            memStream.Dispose();

           
return binaryDataResult;

        }



       
/// <summary>

       
/// 将DataSet格式化成字节数组byte[],并且已经经过压缩

       
/// </summary>

       
/// <param name="dsOriginal">DataSet对象</param>

       
/// <returns>字节数组</returns>

        public static byte[] GetBinaryFormatDataCompress(DataSet dsOriginal)

        {

           
byte[] binaryDataResult = null;

            MemoryStream memStream
= new MemoryStream();

            IFormatter brFormatter
= new BinaryFormatter();

            dsOriginal.RemotingFormat
= SerializationFormat.Binary;

            brFormatter.Serialize(memStream, dsOriginal);

            binaryDataResult
= memStream.ToArray();

            memStream.Close();

            memStream.Dispose();

           
return Compress(binaryDataResult);

        }



       
/// <summary>

       
/// 解压数据

       
/// </summary>

       
/// <param name="data"></param>

       
/// <returns></returns>

        public static byte[] Decompress(byte[] data)

        {

           
byte[] bData;

            MemoryStream ms
= new MemoryStream();

            ms.Write(data,
0, data.Length);

            ms.Position
= 0;

            GZipStream stream
= new GZipStream(ms, CompressionMode.Decompress, true);

           
byte[] buffer = new byte[1024];

            MemoryStream temp
= new MemoryStream();

           
int read = stream.Read(buffer, 0, buffer.Length);

           
while (read > 0)

            {

                temp.Write(buffer,
0, read);

                read
= stream.Read(buffer, 0, buffer.Length);

            }

           
//必须把stream流关闭才能返回ms流数据,不然数据会不完整

            stream.Close();

            stream.Dispose();

            ms.Close();

            ms.Dispose();

            bData
= temp.ToArray();

            temp.Close();

            temp.Dispose();

           
return bData;

        }



       
/// <summary>

       
/// 压缩数据

       
/// </summary>

       
/// <param name="data"></param>

       
/// <returns></returns>

        public static byte[] Compress(byte[] data)

        {

           
byte[] bData;

            MemoryStream ms
= new MemoryStream();

            GZipStream stream
= new GZipStream(ms, CompressionMode.Compress, true);

            stream.Write(data,
0, data.Length);

            stream.Close();

            stream.Dispose();

           
//必须把stream流关闭才能返回ms流数据,不然数据会不完整

           
//并且解压缩方法stream.Read(buffer, 0, buffer.Length)时会返回0

            bData = ms.ToArray();

            ms.Close();

            ms.Dispose();

           
return bData;

        }



       
/// <summary>

       
/// 将字节数组反序列化成DataSet对象

       
/// </summary>

       
/// <param name="binaryData">字节数组</param>

       
/// <returns>DataSet对象</returns>

        public static DataSet RetrieveDataSet(byte[] binaryData)

        {

            DataSet dsOriginal
= null;

            MemoryStream memStream
= new MemoryStream(binaryData);

            IFormatter brFormatter
= new BinaryFormatter();

            Object obj
= brFormatter.Deserialize(memStream);

            dsOriginal
= (DataSet)obj;

           
return dsOriginal;

        }



       
/// <summary>

       
/// 将字节数组反解压后序列化成DataSet对象

       
/// </summary>

       
/// <param name="binaryData">字节数组</param>

       
/// <returns>DataSet对象</returns>

        public static DataSet RetrieveDataSetDecompress(byte[] binaryData)

        {

            DataSet dsOriginal
= null;

            MemoryStream memStream
= new MemoryStream(Decompress(binaryData));

            IFormatter brFormatter
= new BinaryFormatter();

            Object obj
= brFormatter.Deserialize(memStream);

            dsOriginal
= (DataSet)obj;

           
return dsOriginal;

        }



       
/// <summary>

       
/// 将object格式化成字节数组byte[]

       
/// </summary>

       
/// <param name="dsOriginal">object对象</param>

       
/// <returns>字节数组</returns>

        public static byte[] GetBinaryFormatData(object dsOriginal)

        {

           
byte[] binaryDataResult = null;

            MemoryStream memStream
= new MemoryStream();

            IFormatter brFormatter
= new BinaryFormatter();

            brFormatter.Serialize(memStream, dsOriginal);

            binaryDataResult
= memStream.ToArray();

            memStream.Close();

            memStream.Dispose();

           
return binaryDataResult;

        }



       
/// <summary>

       
/// 将objec格式化成字节数组byte[],并压缩

       
/// </summary>

       
/// <param name="dsOriginal">object对象</param>

       
/// <returns>字节数组</returns>

        public static byte[] GetBinaryFormatDataCompress(object dsOriginal)

        {

           
byte[] binaryDataResult = null;

            MemoryStream memStream
= new MemoryStream();

            IFormatter brFormatter
= new BinaryFormatter();

            brFormatter.Serialize(memStream, dsOriginal);

            binaryDataResult
= memStream.ToArray();

            memStream.Close();

            memStream.Dispose();

           
return Compress(binaryDataResult);

        }



       
/// <summary>

       
/// 将字节数组反序列化成object对象

       
/// </summary>

       
/// <param name="binaryData">字节数组</param>

       
/// <returns>object对象</returns>

        public static object RetrieveObject(byte[] binaryData)

        {

            MemoryStream memStream
= new MemoryStream(binaryData);

            IFormatter brFormatter
= new BinaryFormatter();

            Object obj
= brFormatter.Deserialize(memStream);

           
return obj;

        }



       
/// <summary>

       
/// 将字节数组解压后反序列化成object对象

       
/// </summary>

       
/// <param name="binaryData">字节数组</param>

       
/// <returns>object对象</returns>

        public static object RetrieveObjectDecompress(byte[] binaryData)

        {

            MemoryStream memStream
= new MemoryStream(Decompress(binaryData));

            IFormatter brFormatter
= new BinaryFormatter();

            Object obj
= brFormatter.Deserialize(memStream);

           
return obj;

        }



       
/// <summary>

       
/// 解密配置文件并读入到xmldoc中

       
/// </summary>

        public static XmlNode DecryptConfigFile(string filePath)

        {

            FileStream fs
= new FileStream(filePath, FileMode.Open);

            XmlDocument m_XmlDoc
= new XmlDocument();

            BinaryFormatter formatter
= null;

           
try

            {

                formatter
= new BinaryFormatter();

               
// Deserialize the hashtable from the file and//   assign   the   reference   to   the   local   variable.

                m_XmlDoc.LoadXml(KCEncrypt.Decrypt((string)formatter.Deserialize(fs)));

               
return m_XmlDoc.DocumentElement;

            }

           
catch (SerializationException e)

            {

                Console.WriteLine(
"Failed   to   deserialize.   Reason:   " + e.Message);

               
throw;

            }

           
finally

            {

                fs.Close();

                fs
= null;

            }

        }



       
/// <summary>

       
/// 加密密钥后再对文件字符进行加密

       
/// </summary>

        public static void EncryptConfigFile(string filePath, string str)

        {

            FileStream fs
= new FileStream(filePath, FileMode.Create);

            BinaryFormatter formatter
= new BinaryFormatter();



           
try

            {

                formatter.Serialize(fs, KCEncrypt.Encrypt(str));

            }

           
catch (SerializationException e)

            {

                Console.WriteLine(
"Failed   to   serialize.   Reason:   " + e.Message);

               
throw;

            }

           
finally

            {

                fs.Close();

                fs
= null;

            }

        }

    }

}


------------------------------------------------------------------------------------------------------------------------------------


               

C# DataSet对象序列化并压缩、反序列化并压缩
2010-03-19 15:08

using System.Data;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace DataSetSerializerDeserialize
{
    class Program
    {
        /// <summary>
        /// 序列化DataSet对象并压缩
        /// </summary>
        /// <param name="ds"></param>
        static void DataSetSerializerCompression(DataSet ds)
        {
            IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象
           
            MemoryStream ms = new MemoryStream();//创建内存流对象

            formatter.Serialize(ms, ds);//把DataSet对象序列化到内存流

            byte[] buffer = ms.ToArray();//把内存流对象写入字节数组

            ms.Close();//关闭内存流对象

            ms.Dispose();//释放资源

            FileStream fs = File.Create("datasetCompression.dat");//创建文件

            GZipStream gzipStream = new GZipStream(fs, CompressionMode.Compress, true);//创建压缩对象

            gzipStream.Write(buffer, 0, buffer.Length);//把压缩后的数据写入文件


            gzipStream.Close();//关闭压缩流,这里要注意:一定要关闭,要不然解压缩的时候会出现小于4K的文件读取不到数据,大于4K的文件读取不完整           

            gzipStream.Dispose();//释放对象

            fs.Close();//关闭流

            fs.Dispose();//释放对象
        }

        /// <summary>
        /// 不压缩直接序列化DataSet
        /// </summary>
        /// <param name="ds"></param>
        static void DataSetSerializer(DataSet ds)
        {
            IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象

            FileStream fs = File.Create("dataset.dat");//创建文件

            formatter.Serialize(fs, ds);//把DataSet对象序列化到文件

            fs.Close();//关闭流

            fs.Dispose();//释放对象
        }

        static void Main(string[] args)
        {
            DataTable table = new DataTable("ParentTable");
           
            DataColumn column;
            DataRow row;

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "id";
            column.ReadOnly = true;
            column.Unique = true;
            table.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "ParentItem";
            column.AutoIncrement = false;
            column.Caption = "ParentItem";
            column.ReadOnly = false;
            column.Unique = false;
            table.Columns.Add(column);

            DataColumn[] PrimaryKeyColumns = new DataColumn[1];
            PrimaryKeyColumns[0] = table.Columns["id"];
            table.PrimaryKey = PrimaryKeyColumns;

            DataSet dataSet = new DataSet();
            dataSet.Tables.Add(table);

            for (int i = 0; i <= 100; i++)
            {
                row = table.NewRow();
                row["id"] = i;
                row["ParentItem"] = "ParentItem " + i;
                table.Rows.Add(row);
            }
            DataSetSerializer(dataSet);
            DataSetSerializerCompression(dataSet);
        }
    }
}

/// <summary>
/// 反序列化压缩的DataSet
/// </summary>
/// <param name="_filePath"></param>
/// <returns></returns>
static DataSet DataSetDeserializeDecompress(string _filePath)
{
    FileStream fs = File.OpenRead(_filePath);//打开文件

    fs.Position = 0;//设置文件流的位置

    GZipStream gzipStream = new GZipStream(fs, CompressionMode.Decompress);//创建解压对象

    byte[] buffer = new byte[4096];//定义数据缓冲

    int offset = 0;//定义读取位置

    MemoryStream ms = new MemoryStream();//定义内存流

    while ((offset = gzipStream.Read(buffer, 0, buffer.Length)) != 0)
    {
        ms.Write(buffer, 0, offset);//解压后的数据写入内存流
    }


    BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化DataSet对象

    ms.Position = 0;//设置内存流的位置

    DataSet ds;

    try
    {
        ds = (DataSet)sfFormatter.Deserialize(ms);//反序列化
    }
    catch
    {
        throw;
    }
    finally
    {
        ms.Close();//关闭内存流
        ms.Dispose();//释放资源
    }
    fs.Close();//关闭文件流
    fs.Dispose();//释放资源
    gzipStream.Close();//关闭解压缩流
    gzipStream.Dispose();//释放资源
    return ds;
}

/// <summary>
/// 反序列化未压缩的DataSet
/// </summary>
/// <param name="_filePath"></param>
/// <returns></returns>
static DataSet DataSetDeserialize(string _filePath)
{
    FileStream fs = File.OpenRead(_filePath);//打开文件

    fs.Position = 0;//设置文件流的位置

    BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化DataSet对象

    DataSet ds;

    try
    {
        ds = (DataSet)sfFormatter.Deserialize(fs);//反序列化
    }
    catch
    {
        throw;
    }
    finally
    {
        fs.Close();//关闭内存流
        fs.Dispose();//释放资源
    }
    fs.Close();//关闭文件流
    fs.Dispose();//释放资源
    return ds;
}

----------------------------------------------------------------------

C# DataSet对象解压缩及反序列化 收藏

昨天说了DataSet的序列化及压缩,今天把解压缩及反序列化的代码写一下:

 

  1. /// <summary>   
  2. /// 反序列化压缩的DataSet   
  3. /// </summary>   
  4. /// <param name="_filePath"></param>   
  5. /// <returns></returns>   
  6. static DataSet DataSetDeserializeDecompress(string _filePath)   
  7. {   
  8.     FileStream fs = File.OpenRead(_filePath);//打开文件   
  9.   
  10.     fs.Position = 0;//设置文件流的位置   
  11.   
  12.     GZipStream gzipStream = new GZipStream(fs, CompressionMode.Decompress);//创建解压对象   
  13.   
  14.     byte[] buffer = new byte[4096];//定义数据缓冲   
  15.   
  16.     int offset = 0;//定义读取位置   
  17.   
  18.     MemoryStream ms = new MemoryStream();//定义内存流   
  19.   
  20.     while ((offset = gzipStream.Read(buffer, 0, buffer.Length)) != 0)   
  21.     {   
  22.         ms.Write(buffer, 0, offset);//解压后的数据写入内存流   
  23.     }   
  24.   
  25.   
  26.     BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化DataSet对象   
  27.   
  28.     ms.Position = 0;//设置内存流的位置   
  29.   
  30.     DataSet ds;   
  31.   
  32.     try  
  33.     {   
  34.         ds = (DataSet)sfFormatter.Deserialize(ms);//反序列化   
  35.     }   
  36.     catch  
  37.     {   
  38.         throw;   
  39.     }   
  40.     finally  
  41.     {   
  42.         ms.Close();//关闭内存流   
  43.         ms.Dispose();//释放资源   
  44.     }   
  45.     fs.Close();//关闭文件流   
  46.     fs.Dispose();//释放资源   
  47.     gzipStream.Close();//关闭解压缩流   
  48.     gzipStream.Dispose();//释放资源   
  49.     return ds;   
  50. }   
  51.   
  52. /// <summary>   
  53. /// 反序列化未压缩的DataSet   
  54. /// </summary>   
  55. /// <param name="_filePath"></param>   
  56. /// <returns></returns>   
  57. static DataSet DataSetDeserialize(string _filePath)   
  58. {   
  59.     FileStream fs = File.OpenRead(_filePath);//打开文件   
  60.   
  61.     fs.Position = 0;//设置文件流的位置   
  62.   
  63.     BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化DataSet对象   
  64.   
  65.     DataSet ds;   
  66.   
  67.     try  
  68.     {   
  69.         ds = (DataSet)sfFormatter.Deserialize(fs);//反序列化   
  70.     }   
  71.     catch  
  72.     {   
  73.         throw;   
  74.     }   
  75.     finally  
  76.     {   
  77.         fs.Close();//关闭内存流   
  78.         fs.Dispose();//释放资源   
  79.     }   
  80.     fs.Close();//关闭文件流   
  81.     fs.Dispose();//释放资源   
  82.     return ds;   
  83. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值