其實原理很簡單,把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/