打包下载zip代码

本文介绍了一种使用C#实现的文件下载与批量压缩的方法。该方法通过接收数据集、图片URL和压缩文件名作为参数,实现了从指定路径读取多个文件并将其压缩成ZIP格式的功能。此外还提供了如何设置压缩级别、读取文件内容以及如何通过HTTP响应返回压缩文件供用户下载的详细步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="dt">需要处理的数据集</param>
        /// <param name="imgUrl">文件在数据库里的名称</param>
        /// <param name="zipFileName">导出的zip压缩包名称</param>
        public static void FileDownload(DataTable dt, string imgUrl, string zipFileName)
        {
            List<string> tmpStr = new List<string>();
            if (dt.Rows.Count > 0)
            {
                tmpStr.AddRange(from DataRow dr in dt.Rows select dr[imgUrl].ToString());
            }
            if (tmpStr.ToArray().Length > 0)
            {
                using (ZipOutputStream s = new ZipOutputStream(File.Create(HttpContext.Current.Server.MapPath("") + "\\uploaded\\" + zipFileName)))
                {
                    s.SetLevel(1);  //设置压缩等级,等级越高压缩效果越明显,但占用CPU也会更多
                    foreach (string t in tmpStr)
                    {
                        string fileName = HttpContext.Current.Server.MapPath("") + "/uploaded/req/" + t;
                        using (FileStream fs = File.OpenRead(fileName))
                        {
                            byte[] buffer = new byte[4 * 1024];  //缓冲区,每次操作大小
                            if (!string.IsNullOrEmpty(fileName))
                            {
                                ZipEntry entry = new ZipEntry(Path.GetFileName(fileName));
                                s.PutNextEntry(entry);
                                int sourceBytes;
                                do
                                {
                                    sourceBytes = fs.Read(buffer, 0, buffer.Length);    //读取文件内容(1次读4M,写4M)
                                    s.Write(buffer, 0, sourceBytes);                    //将文件内容写入压缩相应的文件
                                } while (sourceBytes > 0);
                            }
                        }
                    }
                    s.CloseEntry();
                }
                var fName = HttpContext.Current.Server.MapPath("") + "\\uploaded\\" + zipFileName;

                if (!string.IsNullOrEmpty(fName))
                {
                    FileInfo downloadFile = new FileInfo(fName);
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.ClearHeaders();
                    HttpContext.Current.Response.Buffer = true;
                    zipFileName = HttpUtility.UrlEncode(zipFileName, Encoding.UTF8);
                    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + zipFileName);
                    HttpContext.Current.Response.ContentType = "application/zip";
                    HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("shift-jis");
                    HttpContext.Current.Response.AppendHeader("Content-Length", downloadFile.Length.ToString());
                    HttpContext.Current.Response.WriteFile(fName);
                    HttpContext.Current.Response.Flush();
                    HttpContext.Current.Response.Close();
                    HttpContext.Current.Response.End();
                }
                if (File.Exists(fName))
                {
                    File.Delete(fName);
                }
            }
        }

 

链接:https://pan.baidu.com/s/1q8j5GUzAWUG1DY0DgkgCgg 密码:b2ru

转载于:https://www.cnblogs.com/zhb7769/p/9244320.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值