C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件

转自:http://www.cnblogs.com/xuanye/archive/2011/10/19/2217211.html

大家可以到http://www.icsharpcode.net/opensource/sharpziplib/ 下载SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, GZip, BZip2 和Tar格式,其实没啥好说的直接上代码

  1 /// <summary>
  2 /// Zip压缩与解压缩
  3 /// </summary>
  4 public class ZipHelper
  5 {
  6     /// <summary>
  7     /// 压缩单个文件
  8     /// </summary>
  9     /// <param name="fileToZip">要压缩的文件</param>
 10     /// <param name="zipedFile">压缩后的文件</param>
 11     /// <param name="compressionLevel">压缩等级</param>
 12     /// <param name="blockSize">每次写入大小</param>
 13     public static void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)
 14     {
 15         //如果文件没有找到,则报错
 16         if (!System.IO.File.Exists(fileToZip))
 17         {
 18             throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
 19         }
 20
 21         using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
 22         {
 23             using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
 24             {
 25                 using (System.IO.FileStream StreamToZip = new System.IO.FileStream(fileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read))
 26                 {
 27                     string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1);
 28
 29                     ZipEntry ZipEntry = new ZipEntry(fileName);
 30
 31                     ZipStream.PutNextEntry(ZipEntry);
 32
 33                     ZipStream.SetLevel(compressionLevel);
 34
 35                     byte[] buffer = new byte[blockSize];
 36
 37                     int sizeRead = 0;
 38
 39                     try
 40                     {
 41                         do
 42                         {
 43                             sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
 44                             ZipStream.Write(buffer, 0, sizeRead);
 45                         }
 46                         while (sizeRead > 0);
 47                     }
 48                     catch (System.Exception ex)
 49                     {
 50                         throw ex;
 51                     }
 52
 53                     StreamToZip.Close();
 54                 }
 55
 56                 ZipStream.Finish();
 57                 ZipStream.Close();
 58             }
 59
 60             ZipFile.Close();
 61         }
 62     }
 63
 64     /// <summary>
 65     /// 压缩单个文件
 66     /// </summary>
 67     /// <param name="fileToZip">要进行压缩的文件名</param>
 68     /// <param name="zipedFile">压缩后生成的压缩文件名</param>
 69     public static void ZipFile(string fileToZip, string zipedFile)
 70     {
 71         //如果文件没有找到,则报错
 72         if (!File.Exists(fileToZip))
 73         {
 74             throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
 75         }
 76
 77         using (FileStream fs = File.OpenRead(fileToZip))
 78         {
 79             byte[] buffer = new byte[fs.Length];
 80             fs.Read(buffer, 0, buffer.Length);
 81             fs.Close();
 82
 83             using (FileStream ZipFile = File.Create(zipedFile))
 84             {
 85                 using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
 86                 {
 87                     string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1);
 88                     ZipEntry ZipEntry = new ZipEntry(fileName);
 89                     ZipStream.PutNextEntry(ZipEntry);
 90                     ZipStream.SetLevel(5);
 91
 92                     ZipStream.Write(buffer, 0, buffer.Length);
 93                     ZipStream.Finish();
 94                     ZipStream.Close();
 95                 }
 96             }
 97         }
 98     }
 99
