unzip file in C# via Winzip and its cmd extension

两盒软妹~` 提交于 2019-12-25 18:36:34

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/17432893/unzip-file-in-c-sharp-via-winzip-and-its-cmd-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!