C#xml的压缩与解压还原(使用系统自带的压缩与解压)(源码分享)
在网上搜索了很多关于xml的压缩与解压的问题,解决方案比较多的是采用开源或者别的组件来实现xml的压缩与解压的,但却找不到系统自身的最简单的实现方式。
其实原理很简单,把xml转成string,然后对string进行压缩。解压就是其逆向的过程。
其实原理很简单,把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;
}
}
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/