DesktopHelper.Util.UnZipClass.UnZipFile C# (CSharp) Method

UnZipFile() public method

功能:解压zip格式的文件。
public UnZipFile ( string zipFilePath, string unZipDir, string &err ) : bool
zipFilePath string 压缩文件路径
unZipDir string 解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
err string 出错信息
return bool
        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 (!File.Exists(zipFilePath))
            {
                err = "压缩文件不存在!";
                return false;
            }
            //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
            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
            {
                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;
        }

Usage Example

Example #1
0
 private void UpdateAndwhoData()
 {
     try
     {
         if (File.Exists(Application.StartupPath + @"\Andwho.zip"))
         {
             UnZipClass unZip = new UnZipClass();
             string errMsg = string.Empty;
             bool success = unZip.UnZipFile(Application.StartupPath + @"\Andwho.zip", Application.StartupPath + @"\", out errMsg);
             if (success && SaveUserConfig())
             {
                 File.Delete(Application.StartupPath + @"\Data\Andwho.db");
                 File.Delete(Application.StartupPath + @"\Andwho.zip");
                 File.Move(Application.StartupPath + @"\Andwho.db", Application.StartupPath + @"\Data\Andwho.db");
             }
         }
         #region Image
         if (File.Exists(Application.StartupPath + @"\hong.png"))
         {
             File.Delete(Application.StartupPath + @"\hong.png");
         }
         if (File.Exists(Application.StartupPath + @"\huang.png"))
         {
             File.Delete(Application.StartupPath + @"\huang.png");
         }
         if (File.Exists(Application.StartupPath + @"\lv.png"))
         {
             File.Delete(Application.StartupPath + @"\lv.png");
         }
         //if (!Directory.Exists(Application.StartupPath + @"\Image"))
         //{
         //    Directory.CreateDirectory(Application.StartupPath + @"\Image");
         //}
         //if (File.Exists(Application.StartupPath + @"\green.png"))
         //{
         //    if (!File.Exists(Application.StartupPath + @"\Image\green.png"))
         //        File.Move(Application.StartupPath + @"\green.png", Application.StartupPath + @"\Image\green.png");
         //}
         //if (File.Exists(Application.StartupPath + @"\yellow.png"))
         //{
         //    if (!File.Exists(Application.StartupPath + @"\Image\yellow.png"))
         //        File.Move(Application.StartupPath + @"\yellow.png", Application.StartupPath + @"\Image\yellow.png");
         //}
         //if (File.Exists(Application.StartupPath + @"\red.png"))
         //{
         //    if (!File.Exists(Application.StartupPath + @"\Image\red.png"))
         //        File.Move(Application.StartupPath + @"\red.png", Application.StartupPath + @"\Image\red.png");
         //}
         #endregion
     }
     catch (Exception ex)
     {
         log.WriteLog(ex.ToString());
     }
 }
All Usage Examples Of DesktopHelper.Util.UnZipClass::UnZipFile
UnZipClass