Dec 11, 2009

How to zip multiple files in c#,asp.net

using opensource(sharpziplib)
downloading dll in the following link

http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
add the dll and using the below namespaces


//Namespaces
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;

code:
Crc32 crc = new Crc32();
ZipOutputStream s = new ZipOutputStream(File.Create(@"c:\newfile.zip"));
s.SetLevel(9); // 0 - store only to 9 - means best compression
ZipEntry entry = null;
String[] str = new String[] { "Ads.xml", "Ads1.xml" };
byte[] buffer = null;
for (int i = 0; i < str.Length; i++)
{
FileStream fs = File.OpenRead(@"c:\" + str[i]);
buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
entry = new ZipEntry(ZipEntry.CleanName(@"c:\" + str[i]));
entry.DateTime = DateTime.Now;
entry.Comment = "test file";
entry.ZipFileIndex = i;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
s.Finish();
s.Close();

No comments:

Post a Comment