XML压缩解压

ggxboy 2010-06-02 04:07:57
跪求XML压缩解压源代码 就是一个普通的XML.
...全文
367 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wlfengku 2011-09-05
  • 打赏
  • 举报
回复
过来看看
wuyq11 2010-06-02
  • 打赏
  • 举报
回复
XML文件序列化
using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath))
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type);
object obj = xs.Deserialize(reader);
reader.Close();
return obj;
}
转化为字符串使用SharpZipLib等压缩
皇城龙三 2010-06-02
  • 打赏
  • 举报
回复
浅谈C# XML WebServer数据序列化及数据压缩


研究一些有关 XML WebServer下数据序列化及数据压缩的实现问题,现与大家分享一下,也希望大家勇于讨论,互相学习。由于要实现网络传输入,考虑到操作系统、网络性能等几方面的原因,就需要对数据实现序列化和数据压缩传输入。本人简单地实验了一下,如果在XML WebServer下直接使用DataSet传输入数据,就算实现压缩,也是非常具大的。最好的方法是自己定义好结构体或都类对象(当然在定义的时间要声明我可序列化的哟),进行序列化,然后再进行数据压缩;到了另外一端时选进行解压缩,然后再反序列化。通过这样的实现,从我的项目经验得到的结论是,大大地提高了数据传输的性能,尤其是对于数据量具大的数据传输入更是如此。本人已把数据对象序列化和反序列化、压缩和解压方法(引用的是开源插件,可以从网上http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx 下载)封装成一个完整的类中了,代码如文中所述,如有不懂不解之处希望各位一起讨论,也希望能听到更好的见解。

view plaincopy to clipboardprint?
/**//*********************************************************************************************
* 文 件 名:ObjectBinaryFormate.cs
* 功能说明:实现对象序列化与反序列
* 版 本:1.0
* 更新说明:---------------------------------------------------------------------------------
* 修 改 人:王峰
* 日 期:2007-01-24
* 修改说明:实现数据的压缩
* -------------------------------------------------------------------------------------------
* 其它说明:无
***********************************************************************************************/
using System;
using System.IO;
using System.Data;
using System.Runtime.Serialization.Formatters.Binary;
//此处需要进相关的网站下载相应的开源插件
using ICSharpCode.SharpZipLib.Zip.Compression;

namespace SmsClientApp
...{
/**//// <summary>
/// ObjectBinaryFormate 的摘要说明。
/// </summary>
public class ObjectBinaryFormate
...{
public ObjectBinaryFormate()
...{
}

/**//**********************************************************************************************
* 方法名称:ChangeObjectToBytes
* 功能说明:把数据对象序列化为字节型数组
* 输 入 值:数据对象
* 输 出 值:无
* 返 回 值:字节数组
* 其它说明:无
**********************************************************************************************/
public static byte[] ChangeObjectToBytes(object objValue)
...{
byte[] dataValue = null;
try
...{
//序列化
BinaryFormatter formate = new BinaryFormatter();
//内存文件流对象
MemoryStream smsStream = new MemoryStream();
formate.Serialize(smsStream, objValue);
dataValue = smsStream.ToArray();
smsStream.Close();
}
catch(Exception e)
...{
System.Console.WriteLine("序列化失败!"+e.Message);
}

//返回压缩后的数据
return CompressByteData(dataValue);
}


/**//**********************************************************************************************
* 方法名称:ChangeBytesToObject
* 功能说明:把字节型数组反序列为数据对象
* 输 入 值:字节数组
* 输 出 值:无
* 返 回 值:数据对象
* 其它说明:无
**********************************************************************************************/
public static object ChangeBytesToObject(byte[] dataValue)
...{
object objValue = null;
try
...{
//解压缩数据
byte[] resultValue = DecompressByteData(dataValue);

//反序列化
BinaryFormatter formate = new BinaryFormatter();
//内存文件流对象
MemoryStream smsStream = new MemoryStream();
smsStream.Write(resultValue, 0, resultValue.Length);
//指针归零
smsStream.Seek(0, SeekOrigin.Begin);
objValue = (object)formate.Deserialize(smsStream);
smsStream.Close();

}
catch(Exception e)
...{
System.Console.WriteLine("反序列化失败!"+e.Message);
}

//返回对象
return objValue;
}


/**//**********************************************************************************************
* 方法名称:CompressByteData
* 功能说明:数据压缩
* 输 入 值:源字节数据
* 输 出 值:无
* 返 回 值:压缩后的字节数据
* 其它说明:无
**********************************************************************************************/
private static byte[] CompressByteData(Byte[] dataValue)
...{
byte[] resultValue = null;
try
...{
//压缩数据
Deflater compressFile = new Deflater(Deflater.BEST_COMPRESSION);
compressFile.SetInput(dataValue);
compressFile.Finish();
//内存文件流对象
MemoryStream smsStream = new MemoryStream();
byte[] bufData = new byte[1024];
while (!compressFile.IsFinished)
...{
int bufLength = compressFile.Deflate(bufData);
smsStream.Write(bufData, 0, bufLength);
}
resultValue = smsStream.ToArray();
smsStream.Close();

}
catch(Exception e)
...{
System.Console.WriteLine("压缩数据失败!"+e.Message);
}

//返回压缩后的数据
return resultValue;
}


/**//**********************************************************************************************
* 方法名称:DecompressByteData
* 功能说明:解压缩数据
* 输 入 值:源字节数据
* 输 出 值:无
* 返 回 值:解压后数据
* 其它说明:无
**********************************************************************************************/
private static byte[] DecompressByteData(byte[] dataValue)
...{
byte[] resultValue = null;
try
...{
//解压缩数据
Inflater decompressFile = new Inflater();
decompressFile.SetInput(dataValue);
//内存文件流对象
MemoryStream smsStream = new MemoryStream();
byte[] bufData = new byte[1024];
while (!decompressFile.IsFinished)
...{
int bufLength = decompressFile.Inflate(bufData);
smsStream.Write(bufData, 0, bufLength);
}
resultValue = smsStream.ToArray();
smsStream.Close();
}
catch(Exception e)
...{
System.Console.WriteLine("解压缩数据失败!"+e.Message);
}

//返回数据
return resultValue;
}
}
}



