1 /***
2 * Title:"基础工具" 项目
3 * Title:"基础工具" 项目
4 * 主题:压缩包帮助类
5 * Description:
6 * 功能:
7 * 1、压缩单个文件
8 * 2、压缩多个文件
9 * 3、压缩多层目录
10 * 4、递归遍历目录
11 * 5、解压缩一个 zip 文件
12 * 6、获取压缩文件中指定类型的文件
13 * 7、获取压缩文件中的所有文件
14 * Date:2021
15 * Version:0.1版本
16 * Author:Coffee
17 * Modify Recoder:
18 */
19
20 using ICSharpCode.SharpZipLib.Zip;
21 using System;
22 using System.Collections.Generic;
23 using System.IO;
24 using System.Text;
25
26 namespace Utils.Zip
27 {
28 class ZipHelper2
29 {
30 /// <summary>
31 /// 压缩单个文件
32 /// </summary>
33 /// <param name="fileToZip">要压缩的文件</param>
34 /// <param name="zipedFile">压缩后的文件</param>
35 /// <param name="compressionLevel">压缩等级</param>
36 /// <param name="blockSize">每次写入大小</param>
37 public static void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)
38 {
39 //如果文件没有找到,则报错
40 if (!System.IO.File.Exists(fileToZip))
41 {
42 throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
43 }
44
45 using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
46 {
47 using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
48 {
49 using (System.IO.FileStream StreamToZip = new System.IO.FileStream(fileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read))
50 {
51 string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1);
52
53 ZipEntry ZipEntry = new ZipEntry(fileName);
54
55 ZipStream.PutNextEntry(ZipEntry);
56
57 ZipStream.SetLevel(compressionLevel);
58
59 byte[] buffer = new byte[blockSize];
60
61 int sizeRead = 0;
62
63 try
64 {
65 do
66 {
67 sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
68 ZipStream.Write(buffer, 0, sizeRead);
69 }
70 while (sizeRead > 0);
71 }
72 catch (System.Exception ex)
73 {
74 throw ex;
75 }
76
77 StreamToZip.Close();
78 }
79
80 ZipStream.Finish();
81 ZipStream.Close();
82 }
83
84 ZipFile.Close();
85 }
86 }
87
88 /// <summary>
89 /// 压缩单个文件
90 /// </summary>
91 /// <param name="fileToZip">要进行压缩的文件名</param>
92 /// <param name="zipedFile">压缩后生成的压缩文件名</param>
93 public static void ZipFile(string fileToZip, string zipedFile)
94 {
95 //如果文件没有找到,则报错
96 if (!File.Exists(fileToZip))
97 {
98 throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
99 }
100
101 using (FileStream fs = File.OpenRead(fileToZip))
102 {
103 byte[] buffer = new byte[fs.Length];
104 fs.Read(buffer, 0, buffer.Length);
105 fs.Close();
106
107 using (FileStream ZipFile = File.Create(zipedFile))
108 {
109 using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
110 {
111 string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1);
112 ZipEntry ZipEntry = new ZipEntry(fileName);
113 ZipStream.PutNextEntry(ZipEntry);
114 ZipStream.SetLevel(5);
115
116 ZipStream.Write(buffer, 0, buffer.Length);
117 ZipStream.Finish();
118 ZipStream.Close();
119 }
120 }
121 }
122 }
123
124 /// <summary>
125 /// 压缩多个文件到指定路径
126 /// </summary>
127 /// <param name="sourceFileNames">压缩到哪个路径</param>
128 /// <param name="zipFileName">压缩文件名称</param>
129 public static void ZipFile(List<string> sourceFileNames, string zipFileName)
130 {
131 //压缩文件打包
132 using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFileName)))
133 {
134 s.SetLevel(9);
135 byte[] buffer = new byte[4096];
136 foreach (string file in sourceFileNames)
137 {
138 if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
139 {
140 string pPath = "";
141 pPath += Path.GetFileName(file);
142 pPath += "\\";
143 ZipSetp(file, s, pPath, sourceFileNames);
144 }
145 else // 否则直接压缩文件
146 {
147
148 ZipEntry entry = new ZipEntry(Path.GetFileName(file));
149 entry.DateTime = DateTime.Now;
150 s.PutNextEntry(entry);
151 using (FileStream fs = File.OpenRead(file))
152 {
153 int sourceBytes;
154 do
155 {
156 sourceBytes = fs.Read(buffer, 0, buffer.Length);
157 s.Write(buffer, 0, sourceBytes);
158 } while (sourceBytes > 0);
159 }
160 }
161 }
162 s.Finish();
163 s.Close();
164 }
165 }
166
167
168 /// <summary>
169 /// 压缩多层目录
170 /// </summary>
171 /// <param name="strDirectory">待压缩目录</param>
172 /// <param name="zipedFile">压缩后生成的压缩文件名,绝对路径</param>
173 public static void ZipFileDirectory(string strDirectory, string zipedFile)
174 {
175 using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
176 {
177 using (ZipOutputStream s = new ZipOutputStream(ZipFile))
178 {
179 s.SetLevel(9);
180 ZipSetp(strDirectory, s, "");
181 }
182 }
183 }
184
185 /// <summary>
186 /// 压缩多层目录
187 /// </summary>
188 /// <param name="strDirectory">待压缩目录</param>
189 /// <param name="zipedFile">压缩后生成的压缩文件名,绝对路径</param>
190 /// <param name="files">指定要压缩的文件列表(完全路径)</param>
191 public static void ZipFileDirectory(string strDirectory, string zipedFile, List<string> files)
192 {
193 using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
194 {
195 using (ZipOutputStream s = new ZipOutputStream(ZipFile))
196 {
197 s.SetLevel(9);
198 ZipSetp(strDirectory, s, "", files);
199 }
200 }
201 }
202
203 /// <summary>
204 /// 递归遍历目录
205 /// </summary>
206 /// <param name="strDirectory">需遍历的目录</param>
207 /// <param name="s">压缩输出流对象</param>
208 /// <param name="parentPath">The parent path.</param>
209 /// <param name="files">需要压缩的文件</param>
210 private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath, List<string> files = null)
211 {
212 if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
213 {
214 strDirectory += Path.DirectorySeparatorChar;
215 }
216
217 string[] filenames = Directory.GetFileSystemEntries(strDirectory);
218
219 byte[] buffer = new byte[4096];
220 foreach (string file in filenames)// 遍历所有的文件和目录
221 {
222 if (files != null && !files.Contains(file))
223 {
224 continue;
225 }
226 if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
227 {
228 string pPath = parentPath;
229 pPath += Path.GetFileName(file);
230 pPath += "\\";
231 ZipSetp(file, s, pPath, files);
232 }
233 else // 否则直接压缩文件
234 {
235 //打开压缩文件
236 string fileName = parentPath + Path.GetFileName(file);
237 ZipEntry entry = new ZipEntry(fileName);
238
239 entry.DateTime = DateTime.Now;
240
241 s.PutNextEntry(entry);
242 using (FileStream fs = File.OpenRead(file))
243 {
244 int sourceBytes;
245 do
246 {
247 sourceBytes = fs.Read(buffer, 0, buffer.Length);
248 s.Write(buffer, 0, sourceBytes);
249 } while (sourceBytes > 0);
250
251 }
252 }
253 }
254 }
255
256 /// <summary>
257 /// 解压缩一个 zip 文件。
258 /// </summary>
259 /// <param name="zipedFile">压缩文件</param>
260 /// <param name="strDirectory">解压目录</param>
261 /// <param name="password">zip 文件的密码。</param>
262 /// <param name="overWrite">是否覆盖已存在的文件。</param>
263 public static void UnZip(string zipedFile, string strDirectory, bool overWrite, string password)
264 {
265
266 if (strDirectory == "")
267 strDirectory = Directory.GetCurrentDirectory();
268 if (!strDirectory.EndsWith("\\"))
269 strDirectory = strDirectory + "\\";
270
271 using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile)))
272 {
273 if (password != null)
274 {
275 s.Password = password;
276 }
277 ZipEntry theEntry;
278
279 while ((theEntry = s.GetNextEntry()) != null)
280 {
281 string directoryName = "";
282 string pathToZip = "";
283 pathToZip = theEntry.Name;
284
285 if (pathToZip != "")
286 directoryName = Path.GetDirectoryName(pathToZip) + "\\";
287
288 string fileName = Path.GetFileName(pathToZip);
289
290 Directory.CreateDirectory(strDirectory + directoryName);
291
292 if (fileName != "")
293 {
294 if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName)))
295 {
296 using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName))
297 {
298 int size = 2048;
299 byte[] data = new byte[2048];
300 while (true)
301 {
302 size = s.Read(data, 0, data.Length);
303
304 if (size > 0)
305 streamWriter.Write(data, 0, size);
306 else
307 break;
308 }
309 streamWriter.Close();
310 }
311 }
312 }
313 }
314
315 s.Close();
316 }
317 }
318
319 /// <summary>
320 /// 解压缩一个 zip 文件。
321 /// </summary>
322 /// <param name="zipedFile">压缩文件</param>
323 /// <param name="strDirectory">解压目录</param>
324 /// <param name="overWrite">是否覆盖已存在的文件。</param>
325 public static void UnZip(string zipedFile, string strDirectory, bool overWrite)
326 {
327 UnZip(zipedFile, strDirectory, overWrite, null);
328 }
329
330 /// <summary>
331 /// 解压缩一个 zip 文件。
332 /// 覆盖已存在的文件。
333 /// </summary>
334 /// <param name="zipedFile">压缩文件</param>
335 /// <param name="strDirectory">解压目录</param>
336 public static void UnZip(string zipedFile, string strDirectory)
337 {
338 UnZip(zipedFile, strDirectory, true);
339 }
340
341 /// <summary>
342 /// 获取压缩文件中指定类型的文件
343 /// </summary>
344 /// <param name="zipedFile">压缩文件</param>
345 /// <param name="fileExtension">文件类型(.txt|.exe)</param>
346 /// <returns>文件名称列表(包含子目录)</returns>
347 public static List<string> GetFiles(string zipedFile, List<string> fileExtension)
348 {
349 List<string> files = new List<string>();
350 if (!File.Exists(zipedFile))
351 {
352 //return files;
353 throw new FileNotFoundException(zipedFile);
354 }
355
356 using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile)))
357 {
358 ZipEntry theEntry;
359 while ((theEntry = s.GetNextEntry()) != null)
360 {
361 if (theEntry.IsFile)
362 {
363 //Console.WriteLine("Name : {0}", theEntry.Name);
364 if (fileExtension != null)
365 {
366 if (fileExtension.Contains(Path.GetExtension(theEntry.Name)))
367 {
368 files.Add(theEntry.Name);
369 }
370 }
371 else
372 {
373 files.Add(theEntry.Name);
374 }
375 }
376 }
377 s.Close();
378 }
379
380 return files;
381 }
382
383 /// <summary>
384 /// 获取压缩文件中的所有文件
385 /// </summary>
386 /// <param name="zipedFile">压缩文件</param>
387 /// <returns>文件名称列表(包含子目录)</returns>
388 public static List<string> GetFiles(string zipedFile)
389 {
390 return GetFiles(zipedFile, null);
391 }
392
393 }//Class_end
394
395 }