//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. Git GUI中文乱码问题解决方法

    使用Git GUI的查看代码的时候,会出现中文乱码: 在乱码的区域点击鼠标右键,选择Encoding,然后选择Unicode(UTF-8),乱码问题解决:

  2. 安装Python2.7出现configure: error: no acceptable C compiler found in $PATH错误

    安装Python2.7出现configure: error: no acceptable C compiler found in $PATH错误 安装步骤: 安装依赖 yum groupinstall ...

  3. 解析虚拟VMware三种网络模式的根本区别

    网络模式(文字解说)VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式).要想在网络管理和维护中合理应用它们,你就应该先了解一下 ...

  4. LeetCode: Best Time to Buy and Sell Stock 解题报告

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

  5. Linux下字符集的安装

    目前环境中经常会遇到编码转化的问题,UTF-8跟GB2312也有问题.只得在Linux上安装GB2312(在Linux操作系统上又称zh_CN.GB2312)的字符集,具体请看下文. Linux下几个 ...

  6. java 多线程 22 :生产者/消费者模式 进阶 利用await()/signal()实现

    java多线程15 :wait()和notify() 的生产者/消费者模式 在这一章已经实现了  wait/notify 生产消费模型 利用await()/signal()实现生产者和消费者模型 一样 ...

  7. Json转list,二层解析转换

    一层结构的数据: { "code": "0", "results": { "boyTotal": 0, "cl ...

  8. java jar包与配置文件的写法

    一个普通的java project,里面引用了config.properties配置文件,将项目打成Runnable jar,然后将config.properties放到打包后的jar路径下,执行该j ...

  9. output-operations-on-dstreams

    Finally, this can be further optimized by reusing connection objects across multiple RDDs/batches. O ...

  10. [转]python进行中文文本聚类(切词以及Kmeans聚类)

    简介 查看百度搜索中文文本聚类我失望的发现,网上竟然没有一个完整的关于Python实现的中文文本聚类(乃至搜索关键词python 中文文本聚类也是如此),网上大部分是关于文本聚类的Kmeans聚类的原 ...