C#文件打包下载

2021/9/16 17:08:39

本文主要是介绍C#文件打包下载,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

using ICSharpCode.SharpZipLib.Zip;
using Qianchen.Application.Base.SystemModule;
using Qianchen.Application.OA.File.FilePreview;
using Qianchen.Util;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Mvc;

namespace Qianchen.Application.Web.Areas.LR_SystemModule.Controllers
{
    /// <summary>
    /// 版 本 v7.0.6 开发框架
    /// Copyright (c) 2013-2020 QC
    /// 创建人:框架开发组
    /// 日 期:2017.03.08
    /// 描 述:附件管理
    /// </summary>
    public class AnnexesController : MvcControllerBase
    {
       // 省略部分代码

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="idsJson">文件id</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public void DownAnnexesFileZip(string id)
        {
            List<FileEntity> fileList = new List<FileEntity>();
            IEnumerable<AnnexesFileEntity> list = annexesFileIBLL.GetList(id);
            //int count = 0;
            Dictionary<string, int> dict = new Dictionary<string, int>();
            foreach (var entity in list)
            {
                if (System.IO.File.Exists(entity.F_FilePath))
                {
                    int count = 0;
                    if (dict.ContainsKey(entity.F_FileName))

                    {

                        if (dict.TryGetValue(entity.F_FileName, out count))

                        {

                            count++;
                            dict.Remove(entity.F_FileName);
                            dict.Add(entity.F_FileName, count);

                            // 处理重名文件,避免打包失败

                            entity.F_FileName = "(" + count + ")" + entity.F_FileName;

                        }

                    }

                    else

                    {

                        dict.Add(entity.F_FileName, 0);

                    }
                    fileList.Add(new FileEntity(entity.F_FileName, entity.F_FilePath));
                }
            }

            DownLoadoldAll(fileList);
        }


        #endregion
        #region 私有方法

        /// <summary>
        /// 打包下载
        /// </summary>
        /// <param name="FileName">文件虚拟路径</param>
        ///  /// <param name="name">返回客户端名称</param>
        private  void DownLoadoldAll(List<FileEntity> fileList)
        {
            Dictionary<string, Stream> streams = new Dictionary<string, Stream>();
            Stream streamWriter = null;
            foreach (var entity in fileList)
            {
                //将所有文件流在循环中加进,根据自己情况来写
                streamWriter = System.IO.File.Open(entity.FilePath, FileMode.Open);
                streams.Add(entity.FileName, streamWriter);
            }

            //下面可以直接复制粘贴
            MemoryStream ms = new MemoryStream();
            ms = PackageManyZip(streams);
            byte[] bytes = new byte[(int)ms.Length];
            ms.Read(bytes, 0, bytes.Length);
            ms.Close();
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode("打包文档.zip", System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
        }

        /// <summary>
        /// 将多个流进行zip压缩,返回压缩后的流
        /// </summary>
        /// <param name="streams">key:文件名;value:文件名对应的要压缩的流</param>
        /// <returns>压缩后的流</returns>
        private static MemoryStream PackageManyZip(Dictionary<string, Stream> streams)
        {
            byte[] buffer = new byte[6500];
            MemoryStream returnStream = new MemoryStream();
            var zipMs = new MemoryStream();
            using (ZipOutputStream zipStream = new ZipOutputStream(zipMs))
            {
                zipStream.SetLevel(9);
                foreach (var kv in streams)
                {
                    string fileName = kv.Key;
                    using (var streamInput = kv.Value)
                    {
                        zipStream.PutNextEntry(new ZipEntry(fileName));
                        while (true)
                        {
                            var readCount = streamInput.Read(buffer, 0, buffer.Length);
                            if (readCount > 0)
                            {
                                zipStream.Write(buffer, 0, readCount);
                            }
                            else
                            {
                                break;
                            }
                        }
                        zipStream.Flush();
                    }
                }
                zipStream.Finish();
                zipMs.Position = 0;
                zipMs.CopyTo(returnStream, 5600);
            }
            returnStream.Position = 0;
            return returnStream;
        }

        #endregion
    }

    public class FileEntity
    {
        public FileEntity() { }
        public FileEntity(string FileName, string FilePath)
        {
            this.FileName = FileName;
            this.FilePath = FilePath;
        }
        /// <summary>
        /// 文件名称
        /// </summary>
        /// <returns></returns>
        public string FileName { get; set; }
        /// <summary>
        /// 文件路径
        /// </summary>
        /// <returns></returns>
        public string FilePath { get; set; }
    }
}

  



这篇关于C#文件打包下载的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程