#ZipLib is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. It is implemented as an assembly (installable in the GAC), and thus can easily be incorporated into other projects (in any .NET language).#ZipLib was ported from the GNU Classpath ZIP library for use with #Develop (http://www.icsharpcode.net/OpenSource/SD) which needed gzip/zip compression. Later bzip2 compression and tar archiving was added due to popular demand.
ZipLib组件与.net自带的Copression比较,在压缩方面更胜一筹,经过BZip2压缩要小很多,亲手测试,不信你也可以试一试。而且这个功能更加强大。下面就是个人做的一个小例子,具体的应用程序源码: /Files/yank/Compress.rar。
using System;2
using System.Data;3
using System.IO;4
using ICSharpCode.SharpZipLib.Zip.Compression;5
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;6
using ICSharpCode.SharpZipLib.GZip;7

8
/// <summary>9
/// Summary description for ICSharp10
/// </summary>11
public class ICSharp12
{13
public ICSharp()14
{15
//16
// TODO: Add constructor logic here17
//18
}19
/// <summary>20
/// 压缩21
/// </summary>22
/// <param name="param"></param>23
/// <returns></returns>24
public string Compress(string param)25
{26
byte[] data = System.Text.Encoding.UTF8.GetBytes(param);27
//byte[] data = Convert.FromBase64String(param);28
MemoryStream ms = new MemoryStream();29
Stream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms);30
try31
{32
stream.Write(data, 0, data.Length);33
}34
finally 35
{36
stream.Close();37
ms.Close();38
}39
return Convert.ToBase64String(ms.ToArray());40
}41
/// <summary>42
/// 解压43
/// </summary>44
/// <param name="param"></param>45
/// <returns></returns>46
public string Decompress(string param)47
{48
string commonString="";49
byte[] buffer=Convert.FromBase64String(param);50
MemoryStream ms = new MemoryStream(buffer);51
Stream sm = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(ms);52
//这里要指明要读入的格式,要不就有乱码53
StreamReader reader = new StreamReader(sm,System.Text.Encoding.UTF8);54
try55
{56
commonString=reader.ReadToEnd();57
}58
finally59
{60
sm.Close();61
ms.Close();62
}63
return commonString;64
}65
}Encoding.UTF8与Convert.FromBase64String
Unicode 标准为所有支持脚本中的每个字符分配一个码位(一个数字)。Unicode 转换格式 (UTF) 是一种码位编码方式。Unicode 标准 3.2 版使用下列 UTF:
UTF-8,它将每个码位表示为一个由 1 至 4 个字节组成的序列。
UTF-16,它将每个码位表示为一个由 1 至 2 个 16 位整数组成的序列。
UTF-32,它将每个码位表示为一个 32 位整数。
Convert.FromBase64String 方法
将指定的 String(它将二进制数据编码为 base 64 数字)转换成等效的 8 位无符号整数数组。
它的参数也又一定的要求:
参数是 由基 64 数字、空白字符和尾随填充字符组成。从零开始以升序排列的以 64 为基的数字为大写字符“A”到“Z”、小写字符“a”到“z”、数字“0”到“9”以及符号“+”和“/”。空白字符为 Tab、空格、回车和换行。s 中可以出现任意数目的空白字符,因为所有空白字符都将被忽略。无值字符“=”用于尾部的空白。s 的末尾可以包含零个、一个或两个填充字符。
异常:
| 异常类型 | 条件 |
|---|---|
| ArgumentNullException | s 为空引用(Visual Basic 中为 Nothing)。 |
| FormatException | s 的长度(忽略空白字符)小于 4。
- 或 - s 的长度(忽略空白字符)不是 4 的偶数倍。 |
s 的长度(忽略空白字符)不是 4 的偶数倍。


