How to Unzip all .Zip file from Folder using C# 4.0 and without using any OpenSource Dll?

前端 未结 6 1431
礼貌的吻别
礼貌的吻别 2020-12-09 03:02

I have a folder containing .ZIP files. Now, I want to Extract the ZIP Files to specific folders using C#, but without using any external assembly or the .Ne

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-09 03:46

    I had the same problem and solved it by calling 7-zip executable through cmd shell from C# code, as follows,

    string zipped_path = "xxx.7z";
    string unzipped_path = "yyy";
    string arguments = "e " + zipped_path + " -o" + unzipped_path;
    
    System.Diagnostics.Process process
             = Launch_in_Shell("C:\\Program Files (x86)\\7-Zip\\","7z.exe", arguments);
    
    if (!(process.ExitCode == 0))
         throw new Exception("Unable to decompress file: " + zipped_path);
    

    And where Launch_in_Shell(...) is defined as,

    public static System.Diagnostics.Process Launch_in_Shell(string WorkingDirectory,
                                                             string FileName, 
                                                             string Arguments)
    {
           System.Diagnostics.ProcessStartInfo processInfo 
                                             = new System.Diagnostics.ProcessStartInfo();
    
            processInfo.WorkingDirectory = WorkingDirectory;
            processInfo.FileName = FileName;
            processInfo.Arguments = Arguments;
            processInfo.UseShellExecute = true;
            System.Diagnostics.Process process 
                                          = System.Diagnostics.Process.Start(processInfo);
    
            return process;
    }
    

    Drawbacks: You need to have 7zip installed in your machine and I only tried it with ".7z" files. Hope this helps.

提交回复
热议问题