首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过Winzip及其命令扩展在C#中解压文件

通过Winzip及其命令扩展在C#中解压文件
EN

Stack Overflow用户
提问于 2013-07-03 02:15:55
回答 2查看 3.1K关注 0票数 0

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

我的问题是,压缩文件的命令行字符串工作得很好,但是解压却不行……下面是我如何解压的代码摘录,请让我知道我应该做什么不同,以确保解压。谢谢!

代码语言:javascript
复制
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“相同的东西,所以我给出了压缩项的准确路径。

EN

回答 2

Stack Overflow用户

发布于 2013-07-03 02:23:00

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

代码语言:javascript
复制
    using (ZipFile decompress = ZipFile.Read(ZipFilePath))
    {    
        foreach (ZipEntry e in decompress)
        {
            e.Extract(TargetPath, ExtractExistingFileAction.OverwriteSilently);
        }
    }

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

票数 0
EN

Stack Overflow用户

发布于 2015-03-05 23:53:10

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

代码语言:javascript
复制
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文件的完整路径。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17432893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档