1
string path = @"C:\Users\<user>\Documents\Visual Studio\Projects\7ZipFile\RequiredDocs\";
ProcessStartInfo zipper = new ProcessStartInfo(@"C:\Program Files\7-Zip\7z.exe");
zipper.Arguments = string.Format("a -t7z {0}.7z {0} *.txt -mx9", path);
zipper.RedirectStandardInput = true;
zipper.UseShellExecute = false;
zipper.CreateNoWindow = true;
zipper.WindowStyle = ProcessWindowStyle.Hidden;
Process process = Process.Start(zipper);

Goal: Zip all *.txt file(s) within "path" and save that zipped file within "path" and these .txt files should not be present in the "path" after zipping

When I run the code, nothing seems to happen (0 error)...

Please help!

Thank you

UPDATE: I am using 7Zip and have installed 7Zip application on Windows where this code will be used w/ .NET 3.5.

007
  • 2,136
  • 4
  • 26
  • 46

2 Answers2

4

The normal way of using 7Zip from a program is to invoke 7za.exe (not the installed 7z program) and include 7za with your application.

This page has a good tutorial on how to use it. Works great every time I have needed to zip/7zip programmatically.

You could also use the ZipArchive class if you want normal zip functionality in a pure .NET way (requires .NET 4.5)

Also, your path should be in quotes in case there is a space. Note that the quotes are escaped with '\'. "" is also a valid escape sequence for a quote in C#:

string.Format("a -t7z \"{0}.7z\" \"{0}\" *.txt -mx9", path);
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • I don't see 7za.exe inside installed 7zip directory..? – 007 Mar 07 '14 at 18:52
  • 1
    You have to download it seperately from 7Zip. Here is the link:http://downloads.sourceforge.net/sevenzip/7za920.zip – BradleyDotNET Mar 07 '14 at 18:55
  • I am getting a warning/error when one of the folder in my path has a space in the name of the folder...i.e. ~\"Visual Studio"\... the program thinks that folder path ends at Visual and Studio is a name of the file... – 007 Mar 07 '14 at 21:29
  • 1
    @user1569220 see my update, you need escaped quotes around your path. – BradleyDotNET Mar 07 '14 at 21:36
  • Whew..thanks for that. One more issue..I am getting the whole folder archived "RequiredDocs"...Update> nvm, got it. :) Thank you very much. – 007 Mar 07 '14 at 21:55
  • No problem, glad you got it working. – BradleyDotNET Mar 07 '14 at 22:05
  • Just one more query, I want to delete the .txt file after zipping it. Seems like if I run File.Delete right after the zipping code, the txt file is deleted but the zip file doesn't have anything inside..is there a way to delete the file after 7za run within the program itself? – 007 Mar 07 '14 at 23:10
  • 1
    @user1569220, Call Process.WaitForExit on your newly created process before running the delete code. – BradleyDotNET Mar 07 '14 at 23:14
  • a -tzip "D:\Folder1 A\FolderX\FolderY\Folder ABC\Folder A B C\Folder X Y\"File - Name Goes Here.zip "D:\Folder1 A\FolderX\FolderY\Folder ABC\Folder A B C\Folder X Y\"*.csv -mx9 <<<<<<<<< is my argument string, I am confused as to where I am missing the quotations...can you tell by looking at this? – 007 Mar 10 '14 at 22:56
  • 1
    The first argument appears to have its quotation end to soon, shouldn't "File - Name Goes Here" be in the quotes? Same with your second one, though you may have issues with the wildcard expansion. Also, try running this directly from command line (instead of code) and make sure you know what a working command looks like, then compare against the one in your debugger. – BradleyDotNET Mar 10 '14 at 23:01
  • removed \" before File - Name Goes Here and placed it right before .zip and seems to work. :) Thanks, also running is cmd is a good advice..forgot it from yesterday. :( – 007 Mar 10 '14 at 23:03
  • No problem, glad you got it working. – BradleyDotNET Mar 10 '14 at 23:04
1

Here's an example from my application. This example extracts an archive but it shows you how to set up the process. Just change the command to 7z and the arguments. This example assumes you're shipping 7za.exe with your application. Good luck.

        public static bool ExtractArchive(string f) {
        string tempDir = Environment.ExpandEnvironmentVariables(Configuration.ConfigParam("TEMP_DIR"));

        if (zipToolPath == null) return false;

        // Let them know what we're doing.
        Console.WriteLine("Unpacking '" + System.IO.Path.GetFileName(f) + "' to temp directory.");
        LogFile.LogDebug("Unpacking '" + System.IO.Path.GetFileName(f) + "' to temp directory '" + tempDir + "'.",
            System.IO.Path.GetFileName(f));

        Process p = new Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        if (pid == PlatformID.Win32NT || pid == PlatformID.Win32S || pid == PlatformID.Win32Windows || pid == PlatformID.WinCE) {
            p.StartInfo.FileName = "\"" + Path.Combine(zipToolPath, zipToolName) + "\"";
            p.StartInfo.Arguments = " e " + "-y -o" + tempDir + " \"" + f + "\"";
        } else {
            p.StartInfo.FileName = Path.Combine(zipToolPath, zipToolName);
            p.StartInfo.Arguments = " e " + "-y -o" + tempDir + " " + f;
        }
        try {
            p.Start();
        } catch (Exception e) {
            Console.WriteLine("Failed to extract the archive '" + f + "'.");
            LogFile.LogError("Exception occurred while attempting to list files in the archive.");
            LogFile.LogExceptionAndExit(e);
        }
        string o = p.StandardOutput.ReadToEnd();
        p.WaitForExit();

        string[] ls = o.Split('\n');
        for (int i = 0; i < ls.Count(); i++) {
            string l = ls[i].TrimEnd('\r');
            if (l.StartsWith("Error")) {
                LogFile.LogError("7za: Error '" + ls[i + 1] + "'.", f);
                Console.WriteLine("Failed to extract the archive '" + f + "'.");
                return false;
            }
        }
        return true;
    }
John Yost
  • 693
  • 5
  • 20