100     /// <summary>
101     /// 压缩多层目录
102     /// </summary>
103     /// <param name="strDirectory">The directory.</param>
104     /// <param name="zipedFile">The ziped file.</param>
105     public static void ZipFileDirectory(string strDirectory, string zipedFile)
106     {
107         using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
108         {
109             using (ZipOutputStream s = new ZipOutputStream(ZipFile))
110             {
111                 ZipSetp(strDirectory, s, "");
112             }
113         }
114     }
115
116     /// <summary>
117     /// 递归遍历目录
118     /// </summary>
119     /// <param name="strDirectory">The directory.</param>
120     /// <param name="s">The ZipOutputStream Object.</param>
121     /// <param name="parentPath">The parent path.</param>
122     private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
123     {
124         if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
125         {
126             strDirectory += Path.DirectorySeparatorChar;
127         }
128         Crc32 crc = new Crc32();
129
130         string[] filenames = Directory.GetFileSystemEntries(strDirectory);
131
132         foreach (string file in filenames)// 遍历所有的文件和目录
133         {
134
135             if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
136             {
137                 string pPath = parentPath;
138                 pPath += file.Substring(file.LastIndexOf("\\") + 1);
139                 pPath += "\\";
140                 ZipSetp(file, s, pPath);
141             }
142
143             else // 否则直接压缩文件
144             {
145                 //打开压缩文件
146                 using (FileStream fs = File.OpenRead(file))
147                 {
148
149                     byte[] buffer = new byte[fs.Length];
150                     fs.Read(buffer, 0, buffer.Length);
151
152                     string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1);
153                     ZipEntry entry = new ZipEntry(fileName);
154
155                     entry.DateTime = DateTime.Now;
156                     entry.Size = fs.Length;
157
158                     fs.Close();
159
160                     crc.Reset();
161                     crc.Update(buffer);
162
163                     entry.Crc = crc.Value;
164                     s.PutNextEntry(entry);
165
166                     s.Write(buffer, 0, buffer.Length);
167                 }
168             }
169         }
170     }
171
172     /// <summary>
173     /// 解压缩一个 zip 文件。
174     /// </summary>
175     /// <param name="zipedFile">The ziped file.</param>
176     /// <param name="strDirectory">The STR directory.</param>
177     /// <param name="password">zip 文件的密码。</param>
178     /// <param name="overWrite">是否覆盖已存在的文件。</param>
179     public void UnZip(string zipedFile, string strDirectory, string password, bool overWrite)
180     {
181
182         if (strDirectory == "")
183             strDirectory = Directory.GetCurrentDirectory();
184         if (!strDirectory.EndsWith("\\"))
185             strDirectory = strDirectory + "\\";
186
187         using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile)))
188         {
189             s.Password = password;
190             ZipEntry theEntry;
191
192             while ((theEntry = s.GetNextEntry()) != null)
193             {
194                 string directoryName = "";
195                 string pathToZip = "";
196                 pathToZip = theEntry.Name;
197
198                 if (pathToZip != "")
199                     directoryName = Path.GetDirectoryName(pathToZip) + "\\";
200
201                 string fileName = Path.GetFileName(pathToZip);
202
203                 Directory.CreateDirectory(strDirectory + directoryName);
204
205                 if (fileName != "")
206                 {
207                     if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName)))
208                     {
209                         using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName))
210                         {
211                             int size = 2048;
212                             byte[] data = new byte[2048];
213                             while (true)
214                             {
215                                 size = s.Read(data, 0, data.Length);
216
217                                 if (size > 0)
218                                     streamWriter.Write(data, 0, size);
219                                 else
220                                     break;
221                             }
222                             streamWriter.Close();
223                         }
224                     }
225                 }
226             }
227
228             s.Close();
229         }
230     }
231
232 }
时间: 2024-12-24 23:15:28

C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件的相关文章

C# 下利用ICSharpCode.SharpZipLib.dll实现文件/文件夹压缩、解压缩

ICSharpCode.SharpZipLib.dll下载地址 1.压缩某个指定目录下日志,将日志压缩到CompressionDirectory文件夹中,并清除原来未压缩日志. #region 压缩logs目录下日志 public static void CompresslogDic() { try { string logFilePath = AppDomain.CurrentDomain.BaseDirectory + "logs"; DirectoryInfo logsDic =

C#文件或文件夹压缩和解压方法(通过ICSharpCode.SharpZipLib.dll)

