Unzip a file in c# using 7z.exe

前端 未结 6 989
被撕碎了的回忆
被撕碎了的回忆 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();
    }
    

提交回复
热议问题