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/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库