//DLL

using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;

 public void DownloadZipFile(string id)
{
string zipFilePath = Property.GetFileUrlString("免冠照片temp", Property.FileEnum.zip);
if (!Directory.Exists(Server.MapPath("~/Template/Template/Zip")))
{
Directory.CreateDirectory(Server.MapPath("~/Template/Template/Zip"));
} try
{ List<string> filenames = new List<string>();
string filesPath = string.Empty;
foreach (var item in id.Split(','))
{ var result = _pictureAppServices.FirstOrDefault(p => p.Id.ToString() == item);
if (result != null)
{
filesPath = result.Url;
}
filenames.Add(Server.MapPath("~/" + filesPath));
// filenames.Add(); ;
} //生成的压缩文件为test.zip
using (FileStream fsOut = System.IO.File.Create(Server.MapPath(zipFilePath+".zip")))
{
//ZipOutputStream类的构造函数需要一个流,文件流、内存流都可以,压缩后的内容会写入到这个流中。
using (ZipOutputStream zipStream = new ZipOutputStream(fsOut))
{
foreach (string file in filenames)
{
FileInfo fi = new FileInfo(file);
string entryName = System.IO.Path.GetFileName(file);
//ZipEntry类代表了一个压缩包中的一个项,可以是一个文件,也可以是一个目录。
ZipEntry newEntry = new ZipEntry(entryName);
newEntry.DateTime = fi.LastWriteTime;
newEntry.Size = fi.Length;
//把压缩项的信息添加到ZipOutputStream中。
zipStream.PutNextEntry(newEntry);
byte[] buffer = new byte[];
//把需要压缩文件以文件流的方式复制到ZipOutputStream中。 using (FileStream streamReader = System.IO.File.OpenRead(file))
{
StreamUtils.Copy(streamReader, zipStream, buffer);
}
zipStream.CloseEntry();
} //使用流操作时一定要设置IsStreamOwner为false。否则很容易发生在文件流关闭后的异常。
zipStream.IsStreamOwner = false;
zipStream.Finish();
zipStream.Close();
}
} // filenames = Directory.GetFiles(filesPath);
//using (ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(zipFilePath)))
//{ // s.SetLevel(9); // 压缩级别 0-9
// //s.Password = "123"; //Zip压缩文件密码
// byte[] buffer = new byte[4096]; //缓冲区大小
// foreach (string file in filenames)
// {
// ZipEntry entry = new ZipEntry(Path.GetFileName(file));
// entry.DateTime = DateTime.Now;
// s.PutNextEntry(entry);
// using (FileStream fs = System.IO.File.OpenRead(file))
// {
// int sourceBytes;
// do
// {
// sourceBytes = fs.Read(buffer, 0, buffer.Length);
// s.Write(buffer, 0, sourceBytes);
// } while (sourceBytes > 0);
// }
// }
// s.Finish();
// s.Close();
//}
}
catch (Exception ex)
{
Console.WriteLine("Exception during processing {0}", ex);
}
      return File(Server.MapPath(zipFilePath), "application/zip", "免冠照片.zip");
}