本文来自:http://blog.csdn.net/kenkao/archive/2009/07/13/4345369.aspx
gxingmin 2010-06-02
  • 打赏
  • 举报
回复
你压缩成rar格式的就可以传输

clsZip zip=new clsZip();
zip.Zip(xml文件,"xml.rar");
deknight 2010-06-02
  • 打赏
  • 举报
回复
最近我很喜欢mark
ggxboy 2010-06-02
  • 打赏
  • 举报
回复
就是压缩完了要进行传输的 向上面方法可以吗?
gxingmin 2010-06-02
  • 打赏
  • 举报
回复
下载个ICSharpCode.SharpZipLib.DLL组件
引用到项目里
ausing System;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;

namespace Common
{
/// <summary>
/// clsZip 的摘要说明。
/// </summary>
public class clsZip
{
public clsZip()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

public void ZipDir(string strDir,string strZipFile)
{
FastZip fz = new FastZip();
fz.CreateEmptyDirectories = true;
fz.CreateZip(strZipFile, strDir, true, "");
fz = null;
}

public void UpZipDir(string strZipFile,string strDir)
{
if (!Directory.Exists(strDir))
Directory.CreateDirectory(strDir);

ZipInputStream zis = new ZipInputStream(File.OpenRead(strZipFile));
ZipEntry theEntry = null;
while ((theEntry = zis.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
if (directoryName != string.Empty)
Directory.CreateDirectory(strDir + directoryName);

if (fileName != string.Empty)
{
FileStream streamWriter = File.Create(Path.Combine(strDir, theEntry.Name));
int size = 2048;
byte[] data = new byte[size];
while (true)
{
size = zis.Read(data, 0, data.Length);
if (size > 0)
streamWriter.Write(data, 0, size);
else
break;
}

streamWriter.Close();
}
}

zis.Close();

}

public void Zip(string strFile, string strZipFile)
{
Crc32 crc1 = new Crc32();
ZipOutputStream stream1 = new ZipOutputStream(File.Create(strZipFile));
try
{
stream1.SetLevel(6);
FileStream stream2 = File.OpenRead(strFile);
byte[] buffer1 = new byte[stream2.Length];
stream2.Read(buffer1, 0, buffer1.Length);
ZipEntry entry1 = new ZipEntry(strFile.Split(new char[] { '\\' })[strFile.Split(new char[] { '\\' }).Length - 1]);
entry1.DateTime = DateTime.Now;
entry1.Size = stream2.Length;
stream2.Close();
crc1.Reset();
crc1.Update(buffer1);
entry1.Crc = crc1.Value;
stream1.PutNextEntry(entry1);
stream1.Write(buffer1, 0, buffer1.Length);
}
catch (Exception exception1)
{
throw exception1;
}
finally
{
stream1.Finish();
stream1.Close();
stream1 = null;
crc1 = null;
}
}


public void UnZip(string strZipFile, string strPath)
{
if (!strPath.EndsWith(@"\"))
{
strPath = strPath + @"\";
}
ZipInputStream stream1 = new ZipInputStream(File.OpenRead(strZipFile));
try
{
ZipEntry entry1;
while ((entry1 = stream1.GetNextEntry()) != null)
{
string text1 = Path.GetFileName(entry1.Name);
if (!Directory.Exists(strPath))
{
Directory.CreateDirectory(strPath);
}
if (text1 != string.Empty)
{
FileStream stream2 = File.Create(strPath + text1);
int num1 = 0x800;
byte[] buffer1 = new byte[0x800];
while (true)
{
num1 = stream1.Read(buffer1, 0, buffer1.Length);
if (num1 <= 0)
{
break;
}
stream2.Write(buffer1, 0, num1);
}
stream2.Close();
}
}
stream1.Close();
}
catch (Exception exception1)
{
throw exception1;
}
finally
{
stream1.Close();
stream1 = null;
}
}



}
}

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