Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top