c# 创建压缩包并下载文件
//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# 创建压缩包并下载文件的更多相关文章
- linux上创建ftp服务器下载文件///使用AWS服务器作为代理,下载sbt相关的包
最近觉得自己下载有些jar的速度太慢了,就在aws上下好了,然后转到我电脑上来,在aws上开了ftp服务器.结果就倒腾了一上午,作个记录,以便后面查看. 1.安装vsftpd yum -y insta ...
- Linux shell ftp命令下载文件 根据文件日期
需求:ftp获取远程数据的文件,根据文件的创建时间点下载文件. 可以自行扩展根据文件的大小等其他需求. 知识点总结: 1.获取文件的时间: ls -lrt|awk '{print $6" & ...
- [转]用JavaScript在浏览器中创建下载文件
前端很多项目中,都有文件下载的需求,特别是JS生成文件内容,然后让浏览器执行下载操作(例如在线图片编辑.在线代码编辑.iPresst等. 但受限于浏览器,很多情况下我们都只能给出个链接,让用户点击打开 ...
- php 通过header下载中文文件名 压缩包损坏或文件不存在的问题
开发中大家都是使用的utf8编码,昨天遇到一个奇坑,本是一件很小的问题,解决也浪费了个吧小时.废话不多说,植入正题: 文件下载方式:通过header二进制流文件下载需求: 文件上传保留文件名不变数据字 ...
- JS前端创建CSV或Excel文件并浏览器导出下载
长期以来,在做文件下载功能的时候都是前端通过ajax把需要生成的文件的内容参数传递给后端,后端通过Java语言将文件生成在服务器,然后返回一个文件下载的连接地址url.前端通过location.hre ...
- [转] 使用 JavaScript 创建并下载文件
[From] https://gaohaoyang.github.io/2016/11/22/js-create-file-and-download/ 本文将介绍如何使用 JavaScript 创建文 ...
- 在浏览器端用JS创建和下载文件
前端很多项目中,都有文件下载的需求,特别是JS生成文件内容,然后让浏览器执行下载操作(例如在线图片编辑.在线代码编辑.iPresst等). 但受限于浏览器,很多情况下我们都只能给出个链接,让用户点击打 ...
- Java创建文件夹、创建文件、上传文件,下载文件
1.创建文件夹 /** * 判断文件夹是否存在 * @param myPath */ public static void judeDirExists(File myPath) { if (!myPa ...
- 使用 JavaScript 创建并下载文件
先上代码 Blob 对象 Blob URLs 模拟 click 小结 参考 本文将介绍如何使用 JavaScript 创建文件,并自动/手动将文件下载.这在导出原始数据时会比较方便. 先上代码 /** ...
随机推荐
- centos6安装部署git服务器(gitlab6.4)
环境准备 python版本2.6git版本 1.8.4.1ruby版本ruby-2.0.0-p353gitlab-shell版本 v1.8.0gitlab版本6.4.3 因centos6系列的pyth ...
- DIOCP开源项目-DIOCP3直接发送对象,帮你处理粘包问题
该DEMO演示,如何在客户端与服务端之间直接传递TStream对象,让你专注于处理数据逻辑,可以忽略处理网络传输间粘包的问题. 上面由服务端向所有的客户端推送一个消息TMemoryStream对象(该 ...
- 深入HBase架构解析(一)[转]
前记 公司内部使用的是MapR版本的Hadoop生态系统,因而从MapR的官网看到了这篇文文章:An In-Depth Look at the HBase Architecture,原本想翻译全文,然 ...
- Docker 入门(Mac环境)-part 1 入门基本操作
part-1 入门基本操作 Docker 安装 去官网下载对应的版本,然后点击安装就可以了: 如果环境是Linux,可以参照之前写的get started教程 查看docker版本 docker -- ...
- hive的UDF读取配置文件
hive的UDF读取配置文件 实现步骤 在读取配置文件的写为./file_name,然后在添加UDF的时候把配置文件也加入资源就好了: add jar xxx.jar; add file file_n ...
- android 修改listview item view 的方法
具体的解答办法很简单: 代码如下 : 1.获取需要更新的view int visiblePosition = mListView.getFirstVisiblePosition(); View vie ...
- Django服务端读取excel文件并且传输到接口
path_name = "opboss_download_" + str(int(time.time())) + ".csv" print(path_name) ...
- LeetCode: Valid Palindrome 解题报告
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- tf.truncated_normal
tf.truncated_normal truncated_normal( shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name ...
- git的使用总结
git使用技巧 git使用技巧 windows下git使用 git ignore file git 打tag vim编码设置 git ls显示中文乱码问题 git view history 查看某个文 ...