从服务器端下载的C#extract .zip文件

时间:2018-05-28 14:00:04

标签: c# visual-studio

您好我必须从下载的同一路径中的服务器中提取我的程序正在下载的.zip

我使用的代码是

using (ZipFile zip = ZipFile.Read(file))
{
     foreach (ZipEntry zipFiles in zip)
     {
         zipFiles.Extract(Root + "\\Resource\\", true);
     }
}

但是我不能让程序自动提取它。我正在使用Ionic.Zip

1 个答案:

答案 0 :(得分:1)

您的代码是正确的,但您可以使用此方法

全部提取:

public void ExtractFileToDirectory(string zipFileName, string outputDirectory)
{
     ZipFile zip = ZipFile.Read(zipFileName);
     Directory.CreateDirectory(outputDirectory);
     zip.ExtractAll(outputDirectory,ExtractExistingFileAction.OverwriteSilently);
}

逐一提取:

public void ExtractFileToDirectory(string zipFileName, string outputDirectory)
{
     ZipFile zip = ZipFile.Read(zipFileName);
     Directory.CreateDirectory(outputDirectory);
      foreach (ZipEntry e in zip)
      {
          e.Extract(outputDirectory, ExtractExistingFileAction.OverwriteSilently);
      }
}

让你的outputDirectory变得正确和存在

样本用法:

    string current_directory = Path.GetDirectoryName(Application.ExecutablePath);
    string Extract_dir_name = "Extract";
    string OutoutputDirectory = current_directory + Path.DirectorySeparatorChar + Extract_dir_name;
    if (check_dir(OutoutputDirectory))
    {
        if (File.Exists(zip_file_loc))
        {
            ExtractFileToDirectory(zip_file_loc, OutoutputDirectory);
        }
        else
        {
            MessageBox.Show("File Is Not Exists");
        }
    }
    else
    {
        MessageBox.Show("Dir Is Not Exists");
    }

检查方法Dir:

    private bool check_dir(string outputDirectory)
    {
        try
        {
            if (Directory.Exists(outputDirectory))
            {
                return true;
            }
            else
            {
                Directory.CreateDirectory(outputDirectory);
                return true;
            }
        }
        catch
        {
            return false;
        }
    }