C#将文件压缩成一个文件流,供前端下载
直接上代码供大家参考。。。
前端页面就是一个下载的Button。。
<body>
<form id="form1" runat="server">
<div>
<asp:button ID="btn_down" runat="server" text="下载" onclick="btn_down_Click" />
</div>
</form>
</body>
后台代码:
protected void btn_down_Click(object sender, EventArgs e)
{
var name = Server.MapPath("huage");
//if (!Directory.Exists(name))
// throw new FileNotFoundException("")
var zipName = "test";
DownZip(name, zipName,);
}
private void DownZip(string dirname, string zipfile, int level = , string password = "")
{
MemoryStream ms = new MemoryStream();//支持存储区为内存的流
ZipOutputStream zos = new ZipOutputStream(ms);
if (password != "")
zos.Password = password; zos.SetLevel(level);
AddZipEntry(dirname, zos, dirname);
zos.Finish();
zos.Close();
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlDecode(zipfile,System.Text.Encoding.UTF8) + ".zip");
Response.BinaryWrite(ms.ToArray());
ms.Close();
Response.Flush();
Response.End();
}
private void AddZipEntry(string strPath, ZipOutputStream zos, string baseDirName)
{
DirectoryInfo dir = new DirectoryInfo(strPath);
foreach (FileSystemInfo item in dir.GetFileSystemInfos())
{
if ((item.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
{
AddZipEntry(item.FullName, zos, baseDirName);
}
else
{
FileInfo f_item = (FileInfo)item;
using (FileStream fs = f_item.OpenRead())
{
byte[] buffer = new byte[(int)fs.Length];
fs.Read(buffer, , buffer.Length);
ZipEntry entry = new ZipEntry(f_item.FullName.Replace(baseDirName, ""));
zos.PutNextEntry(entry);
zos.Write(buffer, , buffer.Length);
}
}
}
}
压缩的代码的没看懂的话,可以参考我的文章:
C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压
C#将文件压缩成一个文件流,供前端下载的更多相关文章
- Java 多文件压缩成一个文件工具类
简单修改来自博客园勇闯天涯zfc的博客 一.内容 ①使用 Java 将多个文件打包压缩成一个压缩文件: ②主要使用 java.io 下的类 二.源代码:ZIPUtil .java import jav ...
- 使用 Java 将多个文件压缩成一个压缩文件
使用 Java 将多个文件压缩成一个压缩文件 一.内容 ①使用 Java 将多个文件打包压缩成一个压缩文件: ②主要使用 java.io 下的类 二.源代码:ZipMultiFile.java pac ...
- os模块、文件压缩 、匹配文件后缀名:fnmatch glob
一.os模块 os模块:是python是系统交互的模块 import os # 0平台信息的一些操作 python是夸平台的,所以内部兼容了不同的平台 1. os.name # 操作系统 nt是win ...
- Java—将文件压缩为zip文件
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- 开源作品-PHP写的JS和CSS文件压缩利器(单文件绿色版)-SuMinify_PHP_1_5
前言: 网站项目需要引用外部文件以减小加载流量,而且第一次加载外部资源文件后,其他同域名的页面如果引用相同的地址,可以利用浏览器缓存直接读取本地缓存资源文件,而不需要每个页面都下载相同的外部资源文件. ...
- windows下多个文件合并成一个文件
如果你拿到的是一堆文件,那么你想把它合并成一个文件来使用,那么按下面的步骤,轻轻松松就可以搞定. 第一步:把所有要合并的文件放到同一个文件下面 第二步:在CMD里面进入到你的文件目录 第三步:输入如下 ...
- PHP调用Linux的命令行执行文件压缩命令&&创建文件夹修改权限
一开始,我和普通青年一样,想到用PHP内置的 ZipArchive纠结的是环境上没安装zip扩展,想采用用PHP调用Linux的命令行 ,执行压缩命令,感兴趣的朋友可以了解下,希望本文对你有所帮助 前 ...
- 将二级目录下的文件合并成一个文件的Python小脚本
这个小程序的目的是将二级目录下的文件全部合并成一个文件(其实几级目录都可以,只要做少许改动) #coding:utf8 import sys, os def process(path): new_fi ...
- python实现将文件夹内所有txt文件合并成一个文件
新建一个文件夹命名为yuliao,把所有txt文件放进去就ok啦!注意路径中‘/’,windows下路径不是这样. #coding=utf-8 import os #获取目标文件夹的路径 filed ...
随机推荐
- iptables详解(3):iptables规则管理
所属分类:IPtables Linux基础 在本博客中,从理论到实践,系统的介绍了iptables,如果你想要从头开始了解iptables,可以查看iptables文章列表,直达链接如下 iptab ...
- What is the difference between rhel 6 and rhel7
What is the difference between rhel 6 and rhel7 difference rhel 6 RHEL 7 release date 10 NOV 2010 as ...
- Python2 下Ubuntu linux Mac 安装 PyV8
在pip install PyV8(注意区分大小写)时,出现了如下报错 command 'x86_64-linux-gnu-gcc' failed with exit status 1 解决方案: 百 ...
- 44.bucket filter:统计各品牌最近一个月的平均价格
课程大纲 GET /tvs/sales/_search { "size": 0, "query": { "term": { &quo ...
- PAT 1113 Integer Set Partition
Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint set ...
- 06007_redis数据存储类型——hash
1.概述 (1)Redis中的Hash类型可以看成具有String Key和String Value的map容器.所以该类型非常适合于存储值对象的信息,如Username.Password和Age等: ...
- AndroidTreeView:Android树形节点View
AndroidTreeView:Android树形节点View AndroidTreeView是github上的一个第三方开源项目,其在github上的项目主页是:https://github. ...
- hdu 3064
1:前n项和公式:1+2+3+...+n = n*(n+1)/2 2:前n项平方和公式:1^2+2^2+.........+n^2=n*(n+1)*(2n+1)/6 #include<stdio ...
- 【ACM】nyoj_106_背包问题_201308152026
背包问题时间限制:3000 ms | 内存限制:65535 KB 难度:3描述 现在有很多物品(它们是可以分割的),我们知道它们每个物品的单位重量的价值v和重量w(1<=v,w<=10 ...
- Spring MVC-控制器(Controller)-多动作控制器(Multi Action Controller)示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_multiactioncontroller.htm 说明:示例基于Spring M ...