国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

C# ICSharpCode.SharpZipLib.dll文件壓縮和解壓功能類整理,上傳文件或下載文件很常用

2019-11-17 02:20:28
字體:
來源:轉載
供稿:網友

C# ICSharpCode.SharpZipLib.dll文件壓縮和解壓功能類整理,上傳文件或下載文件很常用

工作中我們很多時候需要進行對文件進行壓縮,比較通用的壓縮的dll就是ICSharpCode.SharpZipLib.dll,廢話不多了,網上也有很多的資料,我將其最常用的兩個函數(shù)整理了一下,提供了一個通用的類,這樣在工作中可以快速的完成壓縮和解壓縮的動作哦

官網下載地址:http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

1. 在項目中添加對ICSharpCode.SharpZipLib.dll的引用;

2. 在需要使用到ICSharpCode.SharpZipLib中定義的類的編碼界面中將其導入(Imports)

 1 using ICSharpCode.SharpZipLib.Zip; 2 using System; 3 using System.IO; 4  5 namespace ZTO.WayBill.Utilities 6 { 7     /// <summary> 8     /// 壓縮類 9     /// http://www.survivalescaperooms.com/kissdodog/p/3525295.html 10 11     /// </summary>12     public class ZipHelper13     {14         /// <summary>15         /// 壓縮文件夾16         /// </summary>17         /// <param name="source">源目錄</param>18         /// <param name="s">ZipOutputStream對象</param>19         public static void ComPRess(string source, ZipOutputStream s)20         {21             string[] filenames = Directory.GetFileSystemEntries(source);22             foreach (string file in filenames)23             {24                 if (Directory.Exists(file))25                 {26                     // 遞歸壓縮子文件夾27                     Compress(file, s);28                 }29                 else30                 {31                     using (FileStream fs = File.OpenRead(file))32                     {33                         byte[] buffer = new byte[4 * 1024];34                         // 此處去掉盤符,如D:/123/1.txt 去掉D:35                         ZipEntry entry = new ZipEntry(file.Replace(Path.GetPathRoot(file), ""));36                         entry.DateTime = DateTime.Now;37                         s.PutNextEntry(entry);38                         int sourceBytes;39                         do40                         {41                             sourceBytes = fs.Read(buffer, 0, buffer.Length);42                             s.Write(buffer, 0, sourceBytes);43                         } while (sourceBytes > 0);44                     }45                 }46             }47         }48 49         /// <summary>50         /// 解壓縮51         /// </summary>52         /// <param name="sourceFile">壓縮包完整路徑地址</param>53         /// <param name="targetPath">解壓路徑是哪里</param>54         /// <returns></returns>55         public static bool Decompress(string sourceFile, string targetPath)56         {57             if (!File.Exists(sourceFile))58             {59                 throw new FileNotFoundException(string.Format("未能找到文件 '{0}' ", sourceFile));60             }61             if (!Directory.Exists(targetPath))62             {63                 Directory.CreateDirectory(targetPath);64             }65             using (var s = new ZipInputStream(File.OpenRead(sourceFile)))66             {67                 ZipEntry theEntry;68                 while ((theEntry = s.GetNextEntry()) != null)69                 {70                     if (theEntry.IsDirectory)71                     {72                         continue;73                     }74                     string directorName = Path.Combine(targetPath, Path.GetDirectoryName(theEntry.Name));75                     string fileName = Path.Combine(directorName, Path.GetFileName(theEntry.Name));76                     if (!Directory.Exists(directorName))77                     {78                         Directory.CreateDirectory(directorName);79                     }80                     if (!String.IsNullOrEmpty(fileName))81                     {82                         using (FileStream streamWriter = File.Create(fileName))83                         {84                             int size = 4096;85                             byte[] data = new byte[size];86                             while (size > 0)87                             {88                                 streamWriter.Write(data, 0, size);89                                 size = s.Read(data, 0, data.Length);90                             }91                         }92                     }93                 }94             }95             return true;96         }97     }98 }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 麦盖提县| 开鲁县| 湘潭县| 洪江市| 定南县| 镇沅| 托克逊县| 新干县| 洪雅县| 甘谷县| 清新县| 高陵县| 双城市| 前郭尔| 普兰店市| 内乡县| 弋阳县| 海兴县| 陕西省| 永年县| 石屏县| 海宁市| 弥勒县| 安国市| 婺源县| 瑞昌市| 凤凰县| 九龙坡区| 深水埗区| 英吉沙县| 精河县| 高邑县| 区。| 彭水| 太康县| 乌鲁木齐市| 镇赉县| 崇阳县| 张掖市| 牟定县| 阿坝|