<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      C#壓縮庫SharpZipLib的應(yīng)用

         SharpZipLib是一個(gè)開源的C#壓縮解壓庫,應(yīng)用非常廣泛。就像用ADO.NET操作數(shù)據(jù)庫要打開連接、執(zhí)行命令、關(guān)閉連接等多個(gè)步驟一樣,用SharpZipLib進(jìn)行壓縮和解壓也需要多個(gè)步驟。除了初學(xué)者會(huì)用原始的方法做一步一步完成,實(shí)際開發(fā)的時(shí)候都會(huì)進(jìn)行更易用的封裝。這里分享我對SharpZipLib的封裝,支持對文件和字節(jié)進(jìn)行壓縮和解壓操作,支持多個(gè)文件和文件夾壓縮,支持設(shè)置備注和密碼。(文章源網(wǎng)址:http://www.rzrgm.cn/conexpress/p/SharpZipClass.html)
          SharpZipLib的官方地址是:http://icsharpcode.github.io/SharpZipLib/,實(shí)際使用可以通過NuGet獲取,在NuGet的地址是:http://www.nuget.org/packages/SharpZipLib/
      SharpZipLib_NuGet
       
      在Visual Studio中可以通過NuGet程序包管理控制臺(tái)輸入命令PM> Install-Package SharpZipLib或者用NuGet管理界面搜索并安裝。
      我把使用SharpZipLib的方法卸載一個(gè)類里面,作為靜態(tài)方法調(diào)用,免去創(chuàng)建類的麻煩。其中核心壓縮和解壓文件的方法代碼如下:
        1         /// <summary>
        2         /// 壓縮多個(gè)文件/文件夾
        3         /// </summary>
        4         /// <param name="sourceList">源文件/文件夾路徑列表</param>
        5         /// <param name="zipFilePath">壓縮文件路徑</param>
        6         /// <param name="comment">注釋信息</param>
        7         /// <param name="password">壓縮密碼</param>
        8         /// <param name="compressionLevel">壓縮等級,范圍從0到9,可選,默認(rèn)為6</param>
        9         /// <returns></returns>
       10         public static bool CompressFile(IEnumerable<string> sourceList, string zipFilePath,
       11              string comment = null, string password = null, int compressionLevel = 6)
       12         {
       13             bool result = false;
       14 
       15             try
       16             {
       17                 //檢測目標(biāo)文件所屬的文件夾是否存在,如果不存在則建立
       18                 string zipFileDirectory = Path.GetDirectoryName(zipFilePath);
       19                 if (!Directory.Exists(zipFileDirectory))
       20                 {
       21                     Directory.CreateDirectory(zipFileDirectory);
       22                 }
       23 
       24                 Dictionary<string, string> dictionaryList = PrepareFileSystementities(sourceList);
       25 
       26                 using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipFilePath)))
       27                 {
       28                     zipStream.Password = password;//設(shè)置密碼
       29                     zipStream.SetComment(comment);//添加注釋
       30                     zipStream.SetLevel(CheckCompressionLevel(compressionLevel));//設(shè)置壓縮等級
       31 
       32                     foreach (string key in dictionaryList.Keys)//從字典取文件添加到壓縮文件
       33                     {
       34                         if (File.Exists(key))//判斷是文件還是文件夾
       35                         {
       36                             FileInfo fileItem = new FileInfo(key);
       37 
       38                             using (FileStream readStream = fileItem.Open(FileMode.Open,
       39                                 FileAccess.Read, FileShare.Read))
       40                             {
       41                                 ZipEntry zipEntry = new ZipEntry(dictionaryList[key]);
       42                                 zipEntry.DateTime = fileItem.LastWriteTime;
       43                                 zipEntry.Size = readStream.Length;
       44                                 zipStream.PutNextEntry(zipEntry);
       45                                 int readLength = 0;
       46                                 byte[] buffer = new byte[BufferSize];
       47 
       48                                 do
       49                                 {
       50                                     readLength = readStream.Read(buffer, 0, BufferSize);
       51                                     zipStream.Write(buffer, 0, readLength);
       52                                 } while (readLength == BufferSize);
       53 
       54                                 readStream.Close();
       55                             }
       56                         }
       57                         else//對文件夾的處理
       58                         {
       59                             ZipEntry zipEntry = new ZipEntry(dictionaryList[key] + "/");
       60                             zipStream.PutNextEntry(zipEntry);
       61                         }
       62                     }
       63 
       64                     zipStream.Flush();
       65                     zipStream.Finish();
       66                     zipStream.Close();
       67                 }
       68 
       69                 result = true;
       70             }
       71             catch (System.Exception ex)
       72             {
       73                 throw new Exception("壓縮文件失敗", ex);
       74             }
       75 
       76             return result;
       77         }
       78 
       79         /// <summary>
       80         /// 解壓文件到指定文件夾
       81         /// </summary>
       82         /// <param name="sourceFile">壓縮文件</param>
       83         /// <param name="destinationDirectory">目標(biāo)文件夾,如果為空則解壓到當(dāng)前文件夾下</param>
       84         /// <param name="password">密碼</param>
       85         /// <returns></returns>
       86         public static bool DecomparessFile(string sourceFile, string destinationDirectory = null, string password = null)
       87         {
       88             bool result = false;
       89 
       90             if (!File.Exists(sourceFile))
       91             {
       92                 throw new FileNotFoundException("要解壓的文件不存在", sourceFile);
       93             }
       94 
       95             if (string.IsNullOrWhiteSpace(destinationDirectory))
       96             {
       97                 destinationDirectory = Path.GetDirectoryName(sourceFile);
       98             }
       99 
      100             try
      101             {
      102                 if (!Directory.Exists(destinationDirectory))
      103                 {
      104                     Directory.CreateDirectory(destinationDirectory);
      105                 }
      106 
      107                 using (ZipInputStream zipStream = new ZipInputStream(File.Open(sourceFile, FileMode.Open,
      108                     FileAccess.Read, FileShare.Read)))
      109                 {
      110                     zipStream.Password = password;
      111                     ZipEntry zipEntry = zipStream.GetNextEntry();
      112 
      113                     while (zipEntry != null)
      114                     {
      115                         if (zipEntry.IsDirectory)//如果是文件夾則創(chuàng)建
      116                         {
      117                             Directory.CreateDirectory(Path.Combine(destinationDirectory,
      118                                 Path.GetDirectoryName(zipEntry.Name)));
      119                         }
      120                         else
      121                         {
      122                             string fileName = Path.GetFileName(zipEntry.Name);
      123                             if (!string.IsNullOrEmpty(fileName) && fileName.Trim().Length > 0)
      124                             {
      125                                 FileInfo fileItem = new FileInfo(Path.Combine(destinationDirectory, zipEntry.Name));
      126                                 using (FileStream writeStream = fileItem.Create())
      127                                 {
      128                                     byte[] buffer = new byte[BufferSize];
      129                                     int readLength = 0;
      130 
      131                                     do
      132                                     {
      133                                         readLength = zipStream.Read(buffer, 0, BufferSize);
      134                                         writeStream.Write(buffer, 0, readLength);
      135                                     } while (readLength == BufferSize);
      136 
      137                                     writeStream.Flush();
      138                                     writeStream.Close();
      139                                 }
      140                                 fileItem.LastWriteTime = zipEntry.DateTime;
      141                             }
      142                         }
      143                         zipEntry = zipStream.GetNextEntry();//獲取下一個(gè)文件
      144                     }
      145 
      146                     zipStream.Close();
      147                 }
      148                 result = true;
      149             }
      150             catch (System.Exception ex)
      151             {
      152                 throw new Exception("文件解壓發(fā)生錯(cuò)誤", ex);
      153             }
      154 
      155             return result;
      156         }
      壓縮方法CompressFile中sourceList是路徑數(shù)組,支持文件夾和文件的路徑。當(dāng)遇到文件夾時(shí)會(huì)自動(dòng)將下級文件和文件夾包含,并會(huì)檢測重復(fù)項(xiàng)。文件路徑處理的方法如下:
       1         /// <summary>
       2         /// 為壓縮準(zhǔn)備文件系統(tǒng)對象
       3         /// </summary>
       4         /// <param name="sourceFileEntityPathList"></param>
       5         /// <returns></returns>
       6         private static Dictionary<string, string> PrepareFileSystementities(IEnumerable<string> sourceFileEntityPathList)
       7         {
       8             Dictionary<string, string> fileEntityDictionary = new Dictionary<string, string>();//文件字典
       9             string parentDirectoryPath = "";
      10             foreach (string fileEntityPath in sourceFileEntityPathList)
      11             {
      12                 string path = fileEntityPath;
      13                 //保證傳入的文件夾也被壓縮進(jìn)文件
      14                 if (path.EndsWith(@"\"))
      15                 {
      16                     path = path.Remove(path.LastIndexOf(@"\"));
      17                 }
      18 
      19                 parentDirectoryPath = Path.GetDirectoryName(path) + @"\";
      20 
      21                 if (parentDirectoryPath.EndsWith(@":\\"))//防止根目錄下把盤符壓入的錯(cuò)誤
      22                 {
      23                     parentDirectoryPath = parentDirectoryPath.Replace(@"\\", @"\");
      24                 }
      25 
      26                 //獲取目錄中所有的文件系統(tǒng)對象
      27                 Dictionary<string, string> subDictionary = GetAllFileSystemEntities(path, parentDirectoryPath);
      28 
      29                 //將文件系統(tǒng)對象添加到總的文件字典中
      30                 foreach (string key in subDictionary.Keys)
      31                 {
      32                     if (!fileEntityDictionary.ContainsKey(key))//檢測重復(fù)項(xiàng)
      33                     {
      34                         fileEntityDictionary.Add(key, subDictionary[key]);
      35                     }
      36                 }
      37             }
      38             return fileEntityDictionary;
      39         }
      40 
      41         /// <summary>
      42         /// 獲取所有文件系統(tǒng)對象
      43         /// </summary>
      44         /// <param name="source">源路徑</param>
      45         /// <param name="topDirectory">頂級文件夾</param>
      46         /// <returns>字典中Key為完整路徑,Value為文件(夾)名稱</returns>
      47         private static Dictionary<string, string> GetAllFileSystemEntities(string source, string topDirectory)
      48         {
      49             Dictionary<string, string> entitiesDictionary = new Dictionary<string, string>();
      50             entitiesDictionary.Add(source, source.Replace(topDirectory, ""));
      51 
      52             if (Directory.Exists(source))
      53             {
      54                 //一次性獲取下級所有目錄,避免遞歸
      55                 string[] directories = Directory.GetDirectories(source, "*.*", SearchOption.AllDirectories);
      56                 foreach (string directory in directories)
      57                 {
      58                     entitiesDictionary.Add(directory, directory.Replace(topDirectory, ""));
      59                 }
      60 
      61                 string[] files = Directory.GetFiles(source, "*.*", SearchOption.AllDirectories);
      62                 foreach (string file in files)
      63                 {
      64                     entitiesDictionary.Add(file, file.Replace(topDirectory, ""));
      65                 }
      66             }
      67 
      68             return entitiesDictionary;
      69         }

      除了支持文件和文件夾壓縮解壓,還提供了對字節(jié)的壓縮解壓方法:

        1         /// <summary>
        2         /// 壓縮字節(jié)數(shù)組
        3         /// </summary>
        4         /// <param name="sourceBytes">源字節(jié)數(shù)組</param>
        5         /// <param name="compressionLevel">壓縮等級</param>
        6         /// <param name="password">密碼</param>
        7         /// <returns>壓縮后的字節(jié)數(shù)組</returns>
        8         public static byte[] CompressBytes(byte[] sourceBytes, string password = null, int compressionLevel = 6)
        9         {
       10             byte[] result = new byte[] { };
       11 
       12             if (sourceBytes.Length > 0)
       13             {
       14                 try
       15                 {
       16                     using (MemoryStream tempStream = new MemoryStream())
       17                     {
       18                         using (MemoryStream readStream = new MemoryStream(sourceBytes))
       19                         {
       20                             using (ZipOutputStream zipStream = new ZipOutputStream(tempStream))
       21                             {
       22                                 zipStream.Password = password;//設(shè)置密碼
       23                                 zipStream.SetLevel(CheckCompressionLevel(compressionLevel));//設(shè)置壓縮等級
       24 
       25                                 ZipEntry zipEntry = new ZipEntry("ZipBytes");
       26                                 zipEntry.DateTime = DateTime.Now;
       27                                 zipEntry.Size = sourceBytes.Length;
       28                                 zipStream.PutNextEntry(zipEntry);
       29                                 int readLength = 0;
       30                                 byte[] buffer = new byte[BufferSize];
       31 
       32                                 do
       33                                 {
       34                                     readLength = readStream.Read(buffer, 0, BufferSize);
       35                                     zipStream.Write(buffer, 0, readLength);
       36                                 } while (readLength == BufferSize);
       37 
       38                                 readStream.Close();
       39                                 zipStream.Flush();
       40                                 zipStream.Finish();
       41                                 result = tempStream.ToArray();
       42                                 zipStream.Close();
       43                             }
       44                         }
       45                     }
       46                 }
       47                 catch (System.Exception ex)
       48                 {
       49                     throw new Exception("壓縮字節(jié)數(shù)組發(fā)生錯(cuò)誤", ex);
       50                 }
       51             }
       52 
       53             return result;
       54         }
       55 
       56         /// <summary>
       57         /// 解壓字節(jié)數(shù)組
       58         /// </summary>
       59         /// <param name="sourceBytes">源字節(jié)數(shù)組</param>
       60         /// <param name="password">密碼</param>
       61         /// <returns>解壓后的字節(jié)數(shù)組</returns>
       62         public static byte[] DecompressBytes(byte[] sourceBytes, string password = null)
       63         {
       64             byte[] result = new byte[] { };
       65 
       66             if (sourceBytes.Length > 0)
       67             {
       68                 try
       69                 {
       70                     using (MemoryStream tempStream = new MemoryStream(sourceBytes))
       71                     {
       72                         using (MemoryStream writeStream = new MemoryStream())
       73                         {
       74                             using (ZipInputStream zipStream = new ZipInputStream(tempStream))
       75                             {
       76                                 zipStream.Password = password;
       77                                 ZipEntry zipEntry = zipStream.GetNextEntry();
       78 
       79                                 if (zipEntry != null)
       80                                 {
       81                                     byte[] buffer = new byte[BufferSize];
       82                                     int readLength = 0;
       83 
       84                                     do
       85                                     {
       86                                         readLength = zipStream.Read(buffer, 0, BufferSize);
       87                                         writeStream.Write(buffer, 0, readLength);
       88                                     } while (readLength == BufferSize);
       89 
       90                                     writeStream.Flush();
       91                                     result = writeStream.ToArray();
       92                                     writeStream.Close();
       93                                 }
       94                                 zipStream.Close();
       95                             }
       96                         }
       97                     }
       98                 }
       99                 catch (System.Exception ex)
      100                 {
      101                     throw new Exception("解壓字節(jié)數(shù)組發(fā)生錯(cuò)誤", ex);
      102                 }
      103             }
      104             return result;
      105         }
      View Code

      為了測試該類,我寫了一個(gè)WinForm程序,界面如下:

      SharpZipLib測試界面
      可以將文件和文件夾拖放到壓縮列表中,選中壓縮列表中的對象可以按Delete鍵進(jìn)行刪除。文件解壓區(qū)域也可以將要解壓的文件拖放到文件路徑輸入文本框。實(shí)際測試可以正確壓縮和解壓,包括設(shè)置密碼,壓縮和解壓操作和WinRAR完全兼容。如果您在代碼中發(fā)現(xiàn)問題或有可以優(yōu)化完善的地方,歡迎指出,謝謝!
      程序源代碼下載:https://files.cnblogs.com/files/conexpress/SharpZipTest.zip
       
      posted @ 2016-08-25 12:03  Alex Leo  閱讀(13224)  評論(3)    收藏  舉報(bào)
      主站蜘蛛池模板: A男人的天堂久久A毛片| 在线欧美精品一区二区三区| 国产精品va在线观看无码不卡| 国语精品一区二区三区| 日韩卡一卡2卡3卡4卡| 精品人妻av区乱码| 少妇高潮水多太爽了动态图| 日韩中文字幕免费在线观看| 欧美老人巨大XXXX做受视频| 我要看亚洲黄色太黄一级黄| 五月天天天综合精品无码| 国产女人18毛片水真多1| 亚洲乱色伦图片区小说| 国产精品自拍实拍在线看| 亚洲AV成人片不卡无码| 日韩精品一区二区三区影院| 国产精品综合一区二区三区| 亚洲综合一区二区三区| 精品午夜福利无人区乱码| 麻豆国产va免费精品高清在线| 日韩中文字幕精品人妻| 少妇人妻偷人精品免费| 国产亚洲无日韩乱码| 久久久久久国产精品美女| 中文人妻无码一区二区三区在线 | 国产天美传媒性色av高清| 污网站大全免费| 日韩av毛片福利国产福利| 欧美白妞大战非洲大炮| 久久精品手机观看| 国产精品 亚洲一区二区三区| 欧美人成精品网站播放| 亚洲av熟女国产一二三| 国产欧美日韩精品丝袜高跟鞋| 精品一区二区中文字幕| 亚洲精品国产无套在线观| 久久亚洲精品日本波多野结衣| 国产一区二区亚洲一区二区三区 | 亚洲国产一区二区三区最新| 欧洲人妻丰满av无码久久不卡| 午夜福利国产区在线观看|