C#自动解压zip文件

前提:需要引用 shell32.dll,一般dll在电脑C:Windows\System32\下面。

/// 
///解压zip文件
/// 
/// 需要解压文件(路径)
/// 解压路径
private string UnZipFile(string zipFilePath, string unZipDir)
{
string zip = string.Empty;
if (zipFilePath.Length == 0)
{
zip = "压缩文件不能为空!";
}
else if (!zipFilePath.EndsWith(".zip"))
{
zip = "文件格式不正确!";
}
else if (!File.Exists(zipFilePath))
{
zip = "压缩文件不存在!";
}
else
{
if (unZipDir.Length == 0)
{
unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), 
Path.GetFileNameWithoutExtension(zipFilePath));
}
if (!unZipDir.EndsWith("\\"))
{
unZipDir += "\\";
}
if (!Directory.Exists(unZipDir))
{
Directory.CreateDirectory(unZipDir);
}
try
{
ShellClass shellClass = new ShellClass();
Folder folder = shellClass.NameSpace(zipFilePath);
Folder folder2 = shellClass.NameSpace(unZipDir);
FolderItems folderItems = folder.Items();
folder2.CopyHere(folderItems, 20);
zip = "解压成功!";
}
catch (Exception ex)
{
zip = ex.Message;
}
}
return zip;
}

你可能感兴趣的:(C#自动解压zip文件)