- 浏览: 31147 次
- 性别:
- 来自: 惠州
最新评论
最近做一个项目需要压缩,解压文件夹及文件夹里面的子文件夹、文件,在网上找了许多资料,有两种实例:
实例一:
实例二:
这里有个问题,实例二中的解压文件的时候,如果解压到的路径中存在解压文件中的文件,解压的时候不会覆盖,这点值得注意。
另外压缩解压文件要用到ICSharpCode.SharpZipLib.dll这个dll,网上可以搜到下载地址。
实例一:
#region 压缩文件 /// 递归压缩文件夹方法 private bool ZipFileDictory(string FolderToZip, ZipOutputStream s, string ParentFolderName) { bool res = true; string[] folders, filenames; ZipEntry entry = null; FileStream fs = null; Crc32 crc = new Crc32(); try { //创建当前文件夹 entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/")); //加上 “/” 才会当成是文件夹创建 s.PutNextEntry(entry); s.Flush(); //先压缩文件,再递归压缩文件夹 filenames = Directory.GetFiles(FolderToZip); foreach (string file in filenames) { //打开压缩文件 fs = File.OpenRead(file); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); entry = new ZipEntry(Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip) + "/" + Path.GetFileName(file))); 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); } } catch { res = false; } finally { if (fs != null) { fs.Close(); fs = null; } if (entry != null) { entry = null; } GC.Collect(); GC.Collect(1); } folders = Directory.GetDirectories(FolderToZip); foreach (string folder in folders) { if (!ZipFileDictory(folder, s, Path.Combine(ParentFolderName, Path.GetFileName(FolderToZip)))) { return false; } } return res; } /// <summary> /// 压缩目录 /// </summary> /// <param name="FolderToZip">待压缩的文件夹,全路径格式</param> /// <param name="ZipedFile">压缩后的文件名,全路径格式</param> /// <param name="Password"></param> /// <returns></returns> private bool ZipFileDictory(string FolderToZip, string ZipedFile, String Password) { bool res; if (!Directory.Exists(FolderToZip)) { return false; } ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFile)); s.SetLevel(6); s.Password = Password; res = ZipFileDictory(FolderToZip, s, ""); s.Finish(); s.Close(); return res; } /// <summary> /// 压缩文件 /// </summary> /// <param name="FileToZip">要进行压缩的文件名</param> /// <param name="ZipedFile">压缩后生成的压缩文件名</param> /// <param name="Password"></param> /// <returns></returns> private bool ZipFile(string FileToZip, string ZipedFile, String Password) { //如果文件没有找到,则报错 if (!File.Exists(FileToZip)) { throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!"); } //FileStream fs = null; FileStream ZipFile = null; ZipOutputStream ZipStream = null; ZipEntry ZipEntry = null; bool res = true; try { ZipFile = File.OpenRead(FileToZip); byte[] buffer = new byte[ZipFile.Length]; ZipFile.Read(buffer, 0, buffer.Length); ZipFile.Close(); ZipFile = File.Create(ZipedFile); ZipStream = new ZipOutputStream(ZipFile); ZipStream.Password = Password; ZipEntry = new ZipEntry(Path.GetFileName(FileToZip)); ZipStream.PutNextEntry(ZipEntry); ZipStream.SetLevel(6); ZipStream.Write(buffer, 0, buffer.Length); } catch { res = false; } finally { if (ZipEntry != null) { ZipEntry = null; } if (ZipStream != null) { ZipStream.Finish(); ZipStream.Close(); } if (ZipFile != null) { ZipFile.Close(); ZipFile = null; } GC.Collect(); GC.Collect(1); } return res; } /// <summary> /// 压缩文件 和 文件夹 /// </summary> /// <param name="FileToZip">待压缩的文件或文件夹,全路径格式</param> /// <param name="ZipedFile">压缩后生成的压缩文件名,全路径格式</param> /// <param name="Password"></param> /// <returns></returns> private bool Zip(String FileToZip, String ZipedFile, String Password) { if (Directory.Exists(FileToZip)) { return ZipFileDictory(FileToZip, ZipedFile, Password); } else if (File.Exists(FileToZip)) { return ZipFile(FileToZip, ZipedFile, Password); } else { return false; } } #endregion #region 解压文件 /// <summary> /// 解压功能(解压压缩文件到指定目录) /// </summary> /// <param name="FileToUpZip">待解压的文件</param> /// <param name="ZipedFolder">指定解压目标目录</param> /// <param name="Password"></param> private void UnZip(string FileToUpZip, string ZipedFolder, string Password) { if (!File.Exists(FileToUpZip)) { return; } if (!Directory.Exists(ZipedFolder)) { Directory.CreateDirectory(ZipedFolder); } ZipInputStream s = null; ZipEntry theEntry = null; string fileName; FileStream streamWriter = null; try { s = new ZipInputStream(File.OpenRead(FileToUpZip)); s.Password = Password; while ((theEntry = s.GetNextEntry()) != null) { if (theEntry.Name != String.Empty) { fileName = Path.Combine(ZipedFolder, theEntry.Name); /**/ ///判断文件路径是否是文件夹 if (fileName.EndsWith("/") || fileName.EndsWith("\\")) { Directory.CreateDirectory(fileName); continue; } streamWriter = File.Create(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; } } } } } finally { if (streamWriter != null) { streamWriter.Close(); streamWriter = null; } if (theEntry != null) { theEntry = null; } if (s != null) { s.Close(); s = null; } GC.Collect(); GC.Collect(1); } } #endregion
实例二:
#region 压缩文件方法二 public void ZipFilenew(string strFileFolder, string strZip) { if (strFileFolder[strFileFolder.Length - 1] != Path.DirectorySeparatorChar) strFileFolder += Path.DirectorySeparatorChar; ZipOutputStream s = new ZipOutputStream(File.Create(strZip)); s.SetLevel(6); // 0 - store only to 9 - means best compression zip(strFileFolder, strZip, s); s.Finish(); s.Close(); } private void zip(string strFile, string StoreFile, ZipOutputStream s) { 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, StoreFile, s); } else { //打开压缩文件,开始压缩 FileStream fs = File.OpenRead(file); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); string tempfile = file.Substring(StoreFile.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); } } } #endregion #region 解压文件方法二 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; } } #endregion
这里有个问题,实例二中的解压文件的时候,如果解压到的路径中存在解压文件中的文件,解压的时候不会覆盖,这点值得注意。
另外压缩解压文件要用到ICSharpCode.SharpZipLib.dll这个dll,网上可以搜到下载地址。
发表评论
-
C#.Net序列化,反序列化
2011-11-15 10:14 1365C#.Net序列化,反序列化 二进制序列化,反序列化方法: ... -
C#/.NET数据存储到XML
2011-09-04 13:31 2710有时候,在存储数据的时候要求存储成XML格式,我们用代 ... -
C#如何禁用datagridview控件中的button控件
2011-09-03 13:29 6174DataGridView 控件包括 DataGridViewB ... -
C#复制文件夹,删除文件夹的方法
2011-09-03 13:17 4439C#复制文件的时候只提供了复制文件的类,没有复制文件夹。 删除 ... -
SQL分页、查找行记录兼C#ASP.NET分页方法
2011-08-03 21:23 2517SQL语句: select * from (select R ... -
(转)网上找的一个使用aspnetpager控件的使用方法
2010-10-04 20:44 1510前台页面代码:Page.aspx <%@ Page L ... -
ASP.NET的一些常用的方法
2010-10-04 20:39 799观牛腩博客(http://niunan.iteye.com/) ... -
aspnetpager与datalist一起使用,dt作为数据源
2010-10-04 20:29 1269前台页面部分代码: <%@ Page Language= ... -
数据库访问助手类(SQLHelper)
2010-07-03 13:02 805说明:对数据库的操作无非就是增删改查,SQLHelper类能提 ...
相关推荐
今天梳理一下项目中用到的压缩、解压文件夹或文件的方法,发现因为需求不同,已经用了好几个不同组件。今天就好好整理记录下,别下次遇到需求又重头开始了。 DotNetZip DotNetZip是一个开源的免费类库,主要提供了...
网上很少有用VB写的压缩文件的代码,但是,在网络...另外加上了多层文件夹压缩解压。但是,因为时间有限,只是将文件全部读取到缓存处理,所以,针对大文件没有做特别的处理。例子里面有提示,根据需要自己改动一下吧。
在.Net Framework 2.0 中添加了System.IO.Compression 类来实现对文件/文件夹的压缩/解压(GZipStream方法),包括文档,代码,类文件
public static GZipResult Compress(string lpSourceFolder, string lpDestFolder, string zipFileName) { return Compress(lpSourceFolder, "*.*", SearchOption.AllDirectories, lpDestFolder, zipFileName, ...
基于ASP.NET2.0的在线压缩-解压源程序代码,实现实现对文档资源的在线压缩与解压操作,采用VS2005+C#+ASP.NET开发。
利用第三方控件解压缩文件,c#写的源代码,第三方控件源代码
ASP.NET 图片上传下载缩略察看C#和VB双版.rar 从本地向SQL Server上传存入图片 从SQL Server下载并在网页中显示图片 同时能显示缩略图和大图 含VB.NET和C#两个版本 内含数据库文件,简单附加到SQL Server中即可 ...
②获得文件夹名及目录,然后遍历文件夹下面的文件以及子文件夹,循环上传; ③是使用AcitiveX控件。 那我果断就先通过上传对话框获得文件夹名和文件夹所在的系统文件路径,可是接下来就错愕了,一开始是想使用...
主要实现了批量复制、剪切文件及文件夹、批量重命名文件、批量删除文件及文件夹、搜索文件及文件夹、批量压缩及解压文件、分割及合并文件 1、开发环境为Visual Studio 2010,使用.net 4.0开发
主要实现了批量复制、剪切文件及文件夹、批量重命名文件、批量删除文件及文件夹、搜索文件及文件夹、批量压缩及解压文件、分割及合并文件 三、注意事项 1、开发环境为Visual Studio 2010,使用.net 4.0开发。
LeadTools DotNet v16.5.0.1 破解版 专业的图像处理程序开发包(.NET工具主要适用于.Net ...由于本人的上传权限只有40M,单个文件设成了39M,所以共压缩了9部分,这是第5部分,请全部下载完后放在一个文件夹内解压
主要内容有C#开发环境的使用、C#语言基础应用、字符串处理技术、数组和集合...文件夹基本操作、文件流操作、加密、解密及解压缩文件、C#与Word互操作、高效应用Excel、基本图形绘制、图像处理技术、常用图表应用、动画...
利用7Zip实现在线压缩与解压,本程序可以独立运行于没有安装7zip压缩软件的服务器上,可以选择多种压缩格式,如7z、gzip、zip等等格式,可以在线压缩文件夹和文件,具体命令行参数可查询相关资料
该cs文件包含两个类ZipClass和UnZipClass。ZipClass实现了对文件夹、文件集和单个文件的压缩。UnZipClass实现了对压缩文件的解压
DotNetZip是一个易于使用,快速,自由操纵类库和工具集压缩文件或文件夹。 zip和解压很简单:与DotNetZip。NET中编写的应用程序在VB,C#中 - 任何。NET语言 - 可以轻松地创建,阅读,摘录,或更新压缩文件。对于单...
把下载的FCKeditor2.4.2.zip文件包直接解压缩到FCKPro的根目录下,这样根目录下就得到一个FCKeditor文件夹,里面富含所有FCKeditor的核心文件。 然后把下载的FCKeditor.Net.zip随便解压缩到你硬盘的一个空目录,...
解压到当前文件夹就有30多兆,再解压一次得到70多兆的源代码,我只有20兆的上传权限,只能压缩两次上传,不过下载后能正常解压没有问题,我试过了,谢谢支持!也希望各位喜欢C#的朋友一起来交流,我的QQ909872237,...
#### CL.IO.Zip 是一个基于SharpZipLib的一个压缩和解压的类库,提供给用户在.net环境下使用(VB.NET,C#..等等)当前最新版本为:V1.1.0 百度网盘下载地址: 做任何操作之前,请使用单例模式,获取ZipHandler对象...