zip压缩

 首先需要Ionic.Zip.dll 页面前台

<body>
<form id="form1" runat="server">
<p>
    <asp:Button ID="PackDown" runat="server" Text="打包下载" OnClick="PackDown_Click" /></p>
<asp:GridView ID="GridView1" runat="server" Width="500px"
    CellPadding="8" CellSpacing="1">
    <Columns>
        <asp:TemplateField HeaderText="" InsertVisible="False">
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" />
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="文件列表" InsertVisible="False">           
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
</form>
</body>
 
页面后台CS代码:
 
 
void BindFilesList()
        {
            string strSql = "select Name from tableC";
            DataTable dt = m_db.ExecuteDataTable(strSql);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
 
        protected void PackDown_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.ContentType = "application/zip";
            Response.AddHeader("content-disposition", "filename=DotNetZip.zip");
            using (ZipFile zip = new ZipFile(System.Text.Encoding.Default))
            {
                foreach (GridViewRow gvr in GridView1.Rows)
                {
                    if (((CheckBox)gvr.Cells[0].Controls[1]).Checked)
                    {
                        zip.AddFile(Server.MapPath("SoftwareDownload/") + (gvr.Cells[1].Controls[1] as Label).Text, "");
                    }
                }
                zip.Save(Response.OutputStream);
            }
            Response.End();
        }
 
重点参考后台CS里的PackDown_Click方法

 

posted @ 2021-10-20 09:59  vba是最好的语言  阅读(68)  评论(0)    收藏  举报