C# 解压缩文件

  • Post author:
  • Post category:其他


使用SharpZipLib程序集,解压Zip文件;使用库动态库为ICSharpCode.SharpZipLib.dll

zip是一种免费开源的压缩格式,windows平台自带zip压缩和解压工具,由于算法是开源的,所以基于zip的解压缩开源库也很多,SharpZipLib是一个很不错的C#库,它能够解压缩zip、gzip和tar格式的文件,首先下载SharpZipLib解压后,在您的项目中引用ICSharpCode.SharpZLib.dll程序集即可,或者在Nuget包管理器中下载并应用至当前项目。

解压代码示例

/// <param name="GzipFile">压缩包文件名</param>    
/// <param name="targetPath">解压缩目标路径</param>           
public void  ZipDecompress(string GzipFile, string targetPath)
{  
    string directoryName = targetPath;
    if (!Directory.Exists(directoryName))
        Directory.CreateDirectory(directoryName);//生成解压目录    
    string CurrentDirectory = directoryName;
    byte[] data = new byte[2048];
    int size = 2048;
    ZipEntry theEntry = null;
    using (ZipInputStream s = new ZipInputStream(File.OpenRead(GzipFile)))
    {
        while ((theEntry = s.GetNextEntry()) != null)
        {
            if (theEntry.IsDirectory)
            {// 该结点是目录    
                if (!Directory.Exists(CurrentDirectory + theEntry.Name))
                    Directory.CreateDirectory(CurrentDirectory + theEntry.Name);
            }
            else
            {
                if (theEntry.Name != String.Empty)
                {
                    //  检查多级目录是否存在  
                    if (theEntry.Name.Contains("//"))
                    {
                        string parentDirPath = theEntry.Name.Remove(theEntry.Name.LastIndexOf("//") + 1);
                        if (!Directory.Exists(parentDirPath))
                        {
                            Directory.CreateDirectory(CurrentDirectory + parentDirPath);
                        }
                    }
                    string saveFile = Path.Combine(CurrentDirectory, theEntry.Name);
                    string root = Path.GetDirectoryName(saveFile);
                    if (!Directory.Exists(root))
                    {
                        Directory.CreateDirectory(root);
                    }
                    //解压文件到指定的目录    
                    using (FileStream streamWriter = File.Create(saveFile))
                    {
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size <= 0) break;
                            streamWriter.Write(data, 0, size);
                        }
                        streamWriter.Close();
                    }
                }
            }
        }
        s.Close();
    }
}

调用示例

int upgrade = 0;
if (m_UpgradeAppList.Any())//m_UpgradeAppList程序列表
{
    App.Current.Dispatcher.InvokeAsync(() =>
    {
        txt_TotalAppNum.Text = m_UpgradeAppList.Count.ToString();
    });
    m_UpgradeAppList.ForEach((p) =>
   {
       try
       {
           if (!p.IsUpgrade)
           {
               string local = System.IO.Path.Combine(Environment.CurrentDirectory, "download", System.IO.Path.GetFileName(p.ZipPath));//ZipPath地址名称,如RightManager_1.3.0.0.zip
               if (!p.IsDownload)
               {
                   //CommonParams.FTP_ADDR = @"ftp://172.16.1.13/";
                   string src = CommonParams.FTP_ADDR + "/" + p.ZipPath;//Ftp 下载地址
                   m_MainVm.FTP_Download(CommonParams.FTP_USERNAME, CommonParams.FTP_PASSWORD, src, local);//参阅其他笔记
                   p.IsDownload = true;
               }
               string zipDir = System.IO.Path.Combine(Environment.CurrentDirectory, p.RelDirPath);
               App.Current.Dispatcher.InvokeAsync(() =>
            {
                txt_AppName.Text = p.AppName;//p.AppName程序名 如RightManager
            });
               m_MainVm.ZipDecompress(local, zipDir);
               p.IsUpgrade = true;
               if (File.Exists(local))
                   File.Delete(local);
               upgrade++;
               App.Current.Dispatcher.InvokeAsync(() =>
            {
                txt_CurAppNum.Text = upgrade.ToString();//更新解压数目
            });
           }
       }
       catch (Exception ex)
       {
           
       }
   });
}


**************************************************************************************************************



版权声明:本文为BYH371256原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。