Thursday 20 February 2014

Create Zip and download file using IONIC.ZIP in asp.net

 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();}

    }

No comments:

Post a Comment

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...