DotNetZip 加密壓縮

摘要:DotNetZip 加密壓縮

Class Zip

01 using Ionic.Zip;
02
03 namespace WindowsFormsApplication1
04 {
05     class zip
06     {
07           public void  CompressFilesWithZip(List<string> sourceFiles, string zipFile, string passWord)
08         {
09             using (ZipFile zip = new ZipFile(zipFile))

10             {
11                 zip.Password = passWord == string.Empty ? string.Empty : passWord;  //設定密碼
12                 try
13                 {
14                     foreach (string detail in sourceFiles)
15                     {
16                         zip.AddFile(detail);
17                     }

18                 }

19                 catch { }
20                 finally
21                 {
22                     zip.Save();
23                     zip.Dispose();
24                 }

25             }

26         }

27
28     }

29 }

最後在button Click內貼上

01   
02 DirectoryInfo thisdir = new DirectoryInfo(@"c:\DD\")  ;//監看C內的DD資料夾
03   FileInfo[] filelist = thisdir.GetFiles();                                //塞DD內檔案資料
04   List<string> filelist_al = new List<string>();                    //準備塞到list宣告
05    foreach (FileInfo dir in filelist)
06     {
07                 filelist_al.Add(dir.FullName.ToString());      // 塞資料......
08      }

09     zip zi = new zip();
10     zi.CompressFilesWithZip(filelist_al, "檔案名稱.zip", "密碼");

 

如果想結合上次mail 夾帶完檔案資料後,刪除這個ZIP檔案的話   http://www.dotblogs.com.tw/joe80075/archive/2010/09/03/17542.aspx

補上釋放,不然處理程序會被佔住,因而不能刪除

1 MySmtp.Send(mms);//發送之後補上釋放
2       mms.Attachments.Dispose();

參考Will 保哥的

http://blog.miniasp.com/post/2009/01/11/Introduce-SharpZipLib-and-DotNetZip-Library-for-NET.aspx

 

 

 

 

大家一起加入blogads 賺零用錢!!