c# 创建压缩包并下载文件的更多相关文章

  1. linux上创建ftp服务器下载文件///使用AWS服务器作为代理,下载sbt相关的包

    最近觉得自己下载有些jar的速度太慢了,就在aws上下好了,然后转到我电脑上来,在aws上开了ftp服务器.结果就倒腾了一上午,作个记录,以便后面查看. 1.安装vsftpd yum -y insta ...

  2. Linux shell ftp命令下载文件 根据文件日期

    需求:ftp获取远程数据的文件,根据文件的创建时间点下载文件. 可以自行扩展根据文件的大小等其他需求. 知识点总结: 1.获取文件的时间: ls -lrt|awk '{print $6" & ...

  3. [转]用JavaScript在浏览器中创建下载文件

    前端很多项目中,都有文件下载的需求,特别是JS生成文件内容,然后让浏览器执行下载操作(例如在线图片编辑.在线代码编辑.iPresst等. 但受限于浏览器,很多情况下我们都只能给出个链接,让用户点击打开 ...

  4. php 通过header下载中文文件名 压缩包损坏或文件不存在的问题

    开发中大家都是使用的utf8编码,昨天遇到一个奇坑,本是一件很小的问题,解决也浪费了个吧小时.废话不多说,植入正题: 文件下载方式:通过header二进制流文件下载需求: 文件上传保留文件名不变数据字 ...

  5. JS前端创建CSV或Excel文件并浏览器导出下载

    长期以来,在做文件下载功能的时候都是前端通过ajax把需要生成的文件的内容参数传递给后端,后端通过Java语言将文件生成在服务器,然后返回一个文件下载的连接地址url.前端通过location.hre ...

  6. [转] 使用 JavaScript 创建并下载文件

    [From] https://gaohaoyang.github.io/2016/11/22/js-create-file-and-download/ 本文将介绍如何使用 JavaScript 创建文 ...

  7. 在浏览器端用JS创建和下载文件

    前端很多项目中,都有文件下载的需求,特别是JS生成文件内容,然后让浏览器执行下载操作(例如在线图片编辑.在线代码编辑.iPresst等). 但受限于浏览器,很多情况下我们都只能给出个链接,让用户点击打 ...

  8. Java创建文件夹、创建文件、上传文件,下载文件

    1.创建文件夹 /** * 判断文件夹是否存在 * @param myPath */ public static void judeDirExists(File myPath) { if (!myPa ...

  9. 使用 JavaScript 创建并下载文件

    先上代码 Blob 对象 Blob URLs 模拟 click 小结 参考 本文将介绍如何使用 JavaScript 创建文件,并自动/手动将文件下载.这在导出原始数据时会比较方便. 先上代码 /** ...

随机推荐

  1. 如何矫正罗圈腿(O型腿)

    http://bbs.hupu.com/10787225.html 留着自个儿看

  2. Lua 自己实现排序sort比较方法,抛出错误invalid order function for sorting

    明天新功能就要上了,结果刚刚突然QA说项目抛出了错误.握草,吓得立马出了一身汗. 查了一下错误,发现可能是自己写的不稳定排序造成的.自己感觉应该就是.把排序方法写成稳定的之后,代码分离编译进手机,跑了 ...

  3. 【linux排错】"error while loading shared libraries: xxx.so.x" 错误的原因和解决办法

    一般我们在Linux下执行某些外部程序的时候可能会提示找不到共享库的错误, 比如: lcw: error : cannot open shared object file: No such file ...

  4. Python3之pymysql导入mysql

    $cat insert.py #!/usr/bin/python # -*- coding: UTF-8 -*- import os import sys import datetime import ...

  5. Node.js学习笔记(1)--一个最简单的服务器请求

    说明(2017-5-2 10:27:03): 1. 需要安装node,http://nodejs.cn/download/ 2. 安装完后,在cmd里输入node -v可以查看版本. 3. 代码foo ...

  6. 基于jQuery点击图像居中放大插件Zoom

    分享一款基于jQuery点击图像居中放大插件Zoom是一款放大的时候会从原图像的位置以动画方式放大到画面中间,支持点击图像或者按ESC键来关闭效果.效果图如下: 在线预览   源码下载 实现的代码. ...

  7. 手动安装 MyEclipse6.5 FindBugs

    手动安装步骤: 官方网址:http://findbugs.sourceforge.net/; 下载地址:http://sourceforge.net/projects/findbugs/files/f ...

  8. undefined reference to `clock_gettime'编译错误的解决办法

    解决办法如下

  9. exec和sp_executesql

    sqlserver中的exec和sp_executesql都能执行动态的sql语句和存储过程,但exec用法较为简单,不能带参数,也没有返回参数. sp_executesql则显得功能更加完善,可以用 ...

  10. Visual Studio 2008 添加MScomm控件的方法

    1.下载MSCOMM.zip,解压后包含4个文件:MSCOMM32.OCX, MSCOMM.SRG, MSCOMM32.DEP, MSCOMM32.OCA 2.将Mscomm.srg, Mscomm3 ...