实现效果
-
本辅助类主要是用来方便实现文件(包括文件夹)压缩的辅助操作。
-
本类库使用了第三方开源控件ICSharpCode.SharpZipLib。
实现步骤
-
在代码引用相关的代码实现动态调用。
-
引用ICSharpCode.SharpZipLib.dll类库。
实现代码
1、辅助类提供的方法代码如下所示:
/// <summary>
/// 压缩文件中的文件,可设置密码
/// </summary>
public static void ZipFiles(string inputFolderPath, string outputPathAndFile, string password)
/// <summary>
/// 解压文件到指定的目录,可设置密码、删除原文件等
/// </summary>
public static void UnZipFiles(string zipPathAndFile, string outputFolder, string password, bool deleteZipFile)
/// <summary>
/// 压缩文件
/// </summary>
public static bool ZipFile(string fileToZip, string zipedFile)
/// <summary>
/// 解压缩以后的文件名和路径,压缩前的路径
/// </summary>
public static Boolean UnZipFile(string zipFile, string targetDirectory)
2、辅助类的使用例子代码如下所示
if (manifest != null)
{
string path = Path.GetFullPath(manifest.MyApplication.Location);
foreach (ManifestFile file in manifest.ManifestFiles.Files)
{
bool unzip = false;
bool.TryParse(file.Unzip, out unzip);
if (file.Source.EndsWith(".zip", StringComparison.OrdinalIgnoreCase) && unzip)
{
string zipFile = Path.Combine(path, file.Source);
try
{
ZipUtility.UnZipFiles(zipFile, path, null, true);
}
catch (Exception ex)
{
WriteLine(ex.ToString());
}
}
}
}
/// <summary>
/// 编辑状态下的数据保存
/// </summary>
/// <returns></returns>
public override bool SaveUpdated()
{
bool exist = false;
new ItemDetailServiceClient().Using(client =>
{
exist = client.CheckExist(this.txtItemNo.Text, ID);
});
if (exist)
{
MessageUtil.ShowTips("指定的备件编号已经存在,不能重复添加,请修改");
return false;
}
ItemDetailInfo info = null;
new ItemDetailServiceClient().Using(client =>
{
info = client.FindByID(ID);
});
写在最后
请关注程序员老郭,在菜单 老郭说->我的简书-->获取 ZipUtility 完整代码。