通过Winzip及其cmd扩展名在C#中解压文件
所以我有一小段代码,可以检测文件夹中的文件,并在文件到一定年龄后系统地对其进行压缩。我现在正在编写一段代码,以便在用户请求时解压特定日期范围的文件,以便在软件中使用 我的问题是zip文件的命令行字符串工作得很好,但是解压不。。。下面是我如何解压的代码摘录,请让我知道我应该做些什么来确保解压。谢谢通过Winzip及其cmd扩展名在C#中解压文件,c#,visual-studio-2010,command-line,unzip,winzip,C#,Visual Studio 2010,Command Line,Unzip,Winzip,所以我有一小段代码,可以检测文件夹中的文件,并在文件到一定年龄后系统地对其进行压缩。我现在正在编写一段代码,以便在用户请求时解压特定日期范围的文件,以便在软件中使用 我的问题是zip文件的命令行字符串工作得很好,但是解压不。。。下面是我如何解压的代码摘录,请让我知道我应该做些什么来确保解压。谢谢 private void UnZipFile() { if (myRecord == null) { if
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时出错了?即使我在windows命令提示符下测试它,格式也是正确的
***需要注意的是,ZipInfo.FullName在formmat中的用法是:“C:\Users\16208\Software\Beta\logs\6\u 2013\Log\u 10AM\u to_11AM.zip”,因此我给出了压缩项的准确路径。您可以使用一些免费的开源.Net库进行压缩和解压缩(如SLaks所建议的)。比如说
至于您的代码,等待半秒钟可能不足以完成解压缩。您还可以尝试在命令行中运行解压命令并检查输出。如果您安装了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文件的完整路径。使用实际的C#ZIP库。@DarthSheldon有多个.NET ZIP库,如和。这两个选项都比运行WinZip更可取(尤其是因为最终用户可能没有安装WinZip)。它们会,此软件随WinZip一起提供,由于许可,我需要使用指定的库和软件。Winzip是一个允许的im,但任何其他库都必须在中创建-house@DarthSheldon-哦,好的。在这种情况下,当您直接使用
cmd
时,是否可以使用相同的命令行参数进行提取?(只是为了确保代码有问题)是的,我认为问题中提到了这一点,但我使用了相同的字符串“action”将ZipInfo.FileName替换为完整的zip名称以及要解压缩到的文件夹。基本上,我必须使用Win-zip+1:DotNetZip。它至少和WinZip或者Zip/Unzip一样快。没关系,这是一个必需的软件功能
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();