因为工作中需要在C#项目中将多张图片一次上传,最近两天一直在研究,网上提供的方法很多,整理了一下,整合出一个方法,需要添加引用 Shell32.dll,可以在Windows\system32中找到它。
代码如下:
#region 解压zip格式的文件
/// <summary>
/// 功能:解压zip格式的文件。
/// </summary>
/// <param name="zipFilePath">压缩文件路径</param>
/// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>
/// <param name="err">出错信息</param>
/// <returns>解压是否成功</returns>
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
参考:http://blog.csdn.net/ZZJ_4Ever/archive/2009/03/31/4038595.aspx;http://www.cnblogs.com/hiber/archive/2007/10/17/927795.html