unzip file in C# via Winzip and its cmd extension

2019-08-17 04:23发布

so I have a small chunk of code that detects files in a folder and systematically zips them after they become a certain age. I'm now working on a piece of code to unzip files of a certain date-range at user request for use in the software.

My issue is that the command-line string to zip files works great, but the unzip does not... below is the excerpt of code showing how I unzip, please let me know what I should do differently to ensure unzipping. thanks!

private void UnZipFile()
        {
            if (myRecord == null)
            {
                if (File.Exists(zipInfo.FullName))
                {                        
                    Process LogUnzipper = new Process();
                    //32-bit system
                    if (File.Exists("c:\\Program Files\\WinZip\\WZZIP.exe"))
                    {
                        //WZZIP.exe being the WinZip executable
                        LogUnzipper.StartInfo.FileName = "c:\\Program Files\\WinZip\\WZZIP.exe";
                    }
                    //64-bit system
                    else if (File.Exists("c:\\Program Files (x86)\\WinZip\\WZZIP.exe"))
                    {
                        //WZZIP.exe being the WinZip executable
                        LogUnzipper.StartInfo.FileName = "c:\\Program Files (x86)\\WinZip\\WZZIP.exe";
                    }
                    //here is where I think I'm screwing up something..
                    string action = "-e " + "\"" + zipInfo.FullName + "\"" + " \"" + zipInfo.DirectoryName + "\"";
                    //happen in background
                    LogUnzipper.StartInfo.CreateNoWindow = true;
                    LogUnzipper.StartInfo.UseShellExecute = false;
                    LogUnzipper.StartInfo.Arguments = action;
                    LogUnzipper.Start();
                    while (!LogUnzipper.HasExited)
                    {
                        LogUnzipper.WaitForExit(500);// 1/2 sec
                    }
                    //adding break point at this line yields no unzipped Log file :(
                }
                ...

my thoughts are that I'm somehow calling the cmd wrong in string action? even though if I test it in a windows command prompt that's correct formatting.

***it should be noted that ZipInfo.FullName is something along the ex: "C:\Users\16208\Software\Beta\logs\6_2013\Log_10AM_to_11AM.zip" as far as formmat, so I am giving an accurate path to the zipped Item.

2条回答
狗以群分
2楼-- · 2019-08-17 04:42

You can use some free and open-source .Net library for zipping and unzipping (as SLaks suggested). For example DotNetZip.

    using (ZipFile decompress = ZipFile.Read(ZipFilePath))
    {    
        foreach (ZipEntry e in decompress)
        {
            e.Extract(TargetPath, ExtractExistingFileAction.OverwriteSilently);
        }
    }

As for your code, waiting half a second may not be enough for unzipping to complete. Also you can try running your unzip command in a command line and check the output.

查看更多
可以哭但决不认输i
3楼-- · 2019-08-17 04:44

If you have WinZip installed try this:

System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", @"/c start winzip32 -e " + fileName + @" C:\SomeDirectory");
rocStartInfo.UseShellExecute = false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();

Where fileName is the full path to your *.rar file.

查看更多
登录 后发表回答