自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#中關(guān)于zip壓縮解壓幫助類的封裝

開發(fā) 后端
C#作為高級語言,其強大的類庫和第三方提供的類庫??梢宰龊芏嗍虑?。但也有弊端,用第三方類庫性能不是很高。我壓縮幾百M的東西。cpu瞬間跑到50%多。比360壓縮和zip壓縮性能差遠了。所以此類也就適用壓縮比較小的東西。

之前一個同學(xué)問了這個問題后,看了園子里其它園友的封裝,都很零碎,調(diào)用也不是很方便。所以自己就封裝了一個zip解壓的類。后來想整理下怕自己忘了。就把壓縮的類也一并封裝了。

c#下壓縮解壓,主要是用第三方類庫進行封裝的。ICSharpCode.SharpZipLib.dll類庫,鏈接地址為你官方下載鏈接。壓縮主要是用流的方式進行壓縮的。

壓縮文件及文件夾。文件壓縮很簡單,把待壓縮的文件用流的方式讀到內(nèi)存中,然后放到壓縮流中。就可以了。文件夾就稍微麻煩下了。因為要把待壓縮的文件夾解壓后保留文件夾文件的層次結(jié)構(gòu)。所以我的實現(xiàn)方式就是 遞歸遍歷文件夾中的文件。計算其相對位置放到壓縮流中。

代碼如下

  1. /// <summary>  
  2.         /// 壓縮文件或者文件夾  
  3.         /// </summary>  
  4.         /// <param name="_depositPath">壓縮后文件的存放路徑   如C:\\windows\abc.zip</param>  
  5.         /// <returns></returns>  
  6.         public bool CompressionZip(string _depositPath)  
  7.         {  
  8.             bool result = true;  
  9.             FileStream fs = null;  
  10.             try 
  11.             {  
  12.                 ZipOutputStream ComStream = new ZipOutputStream(File.Create(_depositPath));  
  13.                 ComStream.SetLevel(9);      //壓縮等級  
  14.                 foreach (string path in AbsolutePaths)  
  15.                 {  
  16.                     //如果是目錄  
  17.                     if (Directory.Exists(path))  
  18.                     {  
  19.                         ZipFloder(path, ComStream, path);  
  20.                     }  
  21.                     else if (File.Exists(path))//如果是文件  
  22.                     {  
  23.                          fs = File.OpenRead(path);  
  24.                         byte[] bts = new byte[fs.Length];  
  25.                         fs.Read(bts, 0, bts.Length);  
  26.                         ZipEntry ze = new ZipEntry(new FileInfo(path).Name);  
  27.                         ComStream.PutNextEntry(ze);             //為壓縮文件流提供一個容器  
  28.                         ComStream.Write(bts, 0, bts.Length);  //寫入字節(jié)  
  29.                     }  
  30.                 }  
  31.                 ComStream.Finish(); // 結(jié)束壓縮  
  32.                 ComStream.Close();  
  33.             }  
  34.             catch (Exception ex)  
  35.             {  
  36.                 if (fs != null)  
  37.                 {  
  38.                     fs.Close();  
  39.                 }  
  40.                 errorMsg = ex.Message;  
  41.                 result = false;  
  42.             }  
  43.             return result;  
  44.         }  
  45.         //壓縮文件夾  
  46.         private void ZipFloder(string _OfloderPath, ZipOutputStream zos, string _floderPath)  
  47.         {  
  48.             foreach (FileSystemInfo item in new DirectoryInfo(_floderPath).GetFileSystemInfos())  
  49.             {  
  50.                 if (Directory.Exists(item.FullName))  
  51.                 {  
  52.                     ZipFloder(_OfloderPath, zos, item.FullName);  
  53.                 }  
  54.                 else if (File.Exists(item.FullName))//如果是文件  
  55.                 {  
  56.                     DirectoryInfo ODir = new DirectoryInfo(_OfloderPath);  
  57.                     string fullName2 = new FileInfo(item.FullName).FullName;  
  58.                     string path = ODir.Name + fullName2.Substring(ODir.FullName.Length, fullName2.Length - ODir.FullName.Length);//獲取相對目錄  
  59.                     FileStream fs = File.OpenRead(fullName2);  
  60.                     byte[] bts = new byte[fs.Length];  
  61.                     fs.Read(bts, 0, bts.Length);  
  62.                     ZipEntry ze = new ZipEntry(path);  
  63.                     zos.PutNextEntry(ze);             //為壓縮文件流提供一個容器  
  64.                     zos.Write(bts, 0, bts.Length);  //寫入字節(jié)  
  65.                 }  
  66.             }  
  67.         } 

