c# 解压zip 进度_C#解压zip文件

因为工作中需要在C#项目中将多张图片一次上传,最近两天一直在研究,网上提供的方法很多,整理了一下,整合出一个方法,需要添加引用 Shell32.dll,可以在Windows\system32中找到它。

代码如下:

#region 解压zip格式的文件

///

/// 功能:解压zip格式的文件。

///

/// 压缩文件路径

/// 解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹

/// 出错信息

/// 解压是否成功

public bool UnZipFile(string zipFilePath, string unZipDir, out string err)

{

err = "";

if (zipFilePath.Length == 0)

{

err = "压缩文件不能为空!";

return false;

}

else if (!zipFilePath.EndsWith(".zip"))

{

err = "文件格式不正确!";

return false;

}

else if (!System.IO.File.Exists(zipFilePath))

{

err = "压缩文件不存在!";

return false;

}

//解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹

if (unZipDir.Length == 0)

unZipDir = zipFilePath.Replace(System.IO.Path.GetFileName(zipFilePath), System.IO.Path.GetFileNameWithoutExtension(zipFilePath));

if (!unZipDir.EndsWith("\\"))

unZipDir += "\\";

if (!System.IO.Directory.Exists(unZipDir))

System.IO.Directory.CreateDirectory(unZipDir);

try

{

Shell32.ShellClass sc = new Shell32.ShellClass();

Shell32.Folder SrcFolder = sc.NameSpace(zipFilePath);

Shell32.Folder DestFolder = sc.NameSpace(unZipDir);

Shell32.FolderItems items = SrcFolder.Items();

DestFolder.CopyHere(items, 20);

}

catch (Exception ex)

{

err = ex.Message;

return false;

}

return true;

}//解压结束

#endregion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值