Thursday, 27 February 2014

Create a small Server........

//  Create  a folder in main root name with files
 //And Download DLL file
// Ionic.Zip.dll
<asp:GridView ID="gridview1" CellPadding="5" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="chk" Checked="false" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Text" HeaderText="FileName" />
            </Columns>
            <HeaderStyle BackColor="white" Font-Bold="true" ForeColor="White" />
        </asp:GridView>

        <asp:Button ID="btn_Download" Text="Download " runat="server"
        onclick="btn_Download_Click" />

************************ cs file********************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Ionic.Zip;
public partial class zipfolder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        BindGridview();
        }
    }
    protected void BindGridview()
    {
   
        string[] files_Path = Directory.GetFiles(Server.MapPath("~/files/"));
        List<ListItem> files = new List<ListItem>();
        foreach (string path in files_Path)
        {
        files.Add(new ListItem(Path.GetFileName(path)));
        }
        gridview1.DataSource = files;
        gridview1.DataBind();
    }



    protected void btn_Download_Click(object sender, EventArgs e)
    {
        using (ZipFile zip = new ZipFile())
        {
            foreach (GridViewRow gr in gridview1.Rows)
            {
                CheckBox chk = (CheckBox)gr.FindControl("chk");
                if (chk.Checked)
                {
                    string fileName = gr.Cells[1].Text;
                    string filePath = Server.MapPath("~/files/" + fileName);
                    zip.AddFile(filePath, "files");
                }
            }
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=DownloadedFile.zip");
            Response.ContentType = "application/zip";
            zip.Save(Response.OutputStream);
            Response.End();
        }


    }
}

//Result