自动解压zip文件

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

/// <summary>
///解压zip文件
/// </summary>
/// <param name="zipFilePath">需要解压文件(路径)</param>
/// <param name="unZipDir">解压路径</param>
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;
}
0%