简体   繁体   中英

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#:

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; With 7za.exe, I get the typical command-line zip sequence, with files zipping through, adding to archive, and an archive gets created.

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 ). Try command line, with 7za, to extract - same thing.

Edit: I found the solution:

My problem was the -aoa option (which I used for overwrite) - after removing it, it worked.

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.

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.

Look here .

If the command line works, this may be the best way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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