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#?
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.
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..
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.
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