所以我有一小段代码来检测文件夹中的文件,并在它们达到一定年龄后系统地压缩它们。我现在正在编写一段代码,根据用户的要求解压某个日期范围内的文件,以便在软件中使用。
我的问题是,压缩文件的命令行字符串工作得很好,但是解压却不行……下面是我如何解压的代码摘录,请让我知道我应该做什么不同,以确保解压。谢谢!
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 :(
}
...我的想法是,我在string action中不知何故调用了错误的命令?即使我在windows命令提示符下测试,它的格式也是正确的。
*应该注意的是,就formmat而言,ZipInfo.FullName是与ex:"C:\Users\16208\Software\Beta\logs\6_2013\Log_10AM_to_11AM.zip“相同的东西,所以我给出了压缩项的准确路径。
发布于 2013-07-03 02:23:00
您可以使用一些免费的开源.Net库来进行压缩和解压缩(按照SLaks的建议)。例如DotNetZip。
using (ZipFile decompress = ZipFile.Read(ZipFilePath))
{
foreach (ZipEntry e in decompress)
{
e.Extract(TargetPath, ExtractExistingFileAction.OverwriteSilently);
}
}至于您的代码,等待半秒可能不足以完成解压缩。您还可以尝试在命令行中运行unzip命令并检查输出。
发布于 2015-03-05 23:53:10
如果您安装了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文件的完整路径。
https://stackoverflow.com/questions/17432893
复制相似问题