首页 > 编程知识 正文

C#循环下载多个文件(把多个文件压缩成一个文件可以一次性下载)

时间:2023-05-05 15:28:03 阅读:282644 作者:4446

获取源码方式:

第一种:打开微信,搜一搜"别打我女儿的主意"打开微信小程序,找到菜单栏,点击源码,进去就可以获得链接

第二种:可以给本文点赞好评,然后发邮件到792166417@qq.com,如果有学习资料视频可以分享的话,可以捎带分享给我。

using ICSharpCode.SharpZipLib.Checksum;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Demo
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<string> listFJ = new List<string>();//保存附件路径
            List<string> listFJName = new List<string>();//保存附件名字
            for (int i = 0; i < 2; i++)
            {
                listFJ.Add(Server.MapPath("upload/") + (i + 1) + ".txt");
                listFJName.Add((i + 1) + ".txt");
            }
            string time = DateTime.Now.Ticks.ToString();
            ZipFileMain(listFJ.ToArray(), listFJName.ToArray(), Server.MapPath("upload/" + time + ".zip"), 9);//压缩文件
            DownloadFile(Server.UrlEncode("附件.zip"), Server.MapPath("upload/" + time + ".zip"));//下载文件
        }

        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();
        }

        /// <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();
            }
        }
    }
}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。