简体   繁体   English

c#创建7z存档,然后无法打开文件“ name.7z”作为存档

[英]c# create 7z archive, then Can not open file “name.7z” as an archive

I am trying to zip some folders. 我正在尝试压缩一些文件夹。 They have different paths, will not belong to the same directory. 它们具有不同的路径,将不属于同一目录。

I tested the command line arguments that I would give, and it works, but I can't get it to work from c#: 我测试了我要给出的命令行参数,它可以工作,但是我不能从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);

With 7z.exe i get a blink; 使用7z.exe时,我会眨眼; With 7za.exe, I get the typical command-line zip sequence, with files zipping through, adding to archive, and an archive gets created. 使用7za.exe,我得到了典型的命令行zip序列,包括文件的拉链,添加到存档中以及创建了存档。

Then I go to it and right-click, open or double-click... and I get that it is an invalid archive ( Can not open file "name.7z" as an archive ). 然后,我转到它并右键单击,打开或双击...,然后得到它是无效的存档( Can not open file "name.7z" as an archive )。 Try command line, with 7za, to extract - same thing. 尝试使用7za命令行提取-同样的东西。

Edit: I found the solution: 编辑:我找到了解决方案:

My problem was the -aoa option (which I used for overwrite) - after removing it, it worked. 我的问题是-aoa选项(用于覆盖)-删除它后,它起作用了。

This code works for me, packs a directory with files within: 该代码对我有用,在目录中包含文件:

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);

7za.exe is the command line program, you should use it in this instance. 7za.exe是命令行程序,在这种情况下应使用它。

Why are you adding "" to your command line? 为什么要在命令行中添加"" That may be causing your problem. 那可能是造成您的问题。

Also, make sure you put your " around things, don't pad with two of them at the end, that just causes problems. 此外,请确保你把你的"身边事,不要垫他们两个在年底,这只是导致问题。

If the command line works, maybe just use a different start function; 如果命令行有效,则可以使用其他启动功能; the one that takes the path to the exe, and the command line parameters in the second parameter. 一个用于获取exe路径的命令,第二个参数中包含命令行参数。

Look here . 看这里

If the command line works, this may be the best way. 如果命令行有效,这可能是最好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM