使用的类库为: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. Hive的Metastore contains multiple versions

    hive 客户端报错:Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeExcepti ...

  2. iptables 配置需要保存

    iptables-save > /root/myiptables 将iptables规则导入到文件/root/myiptablse iptables-restore < /root/myi ...

  3. HDU 2993 MAX Average Problem dp斜率优化

    MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  4. Netty高性能之道

    1. 背景 1.1. 惊人的性能数据 最近一个圈内朋友告诉我,通过使用Netty4 + Thrift压缩二进制编解码技术,他们实现了10W TPS(1K的复杂POJO对象)的跨节点远程服务调用.相比于 ...

  5. Android 中LocalBroadcastManager的使用方式

    Android 中LocalBroadcastManager的使用方式 在android-support-v4.jar中引入了LocalBroadcastManager,称为局部通知管理器,这种通知的 ...

  6. 最好的JAVA IDE IntelliJ IDEA使用简介(一)—之界面元素

    打开IDEA,(当第一次打开的时候出现的是一个欢迎页面,随便创建一个project来进入到IDEA的主界面),主界面显示如下: 主界面由6个主要区域组成(图中红色数字标注的) 1.菜单和工具栏 2.导 ...

  7. [LeetCode#212]Word Search II

    Problem: Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...

  8. Extjs4开发中的一些问题

    1.  子frame刷新的问题 一般在jsp里面,要实现界面跳转,有很多方法,最典型的就是window.location.href="href",但是在嵌套有iframe框架的页面 ...

  9. php写excel

    $this->loadexcel();        $objPHPExcel = new PHPExcel();        $objPHPExcel->getProperties() ...

  10. 使用ECharts报表统计公司考勤加班,大家加班多吗?

    最近个项目已经连续加班1个月多,因为公司经常有在外面客户现场或出差的情况,人事每个月初会把上个月的份考勤打卡记录全部发出来,让我们对自己的考勤,突然想到可根据大家打卡时间记录统计每天工作时间,看大家是 ...