您好,欢迎访问一九零五行业门户网

C#解压或压缩文件夹的代码

c#解压或压缩文件夹
最近要做一个项目涉及到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
源码如下
using system; using system.data; using system.configuration; using system.web; using system.web.security; using system.web.ui; using system.io; using icsharpcode.sharpziplib.checksums; using icsharpcode.sharpziplib.zip; using icsharpcode.sharpziplib.gzip; /// <summary> /// zipfloclass 的摘要说明 /// </summary> public class zipfloclass { public void zipfile(string strfile, string strzip) { if (strfile[strfile.length - 1] != path.directoryseparatorchar) strfile += path.directoryseparatorchar; zipoutputstream s = new zipoutputstream(file.create(strzip)); s.setlevel(6); // 0 - store only to 9 - means best compression zip(strfile, s, strfile); s.finish(); s.close(); } private void zip(string strfile, zipoutputstream s, string staticfile) { if (strfile[strfile.length - 1] != path.directoryseparatorchar) strfile += path.directoryseparatorchar; crc32 crc = new crc32(); string[] filenames = directory.getfilesystementries(strfile); foreach (string file in filenames) { if (directory.exists(file)) { zip(file, s, staticfile); } else // 否则直接压缩文件 { //打开压缩文件 filestream fs = file.openread(file); byte[] buffer = new byte[fs.length]; fs.read(buffer, 0, buffer.length); string tempfile = file.substring(staticfile.lastindexof("\\") + 1); zipentry entry = new zipentry(tempfile); entry.datetime = datetime.now; entry.size = fs.length; fs.close(); crc.reset(); crc.update(buffer); entry.crc = crc.value; s.putnextentry(entry); s.write(buffer, 0, buffer.length); } } } }
、、、、、、、、、、、、、、、
using system; using system.data; using system.web; using system.text; using system.collections; using system.io; using system.diagnostics; using system.runtime.serialization.formatters.binary; using icsharpcode.sharpziplib.bzip2; using icsharpcode.sharpziplib.zip; using icsharpcode.sharpziplib.zip.compression; using icsharpcode.sharpziplib.zip.compression.streams; using icsharpcode.sharpziplib.gzip; using icsharpcode.sharpziplib.checksums; /// <summary> /// unzipfloclass 的摘要说明 /// </summary> public class unzipfloclass { public string unzipfile(string targetfile, string filedir) { string rootfile = " "; try { //读取压缩文件(zip文件),准备解压缩 zipinputstream s = new zipinputstream(file.openread(targetfile.trim())); zipentry theentry; string path = filedir; //解压出来的文件保存的路径 string rootdir = " "; //根目录下的第一个子文件夹的名称 while ((theentry = s.getnextentry()) != null) { rootdir = path.getdirectoryname(theentry.name); //得到根目录下的第一级子文件夹的名称 if (rootdir.indexof("\\") >= 0) { rootdir = rootdir.substring(0, rootdir.indexof("\\") + 1); } string dir = path.getdirectoryname(theentry.name); //根目录下的第一级子文件夹的下的文件夹的名称 string filename = path.getfilename(theentry.name); //根目录下的文件名称 if (dir != " " ) //创建根目录下的子文件夹,不限制级别 { if (!directory.exists(filedir + "\\" + dir)) { path = filedir + "\\" + dir; //在指定的路径创建文件夹 directory.createdirectory(path); } } else if (dir == " " && filename != "") //根目录下的文件 { path = filedir; rootfile = filename; } else if (dir != " " && filename != "") //根目录下的第一级子文件夹下的文件 { if (dir.indexof("\\") > 0) //指定文件保存的路径 { path = filedir + "\\" + dir; } } if (dir == rootdir) //判断是不是需要保存在根目录下的文件 { path = filedir + "\\" + rootdir; } //以下为解压缩zip文件的基本步骤 //基本思路就是遍历压缩文件里的所有文件,创建一个相同的文件。 if (filename != string.empty) { filestream streamwriter = file.create(path + "\\" + filename); int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.read(data, 0, data.length); if (size > 0) { streamwriter.write(data, 0, size); } else { break; } } streamwriter.close(); } } s.close(); return rootfile; } catch (exception ex) { return "1; " + ex.message; } } }
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,可以测试一下,我是可以运行的。
以上就是c#解压或压缩文件夹的代码的详细内容。
其它类似信息

推荐信息