C#使用SharpZipLib分析
最近要做一个项目涉及到C#中压缩与解压缩的问题的解决方法,大家分享。
这里主要解决文件夹包含文件夹的解压缩问题。
1)下载SharpZipLib.dll,在http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx中有最新免费版本,“Assemblies for .NET 1.1, .NET 2.0, .NET CF 1.0, .NET CF 2.0: Download [297 KB] ”点击Download可以下载,解压后里边有好多文件夹,因为不同的版本,我用的FW2.0。
2)引用SharpZipLib.dll,在项目中点击项目右键-->添加引用-->浏览,找到要添加的DLL-->确认
3)改写了文件压缩和解压缩的两个类,新建两个类名字为ZipFloClass.cs,UnZipFloClass.cs
源码如下
4)引用,新建一个页面,添加两个按钮,为按钮添加Click事件
源码如下
protected void Button1_Click(object sender, EventArgs e)
{
string[] FileProperties = new string[2];
FileProperties[0] = "D:\\unzipped\";//待压缩文件目录
FileProperties[1] = "D:\\zip\\a.zip"; //压缩后的目标文件
ZipFloClass Zc = new ZipFloClass();
Zc.ZipFile(FileProperties[0], FileProperties[1]);
}
protected void Button2_Click(object sender, EventArgs e)
{
string[] FileProperties = new string[2];
FileProperties[0] = "D:\\zip\\b.zip";//待解压的文件
FileProperties[1] = "D:\\unzipped\";//解压后放置的目标目录
UnZipFloClass UnZc = new UnZipFloClass();
UnZc.unZipFile(FileProperties[0], FileProperties[1]);
}
5)一切OK,可以测试一下,我是可以运行的。
文章来自学IT网:http://www.xueit.com/html/2009-03/21_826_00.html