Wednesday, 26 June 2013

Creating zip file in .net

->Download Ionic.zip.reduced.dll
-> Give reference to it.

public void DownloadFiles()
    {
        try
        {
            using (ZipFile zip = new ZipFile())
            {
             
                for (int i = 0; i < gvManageWorksheet.Rows.Count; i++)
                {
                    CheckBox cbox = (CheckBox)gvManageWorksheet.Rows[i].FindControl("chkdelete");
                    if (cbox.Checked)
                    {
                        LinkButton lbtn_FileName = (LinkButton)gvManageWorksheet.Rows[i].FindControl("LinkButton1");
                        string fileName = lbtn_FileName.Text;
                        if (fileName != "")
                        {
                            LinkButton lbtn_UploadedBy = (LinkButton)gvManageWorksheet.Rows[i].FindControl("LinkButton3");
                            string UploadedBy = lbtn_UploadedBy.Text;
                            string fileToAdd = "";                      
                                fileToAdd = Server.MapPath(".\\Worksheet\\" + (Session["SchoolId"]).ToString() + "\\" + fileName + "");
                         
                            FileInfo fileExist = new FileInfo(fileToAdd);
                            if (fileExist.Exists)
                            {
                                zip.AddFile(fileToAdd, "xyz");
                            }
                        }
                    }
                    else
                    {

                    }
                }
                Response.Clear();
                Response.BufferOutput = false;
                string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
                Response.ContentType = "application/zip";
                Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
                //zip.Save();
                zip.Save(HttpContext.Current.Response.OutputStream);
                HttpContext.Current.ApplicationInstance.CompleteRequest();
                //Response.End();
            }
        }
        catch (Exception ex)
        {

        }
    }

No comments:

Post a Comment