【问题标题】:c# create 7z archive, then Can not open file "name.7z" as an archivec# create 7z archive, then Can not open file "name.7z" as an archive
【发布时间】:2012-07-19 01:20:15
【问题描述】:

我正在尝试压缩一些文件夹。它们有不同的路径,不会属于同一个目录。

我测试了我要给出的命令行参数,它可以工作,但我无法在 c# 中让它工作:

string destination = "some path\\name.7z";
string pathToZip = "path to zip\\7z.exe";  // or 7za.exe
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = pathToZip;
p.Arguments = "a \"" + destination + "\" \"";
// room for the foreach - but even one directory doesn't work right now
   p.Arguments += directoryPath + "\" \"";
p.Arguments += "\" -mx=9 -aoa";
Process x = Process.Start(p);

使用 7z.exe 我会眨眼;使用 7za.exe,我获得了典型的命令行 zip 序列,文件通过压缩,添加到存档,然后创建存档。

然后我转到它并右键单击、打开或双击...我知道它是一个无效的存档 (Can not open file "name.7z" as an archive)。尝试使用 7za 的命令行来提取 - 同样的东西。

编辑:我找到了解决方案:

我的问题是 -aoa 选项(我用于覆盖) - 删除它后,它起作用了。

【问题讨论】:

  • 无论你用 p.Arguments 做什么都很糟糕,没有人能读懂它而不让眼睛流血!
  • 同意。我在这里推荐StringBuilder,因为您在for/foreach 循环中进行字符串连接。

标签: c# archive 7zip


【解决方案1】:

此代码适用于我,将包含文件的目录打包:

string destination = @"c:\my test.7z";
string pathToZip = @"C:\Program Files\7-Zip\7z.exe";
string directoryPath = @"c:\my test";

ProcessStartInfo p = new ProcessStartInfo();
p.FileName = pathToZip;
p.Arguments = string.Format("a -mx=9 \"{0}\" \"{1}\"", destination, directoryPath);

Process x = Process.Start(p);

【讨论】:

  • 别忘了添加" 来处理路径中的空格。
  • 确实,我忘了:)。我相信作者的问题是参数的顺序:第一个选项,然后是开关,而不是存档名称和目录/文件路径。
  • 我的问题是 -aoa 选项(我用于覆盖)!在使用了您的确切语法并查看它仍然无法正常工作后,我将其删除并成功了。
【解决方案2】:

如果命令行有效,也许只是使用不同的启动函数;带exe路径的那个,第二个参数中的命令行参数。

Look here.

如果命令行有效,这可能是最好的方法。

【讨论】:

    【解决方案3】:

    7za.exe 是命令行程序,你应该在这个例子中使用它。

    为什么要在命令行中添加""?这可能会导致您的问题。

    另外,确保你把你的"放在东西周围,不要在最后填充两个,这只会导致问题。

    【讨论】:

      猜你喜欢
      • 2012-06-20
      • 2022-12-02
      • 1970-01-01
      • 2020-02-09
      • 2015-08-06
      • 2015-06-13
      • 2011-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多