【问题标题】:Unzip a file in c# using 7z.exe使用 7z.exe 在 c# 中解压缩文件
【发布时间】:2013-05-09 15:07:39
【问题描述】:

我正在尝试从 winform 应用程序中解压缩文件。 我正在使用此代码:

string dezarhiverPath = @AppDomain.CurrentDomain.BaseDirectory + "\\7z.exe";
ProcessStartInfo pro = new ProcessStartInfo();
pro.WindowStyle = ProcessWindowStyle.Hidden;
pro.FileName = dezarhiverPath;
pro.Arguments = @" e c:\TEST.ZIP";
Process x = Process.Start(pro);
x.WaitForExit();

代码不返回错误但什么也没返回。 我也从 cmd 尝试了这个命令:

K:\>"C:\Test\7z.exe" e "c:\TEST.ZIP" 

但在 cmd 中,我收到此错误消息:

7-Zip cannot find the code that works with archives.

有人可以帮我从 c# 中解压缩一些文件吗?

谢谢!

【问题讨论】:

标签: c# unzip 7zip


【解决方案1】:

您为什么要费心尝试在外部使用 7z.exe 应用程序?这是一种非常笨拙的做法。而是使用您可以使用的众多库之一。

如果这是一个新应用程序,并且您的目标是 .NET 4.5,则新的 System.IO.Compression 命名空间有一个 ZipFile 类。

另外,SharpZipLib 是一个用于 .NET 中文件压缩的​​ GPL 库。有online samples

还可以使用 DotNetZip,它已获得 Ms-PL 许可。

【讨论】:

  • 那我会考虑使用 SharpZipLib 或 DotNetZip。 Google 提供了大量信息。
  • 对于 sharpZipLib 我应该添加一个外部引用?
  • @user1577242 在外部使用 winzip32.exe 或任何其他 zip 实用程序,当您可以以 正确 的方式轻松完成并使用库时,这简直是荒谬的。
  • 不要忘记:需要添加对 .NET 程序集 System.IO.Compression.FileSystem 的引用
【解决方案2】:

嘿,使用下面的代码,你的系统中必须有 7zip 应用程序。

  public void ExtractFile(string source, string destination)
    {
        string zPath = @"C:\Program Files\7-Zip\7zG.exe";// change the path and give yours 
        try
        {
            ProcessStartInfo pro = new ProcessStartInfo();
            pro.WindowStyle = ProcessWindowStyle.Hidden;
            pro.FileName = zPath;
            pro.Arguments = "x \"" + source + "\" -o" + destination;
            Process x = Process.Start(pro);
            x.WaitForExit();
        }
        catch (System.Exception Ex) {
          //DO logic here 
          }
    }

创建:

public void CreateZip()
{
    string sourceName = @"d:\a\example.txt";
    string targetName = @"d:\a\123.zip";
    ProcessStartInfo p = new ProcessStartInfo();
    p.FileName = @"C:\Program Files\7-Zip\7zG.exe";
    p.Arguments = "a -tgzip \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
    p.WindowStyle = ProcessWindowStyle.Hidden;
    Process x = Process.Start(p);
    x.WaitForExit();
}

【讨论】:

    【解决方案3】:

    参考以下代码:

    using System.IO.Compression;
    
    string startPath = @"c:\example\start";
    string zipPath = @"c:\example\result.zip";
    string extractPath = @"c:\example\extract";
    
    ZipFile.CreateFromDirectory(startPath, zipPath);
    
    ZipFile.ExtractToDirectory(zipPath, extractPath);
    

    参考链接:

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/849c4969-24b1-4650-88a5-5169727e527f/

    【讨论】:

    • ZIPFile 是谁?我的应用程序无法识别它。
    • 投了反对票,因为您只是从链接页面复制和粘贴信息而没有其他信息。仅链接的答案应该是 cmets。
    • 我也可以只发布代码。但是我也很注意发布它的链接,这样他就可以在那里进行进一步的讨论,并且可能会得到一些有用的东西。当然,这取决于你,谁赞成谁反对。
    • 不是这样。如果您只发布了代码而没有链接,那么这就是抄袭并有资格删除。
    • 如果该答案来自 stack ,那么它将有资格被删除。现在,您也可以根据需要报告此答案。我有信心,不会被删除。
    【解决方案4】:

    您可以使用 SevenZipSharp 库

    using (var input = File.OpenRead(lstFiles[0]))
    {
        using (var ds = new SevenZipExtractor(input))
        {
            //ds.ExtractionFinished += DsOnExtractionFinished;
    
            var mem = new MemoryStream();
            ds.ExtractFile(0, mem);
    
            using (var sr = new StreamReader(mem))
            {
                var iCount = 0;
                String line;
                mem.Position = 0;
                while ((line = sr.ReadLine()) != null && iCount < 100)
                {
                    iCount++;
                    LstOutput.Items.Add(line);
                }
    
            }
        }
    }
    

    【讨论】:

      【解决方案5】:

      试试这个

          string fileZip = @"c:\example\result.zip";
          string fileZipPathExtactx= @"c:\example\";
          ProcessStartInfo p = new ProcessStartInfo();
          p.WindowStyle = ProcessWindowStyle.Hidden;
          p.FileName = dezarhiverPath ;
          p.Arguments = "x \"" + fileZip + "\" -o" + fileZipPathExtact;
          Process x = Process.Start(p);
          x.WaitForExit();
      

      【讨论】:

        【解决方案6】:

        这也许对你有帮助。

                //You must create an empty folder to remove.
                string tempDirectoryPath = @"C:\Users\HOPE\Desktop\Test Folder\zipfolder";
                string zipFilePath = @"C:\Users\HOPE\Desktop\7za920.zip";
                Directory.CreateDirectory(tempDirectoryPath);
                ZipFile.ExtractToDirectory(zipFilePath, tempDirectoryPath);
        

        【讨论】:

          猜你喜欢
          • 2012-02-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-18
          • 2018-06-19
          • 2014-07-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多