使用的类库为:ICSharpCode.SharpZipLib.dll

一种是打包整个文件夹,另一种是打包指定的多个文件,大同小异:

 using ICSharpCode.SharpZipLib.Zip;

 public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 打包下载某文件夹里的所有文件 //需要打包的文件夹
string path = "UploadImage/Images/";
string serverPath = Server.MapPath(path); //创建临时文件夹
string tempName = DateTime.Now.ToString("yyyyMMddHHMMss");
string tempFolder = Path.Combine(serverPath, tempName);
Directory.CreateDirectory(tempFolder); //遍历文件夹中所有的文件到临时文件夹中
DirectoryInfo folder = new DirectoryInfo(serverPath);
foreach (FileInfo file in folder.GetFiles())
{
string filename = file.Name;
File.Copy(serverPath + "/" + filename, tempFolder + "/" + filename);
} //压缩文件
compressFiles(tempFolder, tempFolder + "\\\\" + tempName + ".rar");
//下载文件
// DownloadRAR(tempFolder + "\\\\" + tempName + ".rar", "这里下载文件重命名后的名称"); //某些文件打包下载
DownLodeSum(); } /// <summary>
/// 某些文件打包下载
/// </summary>
public void DownLodeSum()
{
//临时文件夹所在目录
string path = "UploadImage/";
string serverPath = Server.MapPath(path); //创建临时文件夹
string tempName = DateTime.Now.ToString("yyyyMMddHHMMss");
string tempFolder = Path.Combine(serverPath, tempName);
Directory.CreateDirectory(tempFolder); //复制需要压缩的文件到临时文件夹中
File.Copy(Server.MapPath("UploadImage/Images/bg-b.png"), Server.MapPath(path + tempName + "/bg-b.png"));
File.Copy(Server.MapPath("UploadImage/Images/bg-j.png"), Server.MapPath(path + tempName + "/bg-j.png"));
File.Copy(Server.MapPath("UploadImage/Images/icon-cart.png"), Server.MapPath(path + tempName + "/icon-cart.png")); //压缩文件
compressFiles(tempFolder, tempFolder + "\\\\" + tempName + ".rar");
//下载文件
DownloadRAR(tempFolder + "\\\\" + tempName + ".rar", "这里下载文件重命名后的名称"); } /// <summary>
/// 压缩文件
/// </summary>
/// <param name="dir">文件目录</param>
/// <param name="zipfilename">zip文件名</param>
public static void compressFiles(string dir, string zipfilename)
{
if (!Directory.Exists(dir))
{
return;
}
try
{
string[] filenames = Directory.GetFiles(dir);
using (ZipOutputStream s = new ZipOutputStream(File.Create(zipfilename)))
{ s.SetLevel(); // 0 - store only to 9 - means best compression byte[] buffer = new byte[]; foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, , buffer.Length);
s.Write(buffer, , sourceBytes);
} while (sourceBytes > );
}
}
s.Finish();
s.Close();
}
}
catch
{ }
} /// <summary>
/// 下载生成的RAR文件
/// </summary>
private void DownloadRAR(string file, string name)
{
FileInfo fileInfo = new FileInfo(file);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + name + ".rar");
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
string tempPath = file.Substring(, file.LastIndexOf("\\\\"));
//删除临时目录下的所有文件
DeleteFiles(tempPath);
//删除空目录
Directory.Delete(tempPath);
Response.End();
} /// <summary>
/// 删除临时目录下的所有文件
/// </summary>
/// <param name="tempPath">临时目录路径</param>
private void DeleteFiles(string tempPath)
{
DirectoryInfo directory = new DirectoryInfo(tempPath);
try
{
foreach (FileInfo file in directory.GetFiles())
{
if (file.Attributes.ToString().IndexOf("ReadOnly") != -)
{
file.Attributes = FileAttributes.Normal;
}
File.Delete(file.FullName);
}
}
catch (Exception)
{
throw;
}
}
}

点击下载源码

