zoukankan      html  css  js  c++  java
  • MVC实现实现文件流打包成压缩包

    MVC实现实现文件流打包成压缩包


    640?wx_fmt=jpeg


    1、使用压缩类库SharpZipLib 

    SharpZipLib 是一款比较经典实用C#压缩类库

    SharpZipLib 库特点:功能丰富、稳定 ,支持主流 zip、Gzip Tar BZip2 格式

    2、项目中引用

    SharpZipLib的官方地址是:http://icsharpcode.github.io/SharpZipLib/,实际使用可以通过NuGet获取,在NuGet的地址是:http://www.nuget.org/packages/SharpZipLib/

    在Visual Studio中可以通过NuGet程序包管理控制台输入命令PM> Install-Package SharpZipLib或者用NuGet管理界面搜索并安装。

    需要引用命名空间:

     using ICSharpCode.SharpZipLib.Checksums; 

     using ICSharpCode.SharpZipLib.Zip; 

     using System.IO;

    3、MVC代码示例 直接从文件流输出zip

    //控制器写法

    public FileResult PrintData()

    {

                byte[] bytePDF = 需要打包的文件流;

                byte[] result = null;

                using (MemoryStream ms = new MemoryStream())

                {

                    using (ZipOutputStream zipStream = new ZipOutputStream(ms))

                    {

                        zipStream.Password = "123456";//设置压缩包密码

                        ZipEntry entry = new ZipEntry("文件名");

                        entry.DateTime = DateTime.Now;//创建时间

                        zipStream.PutNextEntry(entry);

                        zipStream.Write(bytePDF, 0, bytePDF.Length);

                        zipStream.CloseEntry();

                        zipStream.IsStreamOwner = false;

                        zipStream.Finish();

                        zipStream.Close();

                        ms.Position = 0;

                        //压缩后的数据被保存到了byte[]数组中。

                        result = ms.ToArray();

                    }

                }

    return File(result, "application/zip", "文件名.zip");

    }


  • 相关阅读:
    自制2048小游戏(附源码)
    PyQt5设计思路(长期更新,每写一篇新博客都会更新一次)
    Orthogonal table 实现理论
    Convex Hull 实现理论
    elasticsearch 命令操作
    Springboot
    2018/3/8错题解析
    初始MyBatis
    如何做好微信订阅号
    在路上,三线城市互联网创业记录
  • 原文地址:https://www.cnblogs.com/hgmyz/p/12351598.html
Copyright © 2011-2022 走看看