zoukankan      html  css  js  c++  java
  • .NetCore下多个文件流生成压缩文件

    原有系统的附件都是保存在服务器上的,占用了大量的磁盘空间,如今要切换到AWS S3上做文件存储管理。文件打包下载之前的做法是,后台将文件服务器上的多个文件打包到一个压缩文件中,存放在服务器,后端接口返回压缩文件地址供前端下载;为此,特写下这篇文章来记录此处是如何根据文件流来制作压缩包的。

    AWS S3更多存取文件的demo需看官方源码,本文中有下载的实例。

    需要引用:AWSSDK.Core、AWSSDK.S3,可从NuGet安装;
    需要引用:System.IO.Compression,可从NuGet安装;

    在ConfigureServices下单例注入AWS类

    public void ConfigureServices(IServiceCollection services)
    {
        .......
        services.AddSingleton<Aws>();
        .........
    }

    Configure下获取AWS实例

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        //......//
        Aws.Instance = app.ApplicationServices.GetService<Aws>();
    }

    AWS类

    public class Aws
    {
        #region Init
        private readonly IConfiguration _cfg;
        /// <summary>
        /// 存储桶名称
        /// </summary>
        private string bucketName;
        /// <summary>
        /// aws Id
        /// </summary>
        private readonly string awsAccessKeyId;
        /// <summary>
        /// aws Id key
        /// </summary>
        private readonly string awsSecretAccessKey;
        private IAmazonS3 client;
        private AmazonS3Config config = new AmazonS3Config()
        {
            ServiceURL = "http://s3.amazonaws.com",
            RegionEndpoint = Amazon.RegionEndpoint.CNNorth1 //账号在哪个区域就用哪个区域,不然会出现账号401的问题
        };
    
        #endregion
    
        public Aws(IConfiguration cfg)
        {
            _cfg = cfg;
            awsAccessKeyId = _cfg["AccessKeyId"];
            awsSecretAccessKey = _cfg["SecretAccessKey"];
            bucketName = _cfg["BucketName"];
            client = new AmazonS3Client(awsAccessKeyId, awsSecretAccessKey, config);
        }
    
        /// <summary>
        /// AWS文件批量下载
        /// </summary>
        /// <param name="fileName">文件相对地址(aws key)</param>
        /// <returns></returns>
        public async Task<byte[]> BatchDownload(List<string> fileNames)
        {
            try
            {
                byte[] res;
                using (MemoryStream ms = new MemoryStream())
                {
                    using (ZipArchive zip = new ZipArchive(ms, ZipArchiveMode.Create, true))
                    {
                        foreach (var fileName in fileNames)
                        {
                            //通过aws下载文件流
                            var response = await client.GetObjectAsync(bucketName, newfileName);
                            var pdfStream=  response.ResponseStream;//得到文件流
                            var pdfBytes = StringHelper.StreamToBytes(pdfStream);//文件流转换成字节
    
                            ZipArchiveEntry entry = zip.CreateEntry("abc.pdf");//压缩文件内创建一个文件名为“abc.pdf”,流是什么文件格式就用什么格式,此处我的是pdf格式
                            using (Stream sw = entry.Open())
                            {
                                sw.Write(pdfBytes, 0, pdfBytes.Length);//将文件的字节写到abc.pdf中
                            }
                        }
                        InvokeWriteFile(zip);//重新计算压缩文件的大小,此处参考别人的
                        int nowPos = (int)ms.Position;
                        res = new byte[ms.Length];
                        ms.Position = 0;
                        ms.Read(res, 0, res.Length);
                        ms.Position = nowPos;
                    }
                    return res;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    
        public void InvokeWriteFile(ZipArchive zipArchive)
        {
            foreach (MethodInfo method in zipArchive.GetType().GetRuntimeMethods())
            {
                if (method.Name == "WriteFile")
                {
                    method.Invoke(zipArchive, new object[0]);
                }
            }
        }
    }

    控制器下载压缩文件

    /// <summary>
    ///     批量下载
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    public async Task<IActionResult> Packing(List<int> ids)
    {
        var paths = new List<string>();
        //根据Id查询本地对应的文件名称,Id换取文件名称
        var bytes = await Aws.Instance.BatchDownload(paths).ToList());
    
        Response.ContentType = "application/octet-stream";
    
        var contentDisposition = "attachment;" + "filename=" + HttpUtility.UrlEncode("XXXX.zip");
        Response.Headers.Add("Content-Disposition", new string[] { contentDisposition });
    
        Response.ContentLength = bytes.Length;
        Response.Body.Write(bytes, 0, bytes.Length);//发送读取的内容数据到客户端浏览器
        Response.Body.Flush();
        return new EmptyResult();
    }

    前端Ajax调用

    //后端返回的是文件流
    var ids = [1,2,3];
    var url = "/PackingByAws";
    var form = $("<form></form>").attr("action", url).attr("method", "post");
    ////后端为List<Int>参数
    ids.forEach(function (value) {
        form.append($("<input></input>").attr("type", "hidden").attr("name", "ids").attr("value", value));
    })
    form.appendTo('body').submit().remove();
    第一次写文章,从业也有几年了,一直都是拿来主义。这篇文件也是近期项目遇到的特拿来分享,也是怀着无比忐忑的心情才写下来的,写的不好的地方还请见谅。

    参考:https://www.rainng.com/dotnet-ziparchive-update-stream/



    作者:佐小陌
    链接:https://www.jianshu.com/p/aab752964f4e
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    慎于行,敏于思!GGGGGG
  • 相关阅读:
    修改表结构
    enum/set 约束
    MYSQL 数据类型
    表和数据的基础操作
    存储引擎
    mysql
    HTML 介绍
    selor模块
    select
    协程
  • 原文地址:https://www.cnblogs.com/GarsonZhang/p/13039342.html
Copyright © 2011-2022 走看看