public void crateZip()
{
FileStream fs = null;//, fs2=null;
try
{
string strFName= Session["FileName"]!=null?Session["FileName"].ToString():"";
string destination= Session["FileLocation"] != null?Session["FileLocation"].ToString():"";
string filep = Request.PhysicalApplicationPath + "FolderName//";
string datafolder =filep+ strFName.Substring(0, strFName.Length-4);
string foldername=strFName.Substring(0, strFName.Length - 4);
string path = Server.MapPath("~/FolderName/" + foldername+"/");
string[] filenames = Directory.GetFiles(path);
using (ZipFile zip = new ZipFile())
{
zip.AddFile(destination,"");
zip.AddFiles(filenames, foldername);
zip.Save(Server.MapPath("~/" + foldername + ".zip"));
}
string zipFullPath = Server.MapPath("~/" + foldername + ".zip");
string strPath;
strPath = Session["FileLocation"].ToString();
string strFileName;
strFileName = Session["FileName"].ToString();
//fs=new FileStream();
fs = File.Open(zipFullPath, FileMode.Open);
byte[] bytBytes = new byte[(fs.Length)];
fs.Read(bytBytes, 0, (int)fs.Length);
fs.Close();
Response.AddHeader("Content-disposition", "attachment; filename=" + foldername + ".zip");
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(bytBytes);
if (File.Exists(strPath))
{
System.IO.File.Delete(strPath);
}
if (File.Exists(zipFullPath))
{
System.IO.File.Delete(zipFullPath);
}
if (Directory.Exists(path))
{
Directory.Delete(path,true);
}
Response.Flush();
Response.End();
}
catch (System.Exception ex)
{
}
finally { fs.Dispose(); Response.Clear();}
}
{
FileStream fs = null;//, fs2=null;
try
{
string strFName= Session["FileName"]!=null?Session["FileName"].ToString():"";
string destination= Session["FileLocation"] != null?Session["FileLocation"].ToString():"";
string filep = Request.PhysicalApplicationPath + "FolderName//";
string datafolder =filep+ strFName.Substring(0, strFName.Length-4);
string foldername=strFName.Substring(0, strFName.Length - 4);
string path = Server.MapPath("~/FolderName/" + foldername+"/");
string[] filenames = Directory.GetFiles(path);
using (ZipFile zip = new ZipFile())
{
zip.AddFile(destination,"");
zip.AddFiles(filenames, foldername);
zip.Save(Server.MapPath("~/" + foldername + ".zip"));
}
string zipFullPath = Server.MapPath("~/" + foldername + ".zip");
string strPath;
strPath = Session["FileLocation"].ToString();
string strFileName;
strFileName = Session["FileName"].ToString();
//fs=new FileStream();
fs = File.Open(zipFullPath, FileMode.Open);
byte[] bytBytes = new byte[(fs.Length)];
fs.Read(bytBytes, 0, (int)fs.Length);
fs.Close();
Response.AddHeader("Content-disposition", "attachment; filename=" + foldername + ".zip");
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(bytBytes);
if (File.Exists(strPath))
{
System.IO.File.Delete(strPath);
}
if (File.Exists(zipFullPath))
{
System.IO.File.Delete(zipFullPath);
}
if (Directory.Exists(path))
{
Directory.Delete(path,true);
}
Response.Flush();
Response.End();
}
catch (System.Exception ex)
{
}
finally { fs.Dispose(); Response.Clear();}
}
No comments:
Post a Comment