Overblog Follow this blog
Edit post Administration Create my blog
Balavardhan Reddy Narani

Creation of .Zip file in C# & ASP.Net

March 7 2009 , Written by Balavardhan Published on #ASP.Net

Creation of .Zip file in C#

Required Name Space
            using ICSharpCode.SharpZipLib.Zip;   

Here by I am providing code to Create a Zip file

public void SelectedFilesToZip()
using (ZipOutputStream s = new ZipOutputStream(File.Create("File Path & Name to Create")))
s.SetLevel(9); // 0 - store only to 9 - means best compression
byte[] buffer = new byte[4096];

foreach (string file in Arlist_FilePath)
System.IO.FileInfo filepath = new System.IO.FileInfo(file);
if (filepath.Exists)
string extension = System.IO.Path.GetExtension(file);
if (extension.ToString().Trim().ToUpper() != ".ZIP")
// Using GetFileName makes the result compatible with XP
// as the resulting path is not absolute.

ZipEntry entry = new ZipEntry(Path.GetFileName(file));

// Setup the entry data as required.

// Crc and size are handled by the library for seakable streams
// so no need to do them here.

// Could also use the last write time or similar for the file.

entry.DateTime = DateTime.Now;

using (FileStream fs = File.OpenRead(file))
// Using a fixed size buffer here makes no noticeable difference for output
// but keeps a lid on memory usage.
int sourceBytes;
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);

// Close is important to wrap things up and unlock the file.

Share this post

Repost 0

Comment on this post

Shanti 03/20/2009 16:06

Thank u, Balvardhan. This is a great solution for my scenario and it worked beautifully. Thanks again.