ICSharpCode.SharpZipLib 壓縮、解壓文件 附源碼

http://www.icsharpcode.net/opensource/sharpziplib/ 有SharpZiplib的最新版本,本文使用的版本爲0.86.0.518,支持Zip, GZip, BZip2 和Tar格式緩存

咱們須要dll 在官網上也有,也能夠從百度網盤下載加密

好了,深刻的你們還要多多研究,今天咱們簡單介紹一下 簡單的 單文件、文件夾的壓縮和解壓spa

 

先給你們看一下效果:

 

1、引入ICSharpCode.SharpZipLib

咱們新建個幫助類 ZipHelper.cs  而後 添加 dll 引用 .net

 

2、添加完dll引用以後 咱們 須要添加 這幾個Using引用

1 using ICSharpCode.SharpZipLib.Checksums; 
2 using ICSharpCode.SharpZipLib.Zip;
3 using System;4 using System.IO;

 

3、壓縮單個文件

這裏我添加了幾個參數 你們能夠根據本身的須要 修改一下 code

 1 /// <summary>
 2         /// ZIP:壓縮單個文件
 3         /// add yuangang by 2016-06-13
 4         /// </summary>
 5         /// <param name="FileToZip">須要壓縮的文件(絕對路徑)</param>
 6         /// <param name="ZipedPath">壓縮後的文件路徑(絕對路徑)</param>
 7         /// <param name="ZipedFileName">壓縮後的文件名稱(文件名,默認 同源文件同名)</param>
 8         /// <param name="CompressionLevel">壓縮等級(0 無 - 9 最高,默認 5)</param>
 9         /// <param name="BlockSize">緩存大小(每次寫入文件大小,默認 2048)</param>
10         /// <param name="IsEncrypt">是否加密(默認 加密)</param>
11         public static void ZipFile(string FileToZip, string ZipedPath, string ZipedFileName = "", int CompressionLevel = 5, int BlockSize = 2048, bool IsEncrypt = true)
12         {
13             //若是文件沒有找到,則報錯
14             if (!System.IO.File.Exists(FileToZip))
15             {
16                 throw new System.IO.FileNotFoundException("指定要壓縮的文件: " + FileToZip + " 不存在!");
17             }
18 
19             //文件名稱(默認同源文件名稱相同)
20             string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\\" + new FileInfo(FileToZip).Name.Substring(0, new FileInfo(FileToZip).Name.LastIndexOf('.')) + ".zip" : ZipedPath + "\\" + ZipedFileName + ".zip";
21 
22             using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName))
23             {
24                 using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
25                 {
26                     using (System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read))
27                     {
28                         string fileName = FileToZip.Substring(FileToZip.LastIndexOf("\\") + 1);
29 
30                         ZipEntry ZipEntry = new ZipEntry(fileName);
31 
32                         if (IsEncrypt)
33                         {
34                             //壓縮文件加密
35                             ZipStream.Password = 「123」;
36                         }
37 
38                         ZipStream.PutNextEntry(ZipEntry);
39 
40                         //設置壓縮級別
41                         ZipStream.SetLevel(CompressionLevel);
42 
43                         //緩存大小
44                         byte[] buffer = new byte[BlockSize];
45 
46                         int sizeRead = 0;
47 
48                         try
49                         {
50                             do
51                             {
52                                 sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
53                                 ZipStream.Write(buffer, 0, sizeRead);
54                             }
55                             while (sizeRead > 0);
56                         }
57                         catch (System.Exception ex)
58                         {
59                             throw ex;
60                         }
61 
62                         StreamToZip.Close();
63                     }
64 
65                     ZipStream.Finish();
66                     ZipStream.Close();
67                 }
68 
69                 ZipFile.Close();
70             }
71         }

 

4、壓縮文件夾

 1 /// <summary>
 2         /// ZIP:壓縮文件夾
 3         /// add yuangang by 2016-06-13
 4         /// </summary>
 5         /// <param name="DirectoryToZip">須要壓縮的文件夾(絕對路徑)</param>
 6         /// <param name="ZipedPath">壓縮後的文件路徑(絕對路徑)</param>
 7         /// <param name="ZipedFileName">壓縮後的文件名稱(文件名,默認 同源文件夾同名)</param>
 8         /// <param name="IsEncrypt">是否加密(默認 加密)</param>
 9         public static void ZipDirectory(string DirectoryToZip, string ZipedPath, string ZipedFileName = "", bool IsEncrypt = true)
