public static Stream CreateZip(List<string> listPath, int level = 5)
{
MemoryStream mstream = new MemoryStream();
using (ZipOutputStream zipstream = new ZipOutputStream(mstream))
{
zipstream.SetLevel(level);
Crc32 crc = new Crc32();
foreach (var path in listPath)
{
FileStream fs = File.Open(path, FileMode.Open);
//重置流的位置
fs.Position = 0L;
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length); //ZIP文件条目
ZipEntry entry = new ZipEntry(Path.GetFileName(path));
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close(); crc.Reset();
crc.Update(buffer);
//冗余校验码
entry.Crc = crc.Value; zipstream.PutNextEntry(entry);
zipstream.Write(buffer, 0, buffer.Length);
}
//ZipOutputStream关闭后不关闭mstream
zipstream.IsStreamOwner = false;
}
//重置流的位置
mstream.Position = 0L;
return mstream;
}

使用流读取并压缩文件。

zipstream.IsStreamOwner = false;

必须设置

在MVC中使用FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName)方法可直接下载。

contentType可使用"application/x-zip-compressed"表示zip文件类型。

将零散文件使用ICSharpCode.SharpZipLib压缩打包后一次性下载的更多相关文章

  1. ICSharpCode.SharpZipLib 压缩、解压文件 附源码

    http://www.icsharpcode.net/opensource/sharpziplib/ 有SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, GZip, ...

  2. .NET使用ICSharpCode.SharpZipLib压缩/解压文件

    SharpZipLib是国外开源加压解压库,可以方便的对文件进行加压/解压 1.下载ICSharpCode.SharpZipLib.dll,并复制到bin目录下 http://www.icsharpc ...

  3. ICSharpCode.SharpZipLib压缩解压

    一.使用ICSharpCode.SharpZipLib.dll: 下载地址 http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.asp ...

  4. C#使用ICSharpCode.SharpZipLib压缩后进行web批量下载文件

    参考:http://blog.csdn.net/kongwei521/article/details/51167903#

  5. js中使用showModelDialog中下载文件的时候,闪一下后无法下载

    在js中使用showModelDialog中下载文件的时候,会因为showModelDialog自动设置target为_self导致下载文件“只会闪一下”就消失掉 在吧target设置为_blank后 ...

  6. r.js压缩打包(require + backbone)项目开发文件

    最近项目稳定了一点,之前一直没空关注的开发文件压缩打包问题也有时间来解决了 AMD模块化开发中的代码压缩打包工具——r.js 环境搭建基于nodejs:用于AMD模块化开发中的项目文件压缩打包,不是A ...

  7. ICSharpCode.SharpZipLib

    ICSharpCode.SharpZipLib 压缩.解压文件 附源码   http://www.icsharpcode.net/opensource/sharpziplib/ 有SharpZipli ...

  8. 错误处理:加载文件或程序集“ICSharpCode.SharpZipLib”

    解决方案:如果你的程序是2.0的,则删除 C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/中的所有的文件 如果是4.0的,删除C:/WINDOWS/Micr ...

  9. vue打包后出现的.map文件

    run dev build 打包项目后出现的list中的".map"文件最大. “.map”文件的作用:项目打包后,代码都是经过压缩加密的,如果运行时报错,输出的错误信息无法准确得 ...

随机推荐

  1. CuteFTP文件列表按名称排序,有中文文件名时,软件死掉的解决办法

    看到很多人的解决办法是切换到一个没有中文的文件夹,点击排序后,再切换回来,这个的确是可以解决问题,但是有些繁琐! 直接一步到位的解决办法是: 依次点击菜单:工具->全局选项->导航-> ...

  2. Spring 配置RMI远程调用

    项目中遇到了跨系统调用,当初想的是用webservice或者是hessian.但是这个接口用到的并不多,而且是一个非常简单的方法.所有便想到了RMI.在spring中 实现RMI非常简单. 暴露服务: ...

  3. LINUX下用PHPIZE安装PHP GD扩展

    环境:LNMP in centOS 6.4. linux下PHP的扩展可以用phpize的方法,比较简单地进行启用. 以下以PHP-GD2 库安装为例子. sudo yum install php-g ...

  4. B. Sereja and Suffixes

    B. Sereja and Suffixes time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. pandas实例美国人口分析

  6. linux / OS 杀死进程

    1,查询端口 sudo netstat -apn | grep 端口号 2,杀死进程kill -9 应用程序进程id

  7. aerospike(2)-java client

    地址:https://www.aerospike.com/download/client/java/4.3.1/ 例子顺序:https://github.com/aerospike/aerospike ...

  8. WebApi Helper帮助文档 swagger

      http://www.it165.net/pro/html/201602/61437.htmlhttp://www.cnblogs.com/gossip/p/4546630.html       ...

  9. php数组·的方法-数组检索

    /* * //数组检索函数 * */ //array_keys() 获取数组中所有键名 //array_values() 获取数组中所有键名 $arr6=range('a','e'); print_r ...

  10. VUE验证器哪家强? VeeValidate absolutely!

    VUE验证器哪家强? VeeValidate absolutely! vee-validate表单验证用法 github地址:https://github.com/baianat/vee-valida ...