标签:
今天记压缩解压缩的使用,是基于开源项目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的压缩解压缩
标签:
原文地址:http://www.cnblogs.com/Joy-et/p/4423654.html