Thursday, April 21, 2011

Dynamic Create Zip file .net 2.0

Note: Add two namespace and one Reference liberary Name "vjslib".

using java.io;
using java.util.zip;


private void Zipfile(string fileName,string Extn)
{
string TempPath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
string allFiles = TempPath + @"\" + fileName;
Response.Clear();
Response.ContentType = "text/zip";
Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}", fileName.Replace(Extn, "zip")));
//Response.AddHeader("Content-Length", fileName.Replace("CSV", "zip").Length.ToString());

// Ziping code.
StringBuilder sb = new StringBuilder();
string ZipFileName;
string theDirectory = TempPath;
ZipFileName = TempPath + @"\" + fileName.Replace(Extn, "zip");
FileOutputStream fos = new FileOutputStream(ZipFileName);
ZipOutputStream zos = new ZipOutputStream(fos);
zos.setLevel(9);
string sourceFile = allFiles;
FileInputStream fis = new FileInputStream(sourceFile);
ZipEntry ze = new ZipEntry(sourceFile.Replace(theDirectory + @"\", ""));
zos.putNextEntry(ze);
sbyte[] buffer = new sbyte[1024];
int len;
while ((len = fis.read(buffer)) >= 0)
{
zos.write(buffer, 0, len);
}
fis.close();
zos.closeEntry();
zos.close();
fos.close();

// popup Zip file
Response.TransmitFile(ZipFileName);
Response.End();
}

No comments:

Post a Comment

Hash Table in C#

 The Hashtable is a non-generic collection and used to store the key/value pairs based on the hash code of the key. Key will be used to acce...