文件的批量打包下载

注意:首先引入dll文件ICSharpCode.SharpZipLib.dll

压缩文件

    /// <summary>
    /// 压缩文件
    /// </summary>
    /// <param name="fileName">要压缩的所有文件(完全路径)</param>
    /// <param name="fileName">文件名称</param>
    /// <param name="name">压缩后文件路径</param>
    /// <param name="Level">压缩级别</param>
    public void ZipFileMain(string[] filenames, string[] fileName, string name, int Level)
    {
        ZipOutputStream s = new ZipOutputStream(File.Create(name));
        Crc32 crc = new Crc32();
        //压缩级别
        s.SetLevel(Level); // 0 - store only to 9 - means best compression
        try
        {
            int m = 0;
            foreach (string file in filenames)
            {
                //打开压缩文件
                FileStream fs = File.OpenRead(file);//文件地址
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                //建立压缩实体
                ZipEntry entry = new ZipEntry(fileName[m].ToString());//原文件名
                //时间
                entry.DateTime = DateTime.Now;
                //空间大小
                entry.Size = fs.Length;
                fs.Close();
                crc.Reset();
                crc.Update(buffer);
                entry.Crc = crc.Value;
                s.PutNextEntry(entry);
                s.Write(buffer, 0, buffer.Length);
                m++;
            }
        }
        catch
        {
            throw;
        }
        finally
        {
            s.Finish();
            s.Close();
        }
    }

文件下载

//下载打包文件
    private void DownloadFile(string fileName, string filePath)
    {
        FileInfo fileInfo = new FileInfo(filePath);
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        Response.AddHeader("Content-Transfer-Encoding", "binary");
        Response.ContentType = "application/octet-stream";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.WriteFile(fileInfo.FullName);
        Response.Flush();
        File.Delete(filePath);//删除已下载文件
        Response.End();
    }

具体使用

protected void BtnDownloadFiles_Click(object sender, EventArgs e)
    {
        List<string> listFJ = new List<string>();//保存附件路径
        List<string> listFJName = new List<string>();//保存附件名字
        for (int i = 0; i < gridView.Rows.Count; i++)
        {
            HtmlInputCheckBox chk = (Page.Master.FindControl("ContentPlaceHolder1").FindControl("gridView") as GridView).Rows[i].FindControl("checkboxname") as HtmlInputCheckBox;
            if (chk != null && chk.Checked)
            {
                string temp = gridView.Rows[i].Cells[14].Text.ToString().Trim();
                if (temp != " ")
                {
                    listFJ.Add(Server.MapPath("~") + temp);
                    listFJName.Add(temp);
                }
            }
        }
        string time = DateTime.Now.Ticks.ToString();
        ZipFileMain(listFJ.ToArray(), listFJName.ToArray(), Server.MapPath("~/uploadfiles/" + time + ".zip"), 9);//压缩文件
        DownloadFile(Server.UrlEncode("附件.zip"), Server.MapPath("~/uploadfiles/" + time + ".zip"));//下载文件
    }

  

转载于:https://www.cnblogs.com/xtflz/p/10762115.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值