11

I have a directory that contains several files. I want compress this folder to a zip or tar.gz file. How can I do his work in C#?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
masoud ramezani
  • 22,228
  • 29
  • 98
  • 151

11 Answers11

8

You can use DotNetZip Library. It has quite rich and useful features.


EDIT:

string[] MainDirs = Directory.GetDirectories(DirString);

for (int i = 0; i < MainDirs.Length; i++)
{
    using (ZipFile zip = new ZipFile())
    {
        zip.UseUnicodeAsNecessary = true;
        zip.AddDirectory(MainDirs[i]);
        zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
        zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
        zip.Save(string.Format("test{0}.zip", i));   
    }
}
Kyle Rosendo
  • 25,001
  • 7
  • 80
  • 118
Giorgi
  • 30,270
  • 13
  • 89
  • 125
8

Look into using SharpZipLib. It supports both GZip and ZIP compression in C#.

There is an excellent tutorial here outlining what you need to do to zip a directory with SharpZipLib.

Daniel May
  • 8,156
  • 1
  • 33
  • 43
3

use 7zip from commandline in C# --> LZMA SDK supports C#, and there are codesamples in the package

Natrium
  • 30,772
  • 17
  • 59
  • 73
2

i use the System.IO.Packaging Namespace which was introduced with .NET Framework 3.5. I decided to use that one because it's based on .NET Framework Base classes and no 3rd party code is required which blows up the size of the code..

here's another post on Stackoverflow regarding this Question

And here's the Namespace and ZipPackage declaration / explanation @MSDN

hope that helps

Christian

Community
  • 1
  • 1
Christian Casutt
  • 2,334
  • 4
  • 29
  • 38
1

At my previous job we used #ziplib.

Gerrie Schenck
  • 22,148
  • 20
  • 68
  • 95
1

The question is quite old and so are the answers.

Best answer since end of 2012 is: Use .NET 4.5 and the contained System.IO.Compression and System.IO.Compression.ZipArchive namespace classes.

One of many example links you receive if you search in the internet: http://www.codeproject.com/Articles/381661/Creating-Zip-Files-Easily-in-NET

Since 2014/2015 ff.: With Roslyn the whole framework library was published as Open Source, so AFAI understand it, you are free to extract the code from the 4.5 classes (as it should be not really system specific) and use it as a library for the earlier .NET frameworks. Maybe this would give some license advantages over using the other classes- but this has to be analyzed by you.

Philm
  • 3,448
  • 1
  • 29
  • 28
0

This is a good discussion that discusses the possibility of doing this without any third party libraries. I think you should have a look on it.

Here is a large repository of sample codes that can help you in your work. Good Luck..

Community
  • 1
  • 1
Chathuranga Chandrasekara
  • 20,548
  • 30
  • 97
  • 138
0

GZip is part of Microsoft Framework 2.0 onward. Its called GZipStream under System.IO.Compression namespace.

To compress a directory with this class, you'd have to create a serializable class (for e.g. Directory) which contains a collection of Files.

The Files class would contain file-name and file-stream to read bytes from file. Once you do apply GZip on the Directory, it'll read Files one by one and write them to GZipStream.

Check this link: http://www.vwd-cms.com/forum/forums.aspx?topic=18

ata
  • 8,853
  • 8
  • 42
  • 68
0

Another pre-3.5 option is to use the zip utilities from J#. After all, .Net doesn't care what language the code was originally written in ;-).

Articles on how to do this:

Peter Stuer
  • 1,945
  • 2
  • 19
  • 18
0

You can zip the directory in pure .NET 3.0.

First, you will need a reference to WindowsBase.dll.

This code will open or create a zip file, create a directory inside, and place the file in that directory. If you want to zip a folder, possibly containing sub-directories, you could loop through the files in the directory and call this method for each file. Then, you could depth-first search the sub-directories for files, call the method for each of those and pass in the path to create that hierarchy within the zip file.

public void AddFileToZip(string zipFilename, string fileToAdd, string destDir)
{
    using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
    {
        string destFilename = "." + destDir + "\\" + Path.GetFileName(fileToAdd);
        Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
        if (zip.PartExists(uri))
        {
            zip.DeletePart(uri);
        }
        PackagePart part = zip.CreatePart(uri, "", CompressionOption.Normal);

        using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
        {
            using (Stream dest = part.GetStream())
            {
                CopyStream(fileStream, dest);
            }
        }
    }
}

destDir could be an empty string, which would place the file directly in the zip.

Sources: https://weblogs.asp.net/jongalloway/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib

https://weblogs.asp.net/albertpascual/creating-a-folder-inside-the-zip-file-with-system-io-packaging

amr ras
  • 136
  • 10
0

The most simple solution that I found using System.IO.Compression available from .Net 4.0:

System.IO.Compression.ZipFile.CreateFromDirectory(directoryToArchivePath, archiveDestinationPath);
hatem87
  • 157
  • 4