今天記壓縮解壓縮的使用,是基於開源項目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那樣對文件進行壓縮時,還要進行復雜的操作。
今天就寫這么多吧。
