Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sample C# .net code for zipping a file using 7zip

Tags:

c#

7zip

I have installed 7-zip 4.65 on my machine at C:\Program files. I want to use it in C# code to zip a file. The file name will be provided by the user dynamically. Can any one please provide a sample code on how to use 7zip in C# code?

like image 828
user386647 Avatar asked Jul 08 '10 12:07

user386647


People also ask

What is sample C program?

/* add.c * a simple C program */ #include <stdio.h> #define LAST 10 int main() { int i, sum = 0; for ( i = 1; i <= LAST; i++ ) { sum += i; } /*-for-*/ printf("sum = %d\n", sum); return 0; } The main parts are: preprocessor directives (notable by the # character and the lack of semicolons)

What is %d in C programming?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.


1 Answers

lots of answer given above but i used this below mention code to zip or unzip a file using 7zip

you must have 7zip application in your system .

     public void ExtractFile(string source, string destination)
        {
            // If the directory doesn't exist, create it.
            if (!Directory.Exists(destination))
                Directory.CreateDirectory(destination);

            string zPath = @"C:\Program Files\7-Zip\7zG.exe";
// change the path and give yours 
            try
            {
                ProcessStartInfo pro = new ProcessStartInfo();
                pro.WindowStyle = ProcessWindowStyle.Hidden;
                pro.FileName = zPath;
                pro.Arguments = "x \"" + source + "\" -o" + destination;
                Process x = Process.Start(pro);
                x.WaitForExit();
            }
            catch (System.Exception Ex) {
              //DO logic here 
              }
        }

to create zip file

public void CreateZip()
{
    string sourceName = @"d:\a\example.txt";
    string targetName = @"d:\a\123.zip";
    ProcessStartInfo p = new ProcessStartInfo();
    p.FileName = @"C:\Program Files\7-Zip\7zG.exe";
    p.Arguments = "a -tgzip \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
    p.WindowStyle = ProcessWindowStyle.Hidden;
    Process x = Process.Start(p);
    x.WaitForExit();
}
like image 108
Vishal Sen Avatar answered Nov 10 '22 00:11

Vishal Sen