關(guān)于解壓  解壓就簡單多了。有文件解壓文件,有文件夾 遍歷,解壓其中的文件。解壓的文件中已經(jīng)包含了其與文件夾的層次關(guān)系。

  1. /// <summary>  
  2.         /// 解壓  
  3.         /// </summary>  
  4.         /// <param name="_depositPath">壓縮文件路徑</param>  
  5.         /// <param name="_floderPath">解壓的路徑</param>  
  6.         /// <returns></returns>  
  7.         public bool DeCompressionZip(string _depositPath, string _floderPath)  
  8.         {  
  9.             bool result = true;  
  10.             FileStream fs=null;  
  11.             try 
  12.             {  
  13.                 ZipInputStream InpStream = new ZipInputStream(File.OpenRead(_depositPath));  
  14.                 ZipEntry ze = InpStream.GetNextEntry();//獲取壓縮文件中的每一個文件  
  15.                 Directory.CreateDirectory(_floderPath);//創(chuàng)建解壓文件夾  
  16.                 while (ze != null)//如果解壓完ze則是null  
  17.                 {  
  18.                     if (ze.IsFile)//壓縮zipINputStream里面存的都是文件。帶文件夾的文件名字是文件夾\\文件名  
  19.                     {  
  20.                         string[] strs=ze.Name.Split('\\');//如果文件名中包含’\\‘則表明有文件夾  
  21.                         if (strs.Length > 1)  
  22.                         {  
  23.                             //兩層循環(huán)用于一層一層創(chuàng)建文件夾  
  24.                             for (int i = 0; i < strs.Length-1; i++)  
  25.                             {  
  26.                                 string floderPath=_floderPath;  
  27.                                 for (int j = 0; j < i; j++)  
  28.                                 {  
  29.                                     floderPath = floderPath + "\\" + strs[j];  
  30.                                 }  
  31.                                 floderPath=floderPath+"\\"+strs[i];  
  32.                                 Directory.CreateDirectory(floderPath);  
  33.                             }  
  34.                         }  
  35.                          fs = new FileStream(_floderPath+"\\"+ze.Name, FileMode.OpenOrCreate, FileAccess.Write);//創(chuàng)建文件  
  36.                         //循環(huán)讀取文件到文件流中  
  37.                         while (true)  
  38.                         {  
  39.                             byte[] bts = new byte[1024];  
  40.                            int i= InpStream.Read(bts, 0, bts.Length);  
  41.                            if (i > 0)  
  42.                            {  
  43.                                fs.Write(bts, 0, i);  
  44.                            }  
  45.                            else 
  46.                            {  
  47.                                fs.Flush();  
  48.                                fs.Close();  
  49.                                break;  
  50.                            }  
  51.                         }  
  52.                     }  
  53.                     ze = InpStream.GetNextEntry();  
  54.                 }  
  55.             }  
  56.             catch (Exception ex)  
  57.             {  
  58.                 if (fs != null)  
  59.                 {  
  60.                     fs.Close();  
  61.                 }  
  62.                 errorMsg = ex.Message;  
  63.                 result = false;  
  64.             }  
  65.             return result;  
  66.         } 

最后做個總結(jié)。C#作為高級語言,其強大的類庫和第三方提供的類庫??梢宰龊芏嗍虑?。但也有弊端,用第三方類庫性能不是很高。我壓縮幾百M的東西。cpu瞬間跑到50%多。比360壓縮和zip壓縮性能差遠了。所以此類也就適用壓縮比較小的東西。

完整例子下載地址

原文鏈接:http://www.cnblogs.com/Bonker/archive/2012/12/25/2831970.html

【編輯推薦】

  1. 這也是C#代碼嗎?代碼閱讀性進階
  2. C#網(wǎng)絡(luò)編程系列一:網(wǎng)絡(luò)協(xié)議簡介
  3. C#網(wǎng)絡(luò)編程系列二:HTTP協(xié)議詳解
  4. C#網(wǎng)絡(luò)編程系列三:自定義Web服務(wù)器
  5. C#網(wǎng)絡(luò)編程系列四:自定義Web瀏覽器
責(zé)任編輯:張偉 來源: 博客園
相關(guān)推薦

2011-08-15 14:07:53

Objective-C解壓縮ZIP文件

2012-05-10 09:43:28

2011-12-30 11:14:41

Javazip

2009-08-17 08:42:00

C#文件存儲管理

2009-08-17 13:26:16

壓縮備份C#工程

2021-02-06 10:27:45

C#函數(shù)參數(shù)

2009-06-24 17:32:40

動態(tài)加載AppDoma

2023-01-30 09:04:56

Linux命令unzip

2010-01-15 18:35:25

C++的類

2021-04-14 06:53:52

C# 修飾符 Public

2009-08-24 14:30:49

C# WMI封裝

2010-06-01 13:32:15

Visual Stud

2025-02-07 08:47:38

C#派生類接口

2015-04-08 15:40:53

php在線解壓解壓zip文件

2009-08-04 17:08:12

C# Thread類

2009-08-03 18:12:31

C#抽象類

2009-08-27 16:18:47

C#類C#結(jié)構(gòu)體

2009-08-11 13:07:26

C#類庫中添加Web

2024-04-01 13:05:13

C++接口類開發(fā)

2018-09-14 16:18:26

Linux壓縮文件應(yīng)用程序
點贊
收藏

51CTO技術(shù)棧公眾號