Unzip a file in c# using 7z.exe

前端 未结 6 987
被撕碎了的回忆
被撕碎了的回忆 2020-12-12 05:31

I\'m trying to unzip a file from a winform application. I\'m using this code :

string dezarhiverPath = @AppDomain.CurrentDomain.BaseDirectory + \"\\\\7z.exe\         


        
相关标签:
6条回答
  • 2020-12-12 05:52

    Hey use this code below , you must have 7zip application in your system .

      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 
              }
        }
    

    to create :

    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();
    }
    
    0 讨论(0)
  • 2020-12-12 06:02

    Refer Following Code:

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

    Referance Link:

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

    0 讨论(0)
  • 2020-12-12 06:04

    This maybe can help you.

            //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);
    
    0 讨论(0)
  • 2020-12-12 06:12

    Try this

        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();
    
    0 讨论(0)
  • 2020-12-12 06:13

    Why would you bother trying to use the 7z.exe application externally? That is a very kludgy way of doing it. Instead use one of the many libraries at your disposal.

    If this is a new application, and you are targeting .NET 4.5, The new System.IO.Compression namespace has a ZipFile class.

    Alternatively, SharpZipLib is a GPL library for file compression in .NET. There are online samples.

    Also available is DotNetZip which is Ms-PL licensed.

    0 讨论(0)
  • 2020-12-12 06:17

    You can use SevenZipSharp library

    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);
                }
    
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题