ASP.NET 打包下载文件的更多相关文章

  1. asp.net mvc5 下载文件方法

    控制器自带的 FileContentResult 可以让我们很方便的返回文件到服务端,减少了很多步骤.用于下载文件的时候,像视频.文本.图片这种浏览器支持的文件,默认就会被浏览器打开.这时候想让它变成 ...

  2. ASP.NET批量下载文件的方法

    一.实现步骤 在用户操作界面,由用户选择需要下载的文件,系统根据所选文件,在服务器上创建用于存储所选文件的临时文件夹,将所选文件拷贝至临时文件夹.然后调用 RAR程序,对临时文件夹进行压缩,然后输出到 ...

  3. ASP.NET批量下载文件

    一.实现步骤 在用户操作界面,由用户选择需要下载的文件,系统根据所选文件,在服务器上创建用于存储所选文件的临时文件夹,将所选文件拷贝至临时文件夹.然后调用 RAR程序,对临时文件夹进行压缩,然后输出到 ...

  4. Asp.net mvc 下载文件

    前言 最近有需求需要下载文件,可能是image的图片,也可能是pdf报告,也可能是微软的word或者excel文件. 这里就整理了asp.net mvc 和asp.net webapi 下载的方法 A ...

  5. php打包下载文件

    使用前请先开启:查看下php.ini里面的extension=php_zip.dll前面的分号有没有去掉; $zip=new \ZipArchive(); $zifile = 'download/' ...

  6. C# ASP 上传/下载文件

    1.  上传文件前台页面 <div style="padding-left:20px;"> <asp:FileUpload ID="FileUpload ...

  7. Asp.Net 之 下载文件的常用方式

    1.直接使用Response.TransmitFile(filename)方法 protected void Button_Click(object sender, EventArgs e) { /* ...

  8. ASP.NET 后台下载文件方法

    void DownLoadFile(string fileName) { string filePath = Server.MapPath(fileName);//路径 //以字符流的形式下载文件 F ...

  9. asp.net core 下载文件,上传excel文件

    下载文件: 代码: 后端代码: public IActionResult DownloadFile() { var FilePath = @"./files/deparment.xlsx&q ...

随机推荐

  1. golang开发android环境搭建_window

    golang开发android环境搭建介绍 一 安装依赖软件: git:版本管理 go:  go开发环境(版本>=1.5),可直接下载window版的go安装包. android studio: ...

  2. UI设计网站参考

    1. https://dribbble.com/colors/6A969A 2. 设计师网站导航:http://hao.uisdc.com/ 3. bootstrap的UI:http://www.bo ...

  3. Contest20140709 testA 树型DP

    testA 输入文件: testA.in 输出文件testA.out 时限5000ms 问题描述: 一棵树N个节点,每条边有一个距W,现在定义SUM为所有dist(X,Y)的和1<=X<Y ...

  4. iOS获取键盘的高度

  5. cron表达式详解(Spring定时任务配置时间间隔)

    Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month ...

  6. Innodb和MyISAM比较

    Innodb和MyISAM比较 (1)MyISAM类型的表强调的是性能,其执行速度比InnoDB类型更快 (2)MyISAM不支持事务.外键,InnoDB支持事务和外键 (3)MyISAM使用的表级锁 ...

  7. java学习面向对象之父子构造函数初始化

    在之前讲到java面向对象继承的时候,我们只讲到了两个比较重要的知识点,一个是父子类当中有同名的成员变量,这个时候,我们引入了super这个关键字来区分这两个同名成员变量,除此之外,我们还讲到了父子同 ...

  8. 接收对 http://192.168.1.18:8001/ObtainData/Service 的 HTTP 响应时发生错误。这可能是由于服务终结点绑定未使用 HTTP 协议造成的。这还可能是由于服务器中止了 HTTP 请求上下文(可能由于服务关闭)所致。

    [2015/8/5 19:28:49]错误信息:接收对 http://192.168.1.18:8001/ObtainData/Service 的 HTTP 响应时发生错误.这可能是由于服务终结点绑定 ...

  9. WindowsPhone 8 开发 之 本地数据库应用

    微软提供的有一个本地数据库的例子 http://code.msdn.microsoft.com/wpapps/Local-Database-Sample-57b1614c 可以进行参照. 里边最核心的 ...

  10. android新建项目时 出现appcompat_v7工程错误和红色感叹号

    最近初学android,版本是22.6.0的话,每次创建一个项目就会出现一个appcompat_v7工程:然后我升级到最新的版本23.0.4之后,创建第一个项目,也会出现一个appcompat_v7工 ...