10         {
11             //若是目錄不存在,則報錯
12             if (!System.IO.Directory.Exists(DirectoryToZip))
13             {
14                 throw new System.IO.FileNotFoundException("指定的目錄: " + DirectoryToZip + " 不存在!");
15             }
16 
17             //文件名稱(默認同源文件名稱相同)
18             string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\\" + new DirectoryInfo(DirectoryToZip).Name + ".zip" : ZipedPath + "\\" + ZipedFileName + ".zip";
19 
20             using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName))
21             {
22                 using (ZipOutputStream s = new ZipOutputStream(ZipFile))
23                 {
24                     if (IsEncrypt)
25                     {
26                         //壓縮文件加密
27                         s.Password = 「123」;
28                     }
29                     ZipSetp(DirectoryToZip, s, "");
30                 }
31             }
32         }
33         /// <summary>
34         /// 遞歸遍歷目錄
35         /// add yuangang by 2016-06-13
36         /// </summary>
37         private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
38         {
39             if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
40             {
41                 strDirectory += Path.DirectorySeparatorChar;
42             }
43             Crc32 crc = new Crc32();
44 
45             string[] filenames = Directory.GetFileSystemEntries(strDirectory);
46 
47             foreach (string file in filenames)// 遍歷全部的文件和目錄
48             {
49 
50                 if (Directory.Exists(file))// 先看成目錄處理若是存在這個目錄就遞歸Copy該目錄下面的文件
51                 {
52                     string pPath = parentPath;
53                     pPath += file.Substring(file.LastIndexOf("\\") + 1);
54                     pPath += "\\";
55                     ZipSetp(file, s, pPath);
56                 }
57 
58                 else // 不然直接壓縮文件
59                 {
60                     //打開壓縮文件
61                     using (FileStream fs = File.OpenRead(file))
62                     {
63 
64                         byte[] buffer = new byte[fs.Length];
65                         fs.Read(buffer, 0, buffer.Length);
66 
67                         string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1);
68                         ZipEntry entry = new ZipEntry(fileName);
69 
70                         entry.DateTime = DateTime.Now;
71                         entry.Size = fs.Length;
72 
73                         fs.Close();
74 
75                         crc.Reset();
76                         crc.Update(buffer);
77 
78                         entry.Crc = crc.Value;
79                         s.PutNextEntry(entry);
80 
81                         s.Write(buffer, 0, buffer.Length);
82                     }
83                 }
84             }
85         }

 

5、解壓一個ZIP文件

 1  /// <summary>
 2         /// ZIP:解壓一個zip文件
 3         /// add yuangang by 2016-06-13
 4         /// </summary>
 5         /// <param name="ZipFile">須要解壓的Zip文件(絕對路徑)</param>
 6         /// <param name="TargetDirectory">解壓到的目錄</param>
 7         /// <param name="Password">解壓密碼</param>
 8         /// <param name="OverWrite">是否覆蓋已存在的文件</param>
 9         public static void UnZip(string ZipFile, string TargetDirectory, string Password, bool OverWrite = true)
10         {
11             //若是解壓到的目錄不存在,則報錯
12             if (!System.IO.Directory.Exists(TargetDirectory))
13             {
14                 throw new System.IO.FileNotFoundException("指定的目錄: " + TargetDirectory + " 不存在!");
15             }
16             //目錄結尾
17             if (!TargetDirectory.EndsWith("\\")) { TargetDirectory = TargetDirectory + "\\"; }
18 
19             using (ZipInputStream zipfiles = new ZipInputStream(File.OpenRead(ZipFile)))
20             {
21                 zipfiles.Password = Password;
22                 ZipEntry theEntry;
23 
24                 while ((theEntry = zipfiles.GetNextEntry()) != null)
25                 {
26                     string directoryName = "";
27                     string pathToZip = "";
28                     pathToZip = theEntry.Name;
29 
30                     if (pathToZip != "")
31                         directoryName = Path.GetDirectoryName(pathToZip) + "\\";
32 
33                     string fileName = Path.GetFileName(pathToZip);
34 
35                     Directory.CreateDirectory(TargetDirectory + directoryName);
36 
37                     if (fileName != "")
38                     {
39                         if ((File.Exists(TargetDirectory + directoryName + fileName) && OverWrite) || (!File.Exists(TargetDirectory + directoryName + fileName)))
40                         {
41                             using (FileStream streamWriter = File.Create(TargetDirectory + directoryName + fileName))
42                             {
43                                 int size = 2048;
44                                 byte[] data = new byte[2048];
45                                 while (true)
46                                 {
47                                     size = zipfiles.Read(data, 0, data.Length);
48 
49                                     if (size > 0)
50                                         streamWriter.Write(data, 0, size);
51                                     else
52                                         break;
53                                 }
54                                 streamWriter.Close();
55                             }
56                         }
57                     }
58                 }
59 
60                 zipfiles.Close();
61             }
62         }

 

原創文章 轉載請尊重勞動成果 http://yuangang.cnblogs.comblog

相關文章
相關標籤/搜索