基於ICSharpCode.SharpZipLib.Zip的壓縮解壓縮

來源:互聯網
上載者:User

標籤:

今天記壓縮解壓縮的使用,是基於開源項目ICSharpCode.SharpZipLib.Zip的使用。

一、壓縮:
/// <summary>        /// 壓縮        /// </summary>        /// <param name="sourceDirectory"></param>        /// <param name="targetZipName"></param>        /// <param name="recurse"></param>        /// <param name="filter"></param>        /// <returns></returns>        public static void CreateZip(string zipFileName, string sourceDirectory, bool recurse=true, string fileFilter="")        {            if (string.IsNullOrEmpty(sourceDirectory))            {                throw new ArgumentNullException("SourceZipDirectory");            }            if (string.IsNullOrEmpty(zipFileName))            {                throw new ArgumentNullException("TargetZipName");            }            if (!Directory.Exists(sourceDirectory))            {                throw new DirectoryNotFoundException("SourceDirecotry");            }            if (Path.GetExtension(zipFileName).ToUpper() != ".ZIP")                throw new ArgumentException("TargetZipName  is not zip");            FastZip fastZip = new FastZip();            fastZip.CreateZip(zipFileName, sourceDirectory, recurse, fileFilter);        }

  

二、解壓縮:
/// <summary>        /// 解壓        /// </summary>        /// <param name="zipFileName"></param>        /// <param name="targetDirectory"></param>        /// <param name="fileFilter"></param>        public static  void ExtractZip(string zipFileName, string targetDirectory, string fileFilter="")        {            if (string.IsNullOrEmpty(zipFileName))            {                throw new ArgumentNullException("ZIPFileName");            }            if (!File.Exists(zipFileName))            {                 throw new FileNotFoundException("zipFileName");            }            if (Path.GetExtension(zipFileName).ToUpper() != ".ZIP")            {                throw new ArgumentException("ZipFileName is not Zip ");            }            FastZip fastZip = new FastZip();            fastZip.ExtractZip(zipFileName, targetDirectory, fileFilter);        }
三、添加檔案至壓縮檔中
 /// <summary>        /// 添加檔案到壓縮檔中        /// </summary>        /// <param name="zipFileName"></param>        /// <param name="filesNames"></param>        public static void AddFileToZip(string zipFileName, List<string> filesNames)        {            if (string.IsNullOrEmpty(zipFileName))            {                throw new ArgumentNullException("ZipName");            }            if (!File.Exists(zipFileName))            {                throw new FileNotFoundException("ZipName");            }            if (Path.GetExtension(zipFileName).ToUpper() != ".ZIP")            {                throw new ArgumentException("ZipFileName is not Zip ");            }            if(filesNames==null||filesNames.Count<1)                return;            using (ZipFile zFile = new ZipFile(zipFileName))            {                zFile.BeginUpdate();                foreach (string fileName in filesNames)                {                    zFile.Add(fileName);                }                zFile.CommitUpdate();            }        }

 

四、移除壓縮檔中的檔案
     /// <summary>        /// 移除壓縮檔中的檔案        /// </summary>        /// <param name="zipName"></param>        /// <param name="fileNames"></param>        public static void DeleteFileFromZip(string zipFileName, IList<string> fileNames)        {            if (string.IsNullOrEmpty(zipFileName))            {                throw new ArgumentNullException("ZipName");            }            if (Path.GetExtension(zipFileName).ToUpper() != ".ZIP")            {                throw new ArgumentException("ZipName");            }            if(fileNames==null||fileNames.Count<1)            {            return ;            }            using (ZipFile zipFile = new ZipFile(zipFileName))            {                zipFile.BeginUpdate();               foreach(string fileName in fileNames)               {                zipFile.Delete(fileName);               }                zipFile.CommitUpdate();            }        }

 

 

以上是基於ICSharpCode.SharpZipLib.Zip的部分使用,當然還有許多地方需要學習的。ICSharpCode.SharpZipLib.Zip使用起來比較快速方便,不想GZip那樣對檔案進行壓縮時,還要進行複雜的操作。

今天就寫這麼多吧。

基於ICSharpCode.SharpZipLib.Zip的壓縮解壓縮

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.