利用ICSharpCode.SharpZipLib.dll实现简单加解压
本文主要提供了一个利用ICSharpCode.SharpZipLib.dll实现简单加解压的功能类,ICSharpCode.SharpZipLib.dll可以从http://www.icsharpcode.net/OpenSource/SharpZipLib/取得。因为现阶段只需要对文件夹进行压缩,所以提供接口的功能并不全面,以后有空或有需要的时候会进行扩充。
View Code
using System;
using System.Collections.Generic;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
namespace Common.IO
{
public static class ZipHandler
{
public static void ZipDirectory(string folderToZip, string zipedFileName)
{
ZipDirectory(folderToZip, zipedFileName, string.Empty, true, string.Empty, string.Empty, true);
}
public static void ZipDirectory(string folderToZip, string zipedFileName, string password)
{
ZipDirectory(folderToZip, zipedFileName, password, true, string.Empty, string.Empty, true);
}
/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="folderToZip">需要压缩的文件夹</param>
/// <param name="zipedFileName">压缩后的Zip完整文件名(如D:\test.zip)</param>
/// <param name="isRecurse">如果文件夹下有子文件夹,是否递归压缩</param>
/// <param name="password">解压时需要提供的密码</param>
/// <param name="fileRegexFilter">文件过滤正则表达式</param>
/// <param name="directoryRegexFilter">文件夹过滤正则表达式</param>
/// <param name="isCreateEmptyDirectories">是否压缩文件中的空文件夹</param>
public static void ZipDirectory(string folderToZip, string zipedFileName, string password, bool isRecurse, string fileRegexFilter, string directoryRegexFilter, bool isCreateEmptyDirectories)
{
FastZip fastZip = new FastZip();
fastZip.CreateEmptyDirectories = isCreateEmptyDirectories;
fastZip.Password = password;
fastZip.CreateZip(zipedFileName, folderToZip, isRecurse, fileRegexFilter, directoryRegexFilter);
}
public static void UnZipFile(string zipedFileName, string targetDirectory)
{
UnZipFile(zipedFileName, targetDirectory, string.Empty, string.Empty);
}
public static void UnZipFile(string zipedFileName, string targetDirectory, string password)
{
UnZipFile(zipedFileName, targetDirectory, password, string.Empty);
}
/// <summary>
/// 解压缩文件
/// </summary>
/// <param name="zipedFileName">Zip的完整文件名(如D:\test.zip)</param>
/// <param name="targetDirectory">解压到的目录</param>
/// <param name="password">解压密码</param>
/// <param name="fileFilter">文件过滤正则表达式</param>
public static void UnZipFile(string zipedFileName, string targetDirectory, string password, string fileFilter)
{
FastZip fastZip = new FastZip();
fastZip.Password = password;
fastZip.ExtractZip(zipedFileName, targetDirectory, fileFilter);
}
}
}
using System.Collections.Generic;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
namespace Common.IO
{
public static class ZipHandler
{
public static void ZipDirectory(string folderToZip, string zipedFileName)
{
ZipDirectory(folderToZip, zipedFileName, string.Empty, true, string.Empty, string.Empty, true);
}
public static void ZipDirectory(string folderToZip, string zipedFileName, string password)
{
ZipDirectory(folderToZip, zipedFileName, password, true, string.Empty, string.Empty, true);
}
/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="folderToZip">需要压缩的文件夹</param>
/// <param name="zipedFileName">压缩后的Zip完整文件名(如D:\test.zip)</param>
/// <param name="isRecurse">如果文件夹下有子文件夹,是否递归压缩</param>
/// <param name="password">解压时需要提供的密码</param>
/// <param name="fileRegexFilter">文件过滤正则表达式</param>
/// <param name="directoryRegexFilter">文件夹过滤正则表达式</param>
/// <param name="isCreateEmptyDirectories">是否压缩文件中的空文件夹</param>
public static void ZipDirectory(string folderToZip, string zipedFileName, string password, bool isRecurse, string fileRegexFilter, string directoryRegexFilter, bool isCreateEmptyDirectories)
{
FastZip fastZip = new FastZip();
fastZip.CreateEmptyDirectories = isCreateEmptyDirectories;
fastZip.Password = password;
fastZip.CreateZip(zipedFileName, folderToZip, isRecurse, fileRegexFilter, directoryRegexFilter);
}
public static void UnZipFile(string zipedFileName, string targetDirectory)
{
UnZipFile(zipedFileName, targetDirectory, string.Empty, string.Empty);
}
public static void UnZipFile(string zipedFileName, string targetDirectory, string password)
{
UnZipFile(zipedFileName, targetDirectory, password, string.Empty);
}
/// <summary>
/// 解压缩文件
/// </summary>
/// <param name="zipedFileName">Zip的完整文件名(如D:\test.zip)</param>
/// <param name="targetDirectory">解压到的目录</param>
/// <param name="password">解压密码</param>
/// <param name="fileFilter">文件过滤正则表达式</param>
public static void UnZipFile(string zipedFileName, string targetDirectory, string password, string fileFilter)
{
FastZip fastZip = new FastZip();
fastZip.Password = password;
fastZip.ExtractZip(zipedFileName, targetDirectory, fileFilter);
}
}
}