0

所以我有一小段代码可以检测文件夹中的文件,并在它们达到一定年龄后系统地压缩它们。我现在正在编写一段代码,以根据用户请求解压缩特定日期范围的文件,以便在软件中使用。

我的问题是压缩文件的命令行字符串效果很好,但解压缩不...下面是显示我如何解压缩的代码摘录,请让我知道我应该做些什么不同的事情以确保解压缩。谢谢!

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 :(
                }
                ...

我的想法是我以某种方式将 cmd 称为错误string action?即使我在 Windows 命令提示符下测试它是正确的格式。

***应该注意的是 ZipInfo.FullName 是 ex: "C:\Users\16208\Software\Beta\logs\6_2013\Log_10AM_to_11AM.zip" 就格式而言,所以我给出了一个准确的路径压缩物品。

4

2 回答 2

0

您可以使用一些免费的开源 .Net 库进行压缩和解压缩(如 SLaks 建议的那样)。例如DotNetZip

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

至于您的代码,等待半秒钟可能不足以完成解压缩。您也可以尝试在命令行中运行 unzip 命令并检查输出。

于 2013-07-02T18:23:00.360 回答
0

如果您安装了 WinZip,请尝试以下操作:

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

其中 fileName 是 *.rar 文件的完整路径。

于 2015-03-05T15:53:10.313 回答