1. 首先Nuget          ICSharpCode.SharpZipLib

    <script type="text/javascript">
$(function () {
$("#OutPutLink").click(function () { // 单击下文件时
$.ajax({ // 先判断条件时间内没有文件
url: "/Home/ExistsFile?statTime=" + $("#statTime").val() + "&endTime=" + $("#endTime").val(),
type: "Get",
success: function (data) {
if (data == "Exists") { // 如果有 就下载
window.location.href = "/Home/OutputFile?statTime=" + $("#statTime").val() + "&endTime=" + $("#endTime").val();
}
else
alert("该时间区域内无文件");
}
});
});
});
</script>
/// <summary>
/// 验证该时间段是否有文件
/// </summary>
/// <param name="statTime">开始时间</param>
/// <param name="endTime">结束时间</param>
/// <returns></returns>
public string ExistsFile(DateTime statTime, DateTime endTime)
{
DateTime sTime = DateTime.Parse(statTime.ToString("yyyy-MM-dd") + " 00:00:00");
DateTime eTime = DateTime.Parse(endTime.ToString("yyyy-MM-dd") + " 23:59:59");
DirectoryInfo TheFolder = new DirectoryInfo(Server.MapPath("~/Content/ExcelFile"));
//遍历文件夹下的文件
List<string> listFJName = new List<string>();//保存附件名字
foreach (FileInfo NextFile in TheFolder.GetFiles())
{
string fileType = Path.GetExtension(NextFile.FullName).ToString().ToLower();
if (fileType == ".xls" || fileType == ".xlsx")
{
string fileDate = NextFile.Name.Substring(, );
DateTime dtime = DateTime.ParseExact(fileDate, "yyyyMMdd", null);
if (dtime >= sTime && dtime <= eTime)
{
listFJName.Add(NextFile.Name);
}
}
}
if (listFJName.Count > )
return "Exists";
return "NotExists";
} /// <summary>
/// 执行将文件压缩下载
/// </summary>
/// <param name="statTime"></param>
/// <param name="endTime"></param>
public void OutputFile(DateTime statTime,DateTime endTime)
{
DateTime sTime = DateTime.Parse(statTime.ToString("yyyy-MM-dd") + " 00:00:00");
DateTime eTime = DateTime.Parse(endTime.ToString("yyyy-MM-dd") + " 23:59:59");
DirectoryInfo TheFolder = new DirectoryInfo(Server.MapPath("~/Content/ExcelFile"));
//遍历文件夹下的文件
List<string> listFJ = new List<string>();//保存附件路径
List<string> listFJName = new List<string>();//保存附件名字
foreach (FileInfo NextFile in TheFolder.GetFiles())
{
string fileType = Path.GetExtension(NextFile.FullName).ToString().ToLower();
if (fileType == ".xls" || fileType == ".xlsx")
{
string fileDate = NextFile.Name.Substring(, );
DateTime dtime = DateTime.ParseExact(fileDate, "yyyyMMdd", null);
if (dtime >= sTime && dtime <= eTime)
{
listFJ.Add(Server.MapPath("~/Content/ExcelFile/") + NextFile.Name);
listFJName.Add(NextFile.Name);
}
}
}
if (listFJ.Count > && listFJName.Count > )
{
string time = DateTime.Now.Ticks.ToString();
ZipFileMain(listFJ.ToArray(), listFJName.ToArray(), Server.MapPath("~/Content/ExcelFile/" + time + ".zip"), );//压缩文件
DownloadFile(Server.UrlEncode("附件.zip"), Server.MapPath("~/Content/ExcelFile/" + time + ".zip"));//下载文件
}
} /// <summary>
/// 下载文件
/// </summary>
/// <param name="fileName"></param>
/// <param name="filePath"></param>
private void DownloadFile(string fileName, string filePath)
{
FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
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();
System.IO.File.Delete(filePath);//删除已下载文件
Response.End();
} /// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileName">要压缩的所有文件(完全路径)</param>
/// <param name="fileName">文件名称</param>
/// <param name="name">压缩后文件路径</param>
/// <param name="Level">压缩级别</param>
public void ZipFileMain(string[] filenames, string[] fileName, string name, int Level)
{
ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(name));
Crc32 crc = new Crc32();
//压缩级别
s.SetLevel(Level); // 0 - store only to 9 - means best compression
try
{
int m = ;
foreach (string file in filenames)
{
//打开压缩文件
FileStream fs = System.IO.File.OpenRead(file);//文件地址
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length);
//建立压缩实体
ZipEntry entry = new ZipEntry(fileName[m].ToString());//原文件名
//时间
entry.DateTime = DateTime.Now;
//空间大小
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, , buffer.Length);
m++;
}
}
catch
{
throw;
}
finally
{
s.Finish();
s.Close();
}
}

