Home
Manage Your Code
Snippet: Using SharpZipLib (C#)
Title: Using SharpZipLib Language: C#
Description: Compressing files with SharpZipLib Views: 78
Author: Quang Ninh DINH Date Added: 4/14/2008
Copy Code  
1using System;
2using System.IO;
3
4using ICSharpCode.SharpZipLib.Core;
5using ICSharpCode.SharpZipLib.Zip;
6
7class MainClass
8{
9    public static void Main(string[] args)
10    {
11        string[] filenames = Directory.GetFiles(args[0]);
12        byte[] buffer = new byte[4096];
13
14        using ( ZipOutputStream s = new ZipOutputStream(File.Create(args[1])) ) {
15
16            s.SetLevel(9); // 0 - store only to 9 - means best compression
17
18            foreach (string file in filenames) {
19                ZipEntry entry = new ZipEntry(file);
20                s.PutNextEntry(entry);
21
22                using (FileStream fs = File.OpenRead(file)) {
23                    StreamUtils.Copy(fs, s, buffer);
24                }
25            }
26        }
27    }
28}
29
Notes
From SharpZibLib help file: http://www.icsharpcode.net/OpenSource/SharpZipLib/