//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. ubuntu创建用户命令

    可以用adduser和useradd来添加用户,用userdel来删除用户. 最简单的命令:sudo adduser test会自动同名组,创建/home/test/,从etc/skel/复制文件,并 ...

  2. error: insufficient permissions for device(解决adb shell问题)

    今天在linux下连接平板usb,试用adb shell时出现error: insufficient permissions for device, 而且我们输入adb devices显示: xxna ...

  3. 使用canvas绘制饼状图

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...

  4. 11g等待事件之library cache: mutex X

    11g等待事件之library cache: mutex X 作者: dbafree 日期: 2012 年 07 月 01 日发表评论 (0)查看评论   library cache: mutex X ...

  5. 大量的源文件添加到Android.mk的问题

    LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := game_shared LOCAL_MODULE_FILENAME ...

  6. tf.reduce_mean

    tf.reduce_mean reduce_mean( input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=N ...

  7. 别人不会给你说的---C语言中数组名和指针的区别 及 sizeof用法

    引自: http://blog.csdn.net/tianyue168/article/details/5781924 #i nclude <iostream.h> int  main( ...

  8. oracle快速创建可用用户

    执行语句 create user utaptest identified by utaptest; create tablespace utaptestspace datafile 'd:\data. ...

  9. nginx 4层tcp代理获取真实ip

    举个例子,Nginx 中的代理配置假如是这样配置的: location / { proxy_http_version 1.1; proxy_set_header X-Real-IP $remote_a ...

  10. Python - 连接msql数据库

    可能出现的异常:python中出现SyntaxError: Non-UTF-8 code 解决方法 刚开始一直有用idle写代码,今天用了一下PyDev结果发现中文不支持,在网上搜了一下,结果发现解决 ...