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. [php代码审计] Window api函数 FindFirstFile 在php中的问题

    include文件夹中文件: 内容: <?php echo __FILE__; ?> index.php: 演示如下: “<<”替换多个任意字符: “>”替换单个字符:

  2. CSS3 nth-of-type(n)选择器 last-of-type选择器 nth-last-of-type(n)选择器 CSS3 only-child选择器 only-of-type选择器

    CSS3 nth-of-type(n)选择器 “:nth-of-type(n)”选择器和“:nth-child(n)”选择器非常类似,不同的是它只计算父元素中指定的某种类型的子元素.当某个元素中的子元 ...

  3. 如何修改Pspice元件库中元件的模型参数?

    比如下面是我取出的一个稳压管参数....* created using Parts release 7.1p on 11/03/98 * per Microsemi estimate: Trr(est ...

  4. 【leetcode】1041. Robot Bounded In Circle

    题目如下: On an infinite plane, a robot initially stands at (0, 0) and faces north.  The robot can recei ...

  5. Java缓冲流写出数据实例

    public class BufferedWriterDemo throws IOException { public static void main(String[] args) throws I ...

  6. MySql不区分大小写。

    解决方案: 1:给相关字段添加上让其区分大小写. alter table 表名 modify column 字段名 varchar(100) binary character set utf8

  7. java 简单指令说明

    javac:Java编译器,Java程序的编译工具,用来将Java程序的源文件编译成字节码文件,也就是.class文件.java:Java解释器,解释和执行已经转换成字节码的Java应用程序.jdb: ...

  8. Best Practices For Running On The PS4

    原文:https://forums.unrealengine.com/showthread.php?54448-Best-Practices-For-Running-On-The-PS4 Hey gu ...

  9. 使div弹窗可拖拽指令

    在项目开发过程中,有些情况dialog弹窗,直接使用div模拟弹窗效果,并需要支持div可拖拽. div模拟弹窗效果: (1)在用于存放指令的文件夹内,新建js文件,命名为:drag.js.具体代码如 ...

  10. 云计算openstack共享组件——Memcache 缓存系统

    一.缓存系统 静态web页面: 1.工作流程: 在静态Web程序中,客户端使用Web浏览器(IE.FireFox等)经过网络(Network)连接到服务器上,使用HTTP协议发起一个请求(Reques ...