我在网上收集一下文件的压缩和解压的方法,是通过ICSharpCode.SharpZipLib.dll 来实现的 一.介绍的目录 第一步:下载压缩和解压的 ICSharpCode.SharpZipLib.dll 支持库 第二步:创建一个压缩和解压的demo项目 第三步:查看压缩和解压的文件的结果 二.demo演示(包括源码和界面) 1.下载文件压缩和解压的支持库dll ,下载地址:http://pan.baidu.com/s/1pLausnL 2.创建window创建项目 1) 添加引用(文件压缩

C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用

工作中我们很多时候需要进行对文件进行压缩,比较通用的压缩的dll就是ICSharpCode.SharpZipLib.dll,废话不多了,网上也有很多的资料,我将其最常用的两个函数整理了一下,提供了一个通用的类,这样在工作中可以快速的完成压缩和解压缩的动作哦 官网下载地址:  http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx 1. 在项目中添加对ICSharpCode.SharpZipLib.dll的引用: 2. 在需要

C# 下利用ICSharpCode.SharpZipLib.dll实现文件/目录压缩、解压缩

ICSharpCode.SharpZipLib.dll下载地址 1.压缩某个指定文件夹下日志,将日志压缩到CompressionDirectory文件夹中,并清除原来未压缩日志. #region 压缩logs文件夹下日志 public static void CompresslogDic() { try { string logFilePath = AppDomain.CurrentDomain.BaseDirectory + "logs"; DirectoryInfo logsDic

ICSharpCode.SharpZipLib 开源压缩库使用示例

官方网站:http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx 插件描述: ICSharpCode.SharpZipLib.dll 是一个完全由c#编写的Zip, GZip, Tar and BZip2 library,可以方便地支持这几种格式的压缩解压缩, SharpZipLib 的许可是经过修改的GPL,底线是允许用在不开源商业软件中,意思就是免费使用. 一.在ThinksKing的Plugins里面找到已经解压好的Sh

ICSharpCode.SharpZipLib实现压缩解压缩

最近,在项目中经常需要处理压缩和解压缩文件的操作.经过查找,发现了ICSharpCode.SharpZipLib.dll ,这是一个完全由c#编写的Zip, GZip.Tar . BZip2 类库,可以方便地支持这几种格式的压缩解压缩,详细参考了 ICSharpCode.SharpZipLib 插件使用示例.整理很详细.记录备用.

ICSharpCode.SharpZipLib.dll,MyZip.dll,Ionic.Zip.dll 使用

MyZip.dll : 有BUG,会把子目录的文件解压到根目录.. ICSharpCode.SharpZipLib.dll: 把ICSharpCode.SharpZipLib.dll复制一份,重命名为ICSharpCode.SharpZipLib_up.dll ,A项目(主程序)用ICSharpCode.SharpZipLib.dll,B项目(升级程序)用ICSharpCode.SharpZipLib_up.dll.A,B项目其实调用的还是ICSharpCode.SharpZipLib.dll.

Linux下压缩某个文件夹(文件夹打包)

tar -zcvf /home/xahot.tar.gz /xahottar -zcvf 打包后生成的文件名全路径 要打包的目录 例子:把/xahot文件夹打包后生成一个/home/xahot.tar.gz的文件.zip 压缩方法:压缩当前的文件夹 zip -r ./xahot.zip ./* -r表示递归zip [参数] [打包后的文件名] [打包的目录路径]解压 unzip xahot.zip 不解释linux zip命令的基本用法是:linux zip命令参数列表:-a 将文件转成ASCI

albert1017 Linux下压缩某个文件夹(文件夹打包)

tar -zcvf /home/xahot.tar.gz /xahottar -zcvf 打包后生成的文件名全路径 要打包的目录例子:把/xahot文件夹打包后生成一个/home/xahot.tar.gz的文件. # tar -xf all.tar 这条命令是解出all.tar包中所有文件,-x是解开的意思 zip 压缩方法: 压缩当前的文件夹 zip -r ./xahot.zip ./* -r表示递归zip [参数] [打包后的文件名] [打包的目录路径]解压 unzip xahot.zip