原文: 基于ICSharpCode.SharpZipLib.Zip的压缩解压缩

今天记压缩解压缩的使用,是基于开源项目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那样对文件进行压缩时,还要进行复杂的操作。

今天就写这么多吧。

posted on 2015-04-15 18:04 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/4429335.html

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