.net 批量导出文件,以ZIP压缩方式导出的更多相关文章

  1. openlayers4 入门开发系列之批量叠加 zip 压缩 SHP 图层篇(附源码下载)

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

  2. arcgis api 3.x for js 入门开发系列批量叠加 zip 压缩 SHP 图层优化篇(附源码下载)

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...

  3. Android总结之Gzip/Zip压缩

    前言: 做过Android网络开发的都知道,在网络传输中我们一般都会开启GZIP压缩,但是出于刨根问底的天性仅仅知道如何开启就不能满足俺的好奇心的,所以想着写个demo测试一下比较常用的两个数据压缩方 ...

  4. 【VC++技术杂谈008】使用zlib解压zip压缩文件

    最近因为项目的需要,要对zip压缩文件进行批量解压.在网上查阅了相关的资料后,最终使用zlib开源库实现了该功能.本文将对zlib开源库进行简单介绍,并给出一个使用zlib开源库对zip压缩文件进行解 ...

  5. RAR和ZIP:压缩大战真相

    转:http://fqd2eh4y.blog.163.com/blog/static/69195855200801035015857 前言--王者归来? 等待足足两年之久,压缩霸主WinZip终于在万 ...

  6. 正确的 zip 压缩与解压代码

    网上流传的zip压缩与解压 的代码有非常大的问题 尽管使用了ant进行压缩与解压,可是任务的流程还是用的java.util.zip 的方式写的,我在使用的过程中遇到了压缩的文件夹结构有误,甚至出现不同 ...

  7. java基础---->Zip压缩的使用(转)

    java中提供了对压缩格式的数据流的读写.它们封装到现成的IO 类中,以提供压缩功能.下面我们开始java中压缩文件的使用. 目录导航: 关于压缩的简要说明 GZIP压缩文件的使用 ZIP压缩文件的使 ...

  8. Inno Setup入门(三)——指定压缩方式

    Setup段中的compression指定了采用的压缩方式,较高的压缩率需要较多的时间或者需要更大的内存空间,可用的值如下: zip zip/1到zip/9 bzip bzip/1 到bzip/9 l ...

  9. Java ZIP压缩和解压缩文件并兼容linux

    JDK中自带的ZipOutputStream在压缩文件时,如果文件名中有中文,则压缩后的 zip文件打开时发现中文文件名变成乱码. 解决的方法是使用apache-ant-zip.jar包(见附件)中的 ...

随机推荐

  1. Python配置模块:configparser参数含义

    https://blog.csdn.net/CoderPai/article/details/80420698

  2. Page.after

    解释: Page.after可以增加Page级的切面,触发的时机是在所拦截的对应生命周期方法执行之后,也可以拦截所有页面上发生的事件(对于要拦截的事件,在swan文件上必须显示绑定了相应事件). 方法 ...

  3. [CSP-S模拟测试]:Seat(概率DP+数学)

    题目描述 有$n+2$个座位等距地排成一排,从左到右编号为$0$至$n+1$.最开始时$0$号以及$n+1$号座位上已经坐了一个小$G$,接下来会有$n$个小$G$依次找一个空座位坐下.由于小$G$们 ...

  4. 2018 CCPC 吉林站 H Lovers || HDU 6562 (线段树哦)

    http://acm.hdu.edu.cn/showproblem.php?pid=6562 题意: q次操作 1.将第l~r个数的左边和和右边都加上一个数d, 使得这个数变成 dsiddsid的形式 ...

  5. vue 如何通过监听路由变化给父级路由菜单添加active样式

    1.项目需求:在项目开发中,多级菜单的情况下,勾选子菜单时,需要在父级菜单添加active样式. 2.遇到的问题:一级路由菜单的话,点击当前路由会自动在路由标签上添加router-link-exact ...

  6. python的final class

    https://zhuanlan.zhihu.com/p/31674972 https://rainmanwy.github.io/Python的final-Class/

  7. Aliyun mysql配置 远程访问 10038

    环境 centos7.2    mysql5.7 网上搜了好多的文章,有的说在/etc/my.cnf 注释掉bind-address = 127.0.0.1 ,也开启了3306端口的防火墙,也添加了远 ...

  8. 【ABAP系列】SAP ABAP实现LOG显示的方法

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP实现LOG显示的 ...

  9. 2019/10/27 TZOJ

    1001 Gaussian Prime http://www.tzcoder.cn/acmhome/problemdetail.do?&method=showdetail&id=379 ...

  10. C#获取本地路径

    /// <summary> /// 本地路径 /// </summary> /// <param name="path"></param& ...