其实是没有什么是终极的,一个抓眼球的标题而已。CLR2.0里带来很多新东东,其中System.IO.Compression就是一个新的命名空间,里面包括两个类GzipStream和DeflaterStream,它们都可以用于解压缩,但是不支持ZIP、RAR等常用的压缩文件,RAR是涉及到专利的问题,而ZIP虽然格式是公开的,但是我想微软不太想支持,它可能更想大家用它自家的私房菜CAB格式,可是Dotnet CLR居然也不支持,一起鄙视它!
网上有很多贴是关于如何解压缩的问题,但是很少有比较全的介绍,或者是存在有BUG的代码。CSDN上的网友问这个问题也问得比较多,但是没有标准的答案,我现在把我最近写的压缩代码发出来,算是给大家一个标准答案吧。
下面是两个类的代码,均属于ExpertLib.Compress这个空间里,它们分别是
Compress(ExpertLib.Compress.Compress) //用GzipStream实现的多种常用的压缩函数
ZIP(ExpertLib.Compress.ZIP) //可以将目录打包解压的ZIP标准文件
代码支持:
首先你需要在CLR2.0上才能运行这个代码,因为GzipStream是2.0才提供的,为什么我不使用DeflaterStream,没有原因!
另外,如果你使用ZIP这个类来实现解压缩ZIP文件,请到此处下载最新的SharpZipLib (下载地址:http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx ),当然你也需要下for2.0版的,这个地址可以下到它的源代码,大家可以看看源代码。
特别提示:
我在Compress这个类里特别提供了ToBase64String和FromBase64String两个方法,主要是通过HTTP协议传输数据时,这个编码方式可以通过防火墙。
BUG报告:
ZIP的实现里用到了FastZip这个类,但是FastZip好象并不支持PassWord,或者是我没用对。
----------------------------------------------我是分隔线--------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Xml;
using System.Data;
using System.Runtime.Serialization.Formatters.Binary;
namespace Streaming.Compress
{
/// <summary>
/// 压缩类,仅支持CLR 2.0,使用了.0新增的GZipStream类
/// </summary>
public class Compress
{
#region 压缩
#region CompressXmlDocument
/// <summary>
/// 压缩一个XML文档
/// </summary>
/// <param name="input">The XML document to compress.</param>
/// <returns>Returns the compressed XML document.</returns>
public static byte[] CompressXmlDocument(XmlDocument input)
{
return CompressString(input.InnerXml);
}
#endregion
#region CompressDataSet
/// <summary>
/// 压缩DataSet
/// </summary>
/// <param name="input">The DataSet to compress.</param>
/// <returns>Returns the compressed DataSet.</returns>
public static byte[] CompressDataSet(DataSet input)
{
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, input);
return CompressBytes(ms.GetBuffer());
}
}
#endregion
#region CompressString
/// <summary>
/// 压缩字符串
/// </summary>
/// <param name="unCompressedString"></param>
/// <returns></returns>
public static byte[] CompressString(string unCompressedString)
{
byte[] bytData = System.Text.Encoding.UTF8.GetBytes(unCompressedString);
return CompressBytes(bytData);
}
#endregion CompressString
#region CompressBytes
/// <summary>
/// 压缩字节数组
/// </summary>
public static byte[] CompressBytes(byte[] data)
{
MemoryStream output = new MemoryStream();
GZipStream gzip = new GZipStream(output,
CompressionMode.Compress, true);
gzip.Write(data, 0, data.Length);
gzip.Close();
return output.ToArray();
}
#endregion
#endregion
#region 解压缩
#region DecompressXmlDocument
/// <summary>
/// 解压缩用CompressXmlDocument生成的Byte数组
/// </summary>
/// <param name="input">The buffer that contains the compressed
/// stream with the XML document.</param>
/// <returns>Returns the decompressed XML document.</returns>
public static XmlDocument DecompressXmlDocument(byte[] input)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(DecompressString(input));
return doc;
}
#endregion
#region DecompresString
/// <summary>
/// 解压缩用CompressString压缩生成的字符串
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string DecompressString(byte[] data)
{
return System.Text.Encoding.UTF8.GetString(DecompressBytes(data));
}
#endregion
#region DecompressDataSet
/// <summary>
/// 解压缩用CompressDataSet生成的Byte数组
/// </summary>
/// <param name="input">The buffer that contains the compressed
/// stream with the DataSet.</param>
/// <returns>Returns the decompressed DataSet.</returns>
public static DataSet DecompressDataSet(byte[] input)
{
BinaryFormatter bf = new BinaryFormatter();
byte[] buffer = DecompressBytes(input);
using (MemoryStream ms = new MemoryStream(buffer))
{
return (DataSet)bf.Deserialize(ms);
}
}
#endregion
#region DecompressBytes
/// <summary>
/// 解压字节数组
/// </summary>
/// <param name="data">The buffer that contains the compressed
/// stream with the bytes.</param>
/// <returns>Returns the decompressed bytes.</returns>
public static byte[] DecompressBytes(byte[] data)
{
MemoryStream input = new MemoryStream();
input.Write(data, 0, data.Length);
input.Position = 0;
GZipStream gzip = new GZipStream(input, CompressionMode.Decompress, true);
MemoryStream output = new MemoryStream();
byte[] buff = new byte[4096];
int read = -1;
read = gzip.Read(buff, 0, buff.Length);
while (read > 0)
{
output.Write(buff, 0, read);
read = gzip.Read(buff, 0, buff.Length);
}
gzip.Close();
return output.ToArray();
}
#endregion
#endregion
#region 辅助函数
#region ToBase64String
/// <summary>
/// 转成可以在网上传输的Base64编码字符串
/// </summary>
public static string ToBase64String(byte[] data)
{
return Convert.ToBase64String(data);
}
#endregion
#region FromBase64String
/// <summary>
/// 从Base64编码字符串转成字节数组
/// </summary>
public static byte[] FromBase64String(string str)
{
return Convert.FromBase64String(str);
}
#endregion
#endregion
}
}
----------------------------------------------我是分隔线--------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
namespace Streaming.Compress
{
#region 委托
public class ZipProcessEventArgs : System.EventArgs
{
public string Name;
public bool ContinueRunning;
/// <summary>
/// 文件或目录,false表示文件,true表示目录
/// </summary>
public bool FileOrDirectory;
}
public delegate void ZipProcessEventDelegate(object sender, ZipProcessEventArgs e);
#endregion
/// <summary>
/// ZIP格式文件解压缩类
/// </summary>
public class ZIP
{
#region 内部变量
private FastZipEvents events;
private FastZip fz;
#endregion
#region 构造器
public ZIP()
{
events = new FastZipEvents();
events.ProcessDirectory = new ProcessDirectoryHandler(OnProcessDirectory);
events.ProcessFile = new ProcessFileHandler(OnProcessFile);
fz = new FastZip(events);
}
#endregion
#region Event
/// <summary>
/// 处理文件时发生
/// </summary>
public event ZipProcessEventDelegate ProcessFileEvent;
/// <summary>
/// 处理目录时发生
/// </summary>
public event ZipProcessEventDelegate ProcessDirectoryEvent;
#endregion
#region 私有方法
private void OnProcessFile(object sender, ScanEventArgs e)
{
if (this.ProcessFileEvent != null)
{
ZipProcessEventArgs ea = new ZipProcessEventArgs();
ea.Name = e.Name;
ea.ContinueRunning = true;
ea.FileOrDirectory = false;
this.ProcessFileEvent(this, ea);
e.ContinueRunning = ea.ContinueRunning;
}
}
private void OnProcessDirectory(object sender, DirectoryEventArgs e)
{
if (this.ProcessDirectoryEvent != null)
{
ZipProcessEventArgs ea = new ZipProcessEventArgs();
ea.Name = e.Name;
ea.ContinueRunning = true;
ea.FileOrDirectory = true;
this.ProcessDirectoryEvent(this, ea);
e.ContinueRunning = ea.ContinueRunning;
}
}
#endregion
#region ZipDirectory
/// <summary>
/// 将一个目录中所有的文件压缩到一个Zip包中
/// </summary>
/// <param name="zipFilename">压缩后的文件名</param>
/// <param name="sourceDirectory">待压缩文件的所在目录</param>
public void ZipDirectory(string zipFile, string sourceDirectory)
{
ZipDirectory(zipFile, sourceDirectory, null);
}
/// <summary>
/// 将一个目录中所有的文件压缩到一个Zip包中
/// </summary>
public void ZipDirectory(string zipFile, string sourceDirectory, string password)
{
if ((zipFile == null) || (zipFile.Trim() == ""))
{
throw new System.ArgumentNullException("zipFile");
}
if ((sourceDirectory == null) || (sourceDirectory.Trim() == ""))
{
throw new System.ArgumentNullException("sourceDirectory");
}
if (!Directory.Exists(sourceDirectory))
{
throw new System.Exception("No Exists Directory!");
}
if ((password == null) || (password == ""))
{
fz.Password = null;
}
else
{
fz.Password = password;
}
fz.CreateZip(zipFile, sourceDirectory, true, null);
fz.Password = null;
}
#endregion
#region UnzipDirectory
/// <summary>
/// 解压缩一个zip文件到一个目录中
/// </summary>
/// <param name="zipFile">待解压缩的文件</param>
/// <param name="directory">解压缩后文件存放的目录</param>
public void UnzipDirectory(string zipFile, string directory)
{
UnzipDirectory(zipFile, directory, null);
}
/// <summary>
/// 解压缩一个zip文件到一个目录中
/// </summary>
public void UnzipDirectory(string zipFile, string directory, string password)
{
if ((zipFile == null) || (zipFile.Trim() == ""))
{
throw new System.ArgumentNullException("zipFile");
}
if ((directory == null) || (directory.Trim() == ""))
{
throw new System.ArgumentNullException("sourceDirectory");
}
if (!File.Exists(zipFile))
{
throw new System.Exception("No Exists Zip file: " + zipFile);
}
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
if ((password == null) || (password == ""))
{
fz.Password = null;
}
else
{
fz.Password = password;
}
fz.ExtractZip(zipFile, directory, null);
fz.Password = null;
}
#endregion
}
}