首页 >> 大全

C#使用Expand、Shell32解压Cab、XSN文件

2023-10-15 大全 34 作者:考证青年

前言:

需要解压表单的xsn文件,在项目中以前使用的是命令行解压,都没有出过问题,近段时间项目中突然报错解压失败,通过分析解压操作得出结论:

1.正常正常情况下,命令行解压没有任何问题,同一个站点,相同的请求,随机出现解压失败的错误。而且最容易复现的情况为:高频率刷新页面。

2.监视解压的目标目录,解压失败的时候,目录没有任何变化。而解压成功时,目录监视则正常。

然后将命令放到bat文件中,在bat文件中,执行命令之前,先执行 “md” 命令创建随机目录,C#代码代码执行bat命令,发现在解压失败的时候,bat命令即使执行完成,目录监视也没有发现md命令创建的目录。只能猜测C#在执行命令行的时候,某些情况下会存在不同步的情况。

也没有时间专门去研究这个同步的问题,项目中有使用C#调用COM组件的地方,然后去网上搜了一下COM组件解压的cab文件的资料,发现使用进行解压则没有问题。只是需要注意添加引用的方式:

1.添加“ Shell And ” 引用,如下图所示:

c#解压7z文件_解压文件使用了未知方式_

_解压文件使用了未知方式_c#解压7z文件

2.生成项目,在bin目录下会生成“..dll”程序集,拷贝到其他目录,然后移除对的引用:

_解压文件使用了未知方式_c#解压7z文件

3.添加对“..dll”程序集的引用,然后效果如下图所示:

c#解压7z文件_解压文件使用了未知方式_

至于为什么要进行上述操作,是因为:直接添加对“ Shell...”的引用,代码生成之后在其他系统可能无法正常调用,如Win 2003 生成的无法在上使用,但是通过上述方式引用之后,则可以了了。这样就可以正常使用Shell进行操作了。进行Shell操作的资料可以参考:

_解压文件使用了未知方式_c#解压7z文件

最终代码整理如下:代码中也包括cmd命令行的方式,在此供参考。

代码:

public partial class Extract : System.Web.UI.Page{/// /// 要解压的文件名称/// private String XSNFileName = @"infopath.xsn";/// /// 解压到....  的目标路径/// private String TargetDirectory = @"C:\xsn";/// /// cab文件名称/// private String CabFileName = "cab.cab";protected void Page_Load(object sender, EventArgs e){//使用cmd命令解压this.ExtractByCmd();//使用shell32进行解压this.ExtractByShell();}#region cmd命令解压/// /// 使用cmd命令进行解压/// private void ExtractByCmd(){//使用cmd命令:expand  sourcefile  targetDir  -F:*    //  上面的命令得注意:目标目录不能是sourceFile的目录。System.Text.StringBuilder sbString = new System.Text.StringBuilder();String tempDir = Guid.NewGuid().ToString();System.IO.Directory.CreateDirectory(System.IO.Path.Combine(this.TargetDirectory, tempDir));String cmdString = String.Format("\"{0}\" \"{1}\" -F:*", this.XSNFileName,tempDir);using (Process process = new Process()){process.StartInfo.FileName = "expand";process.StartInfo.WorkingDirectory = this.TargetDirectory;process.StartInfo.Arguments = cmdString;process.StartInfo.RedirectStandardInput = true;process.StartInfo.RedirectStandardOutput = true;process.StartInfo.RedirectStandardError = true;process.StartInfo.UseShellExecute = false;process.Start();process.WaitForExit();//this.Response.Write(process.StandardOutput.ReadToEnd());}System.IO.DirectoryInfo tempDirectory = new System.IO.DirectoryInfo(System.IO.Path.Combine(this.TargetDirectory, tempDir));sbString.Append("使用CMD命令进行解压:已经解压的文件:
");foreach (var item in tempDirectory.GetFiles())sbString.AppendFormat("{0}
", item.Name);this.Response.Write(sbString.ToString());}#endregion#region 使用shell解压/// /// 使用Shell解压/// private void ExtractByShell(){//shell能解压zip和cab文件,xsn文件是cab格式文件,但是需要注意直接使用后缀xsn解压会失败。此时需要重命名为cab即可//shell是支持要解压的文件和目标目录相同。//1.重命名String tempString=Path.Combine(this.TargetDirectory,this.CabFileName);if (File.Exists(tempString)) File.Delete(tempString);new FileInfo(Path.Combine(this.TargetDirectory, this.XSNFileName)).CopyTo(tempString);//2.解压Shell32.ShellClass shellClass = new Shell32.ShellClass();Shell32.Folder sourceFoloder = shellClass.NameSpace(Path.Combine(this.TargetDirectory, this.CabFileName));tempString = Path.Combine(this.TargetDirectory, Guid.NewGuid().ToString());Directory.CreateDirectory(tempString);Shell32.Folder targetDir = shellClass.NameSpace(tempString);foreach (var item in sourceFoloder.Items())targetDir.CopyHere(item, 4);//各个参数的含义,参照:http://www.fluxbytes.com/csharp/unzipping-files-using-shell32-in-c/DirectoryInfo tempDire = new DirectoryInfo(tempString);System.Text.StringBuilder sbString = new System.Text.StringBuilder();sbString.Append("


使用Shell32进行解压。已经解压的文件:
");foreach (var item in tempDire.GetFiles())sbString.AppendFormat("{0}
", item.Name);this.Response.Write(sbString.ToString());}#endregion }

最终测试结果如下:

使用CMD命令进行解压:已经解压的文件:
manifest.xsf 
sampledata.xml 
schema.xsd 
template.xml 
view1.xsl 使用Shell32进行解压。已经解压的文件:
manifest.xsf 
sampledata.xml 
schema.xsd 
template.xml 
view1.xsl 

在出问题的项目服务器上,使用的方式进行xsn文件解压,测试后发现没有任何问题,即使高频率重复刷新。

以上只是项目中遇到的实际情况阐述,并不一定是最好的解决方案,如果大家更好的方案,请留言。

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了