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(); }

1private void zip(string strFile, ZipOutputStream s, string staticFile) 2{ 3 if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar; 4 Crc32 crc = new Crc32(); 5 string[] filenames = Directory.GetFileSystemEntries(strFile); 6 foreach (string file in filenames) 7 { 8 9 if (Directory.Exists(file)) 10 { 11 zip(file, s, staticFile); 12 } 13 14 else // 否则直接压缩文件 15 { 16 //打开压缩文件 17 FileStream fs = File.OpenRead(file); 18 19 byte[] buffer = new byte[fs.Length]; 20 fs.Read(buffer, 0, buffer.Length); 21 string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1); 22 ZipEntry entry = new ZipEntry(tempfile); 23 24 entry.DateTime = DateTime.Now; 25 entry.Size = fs.Length; 26 fs.Close(); 27 crc.Reset(); 28 crc.Update(buffer); 29 entry.Crc = crc.Value; 30 s.PutNextEntry(entry); 31 32 s.Write(buffer, 0, buffer.Length); 33 } 34 } 35}

}

、、、、、、、、、、、、、、、

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 {

1public string unZipFile(string TargetFile, string fileDir) 2{ 3 string rootFile = " "; 4 try 5 { 6 //读取压缩文件(zip文件),准备解压缩 7 ZipInputStream s = new ZipInputStream(File.OpenRead(TargetFile.Trim())); 8 ZipEntry theEntry; 9 string path = fileDir; 10 //解压出来的文件保存的路径 11 12 string rootDir = " "; 13 //根目录下的第一个子文件夹的名称 14 while ((theEntry = s.GetNextEntry()) != null) 15 { 16 rootDir = Path.GetDirectoryName(theEntry.Name); 17 //得到根目录下的第一级子文件夹的名称 18 if (rootDir.IndexOf("\\") >= 0) 19 { 20 rootDir = rootDir.Substring(0, rootDir.IndexOf("\\") + 1); 21 } 22 string dir = Path.GetDirectoryName(theEntry.Name); 23 //根目录下的第一级子文件夹的下的文件夹的名称 24 string fileName = Path.GetFileName(theEntry.Name); 25 //根目录下的文件名称 26 if (dir != " " ) 27 //创建根目录下的子文件夹,不限制级别 28 { 29 if (!Directory.Exists(fileDir + "\\" + dir)) 30 { 31 path = fileDir + "\\" + dir; 32 //在指定的路径创建文件夹 33 Directory.CreateDirectory(path); 34 } 35 } 36 else if (dir == " " && fileName != "") 37 //根目录下的文件 38 { 39 path = fileDir; 40 rootFile = fileName; 41 } 42 else if (dir != " " && fileName != "") 43 //根目录下的第一级子文件夹下的文件 44 { 45 if (dir.IndexOf("\\") > 0) 46 //指定文件保存的路径 47 { 48 path = fileDir + "\\" + dir; 49 } 50 } 51 52 if (dir == rootDir) 53 //判断是不是需要保存在根目录下的文件 54 { 55 path = fileDir + "\\" + rootDir; 56 } 57 58 //以下为解压缩zip文件的基本步骤 59 //基本思路就是遍历压缩文件里的所有文件,创建一个相同的文件。 60 if (fileName != String.Empty) 61 { 62 FileStream streamWriter = File.Create(path + "\\" + fileName); 63 64 int size = 2048; 65 byte[] data = new byte[2048]; 66 while (true) 67 { 68 size = s.Read(data, 0, data.Length); 69 if (size > 0) 70 { 71 streamWriter.Write(data, 0, size); 72 } 73 else 74 { 75 break; 76 } 77 } 78 79 streamWriter.Close(); 80 } 81 } 82 s.Close(); 83 84 return rootFile; 85 } 86 catch (Exception ex) 87 { 88 return "1; " + ex.Message; 89 } 90}

}

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]);

1} 2protected void Button2_Click(object sender, EventArgs e) 3{ 4 string[] FileProperties = new string[2]; 5 FileProperties[0] = "D:\\zip\\b.zip";//待解压的文件 6 FileProperties[1] = "D:\\unzipped\\";//解压后放置的目标目录 7 UnZipFloClass UnZc = new UnZipFloClass(); 8 UnZc.unZipFile(FileProperties[0], FileProperties[1]); 9}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

解决Chrome插件安装时出现的“程序包无效”问题

错误信息:程序包无效。详细信息:“Cannotloadextensionwithfileordirectoryname.Filenamesstartingwith""arereservedforusebythesystem.”。1、找到Chrome安装程序路径,找到对应的插件2、把crx后缀名改为rar,解压缩得到文件夹

Linux安装zookeeper

安装zookeeper1、解压缩zookeeper3.4.6.tar.gz:    tarzxvfzookeeper3.4.6.tar.gz2、创建/usr/local/zookeeper文件夹:mkdirp/usr/local/zookeeper 3、进入到/usr/local/zookeeper目录

Python压缩和解压缩实践

工作需要,将已经打好的war包解压出来,重新压缩WEBINF中的classes文件夹到WEBINF.zip那就只好从实际出发,用代码来了解一下python的压缩解压缩了。!/usr/bin/env python encoding: utf8"""@version: 1.0@autho

mysql 5.7.18

1\.下载2\.解压缩3\.添加path环境变量,路径指向mysql所在bin目录下4\.在主目录下创建data文件夹5\.注册windows系统服务 新建一个my.ini文件,拷贝到c:\\windows目录下,内容如下:\client\port3306defaultcharactersetutf8

C#解压或压缩文件夹 - HelloWorld