-->

使用7z.exe解压在C#中的文件(Unzip a file in c# using 7z.exe)

2019-09-03 06:47发布

我试图解压从一个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#的一些文件?

谢谢!

Answer 1:

为什么你会打扰尝试从外部使用7z.exe应用程序? 这是做的很缺憾的方式。 而是使用了许多图书馆的一个在您的处置。

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

另外, SharpZipLib是一个GPL库在.NET文件压缩。 有网上的样品 。

同时还提供DotNetZip这是MS-PL许可。



Answer 2:

请参阅下面的代码:

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/



Answer 3:

您可以使用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);
            }

        }
    }
}


Answer 4:

试试这个

    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();


Answer 5:

嘿下面使用此代码,您必须在您的系统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();
}


Answer 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);


文章来源: Unzip a file in c# using 7z.exe
标签: c# unzip 7zip