.net 批量导出文件,以ZIP压缩方式导出
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压缩方式导出的更多相关文章
- openlayers4 入门开发系列之批量叠加 zip 压缩 SHP 图层篇(附源码下载)
前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...
- arcgis api 3.x for js 入门开发系列批量叠加 zip 压缩 SHP 图层优化篇(附源码下载)
前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...
- Android总结之Gzip/Zip压缩
前言: 做过Android网络开发的都知道,在网络传输中我们一般都会开启GZIP压缩,但是出于刨根问底的天性仅仅知道如何开启就不能满足俺的好奇心的,所以想着写个demo测试一下比较常用的两个数据压缩方 ...
- 【VC++技术杂谈008】使用zlib解压zip压缩文件
最近因为项目的需要,要对zip压缩文件进行批量解压.在网上查阅了相关的资料后,最终使用zlib开源库实现了该功能.本文将对zlib开源库进行简单介绍,并给出一个使用zlib开源库对zip压缩文件进行解 ...
- RAR和ZIP:压缩大战真相
转:http://fqd2eh4y.blog.163.com/blog/static/69195855200801035015857 前言--王者归来? 等待足足两年之久,压缩霸主WinZip终于在万 ...
- 正确的 zip 压缩与解压代码
网上流传的zip压缩与解压 的代码有非常大的问题 尽管使用了ant进行压缩与解压,可是任务的流程还是用的java.util.zip 的方式写的,我在使用的过程中遇到了压缩的文件夹结构有误,甚至出现不同 ...
- java基础---->Zip压缩的使用(转)
java中提供了对压缩格式的数据流的读写.它们封装到现成的IO 类中,以提供压缩功能.下面我们开始java中压缩文件的使用. 目录导航: 关于压缩的简要说明 GZIP压缩文件的使用 ZIP压缩文件的使 ...
- Inno Setup入门(三)——指定压缩方式
Setup段中的compression指定了采用的压缩方式,较高的压缩率需要较多的时间或者需要更大的内存空间,可用的值如下: zip zip/1到zip/9 bzip bzip/1 到bzip/9 l ...
- Java ZIP压缩和解压缩文件并兼容linux
JDK中自带的ZipOutputStream在压缩文件时,如果文件名中有中文,则压缩后的 zip文件打开时发现中文文件名变成乱码. 解决的方法是使用apache-ant-zip.jar包(见附件)中的 ...
随机推荐
- 专家告诉你!如何避免黑客BGP劫持?
BGP前缀劫持是针对Internet组织的持久威胁,原因是域间路由系统缺乏授权和身份验证机制. 仅在2017年,数千起路由事件导致代价高昂的中断和信息拦截,而问题的确切程度未知.尽管在过去20年中已经 ...
- HAProxy+Heartbeat双节点出现VIP情况
本文使用heartbeat做高可用,主节点192.168.0.204,备节点192.168.0.205,vip192.168.0.206,防火墙启动状态 先启动主节点,再启动备节点后,发现以下问题: ...
- 20180715-Java StringBuffer和StringBuilder类
public class Test{ public static void main(String args[]){ StringBuffer sBuffer = new StringBuffer(& ...
- tomcat配置解决乱码问题
在服务器上,如果项目是Tomcat启动的,可以用以下方式的设置解决乱码问题: 方法1.在Tomcat的catalina.sh(或者catalina.bat)文件中,开头加入: set JAVA_OPT ...
- 学习如何使用Markdown
Markdown 新手指南点击查看 一级标题 二级标题 三级标题 四级标题 五级标题 六级标题 ---段落 引用 这是一个无序列表 这是一个无序列表 这是一个父无序列表 这是一个子无序列表 这是一个有 ...
- BF语言学习
Brainfuck是一种极小化的计算机语言,它是由Urban Müller在1993年创建的.由于fuck在英语中是脏话,这种语言有时被称为brainf*ck或brainf**k,甚至被简称为BF.这 ...
- Only variables should be passed by reference
报错位置代码: $status->type = array_pop(explode('\\',$status->type)) (此处$status->type值原本是 APP\ ...
- DG-V$MANAGED_STANDBY视图
V$MANAGED_STANDBY displays current status information for some Oracle Database processes related to ...
- python的final class
https://zhuanlan.zhihu.com/p/31674972 https://rainmanwy.github.io/Python的final-Class/
- Hive 窗口函数之 lead() over(partition by ) 和 lag() over(partition by )
lead函数用于提取当前行前某行的数据 lag函数用于提取当前行后某行的数据 语法如下: lead(expression,offset,default) over(partition by ... o ...