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>
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(0, 8);
DateTime dtime = DateTime.ParseExact(fileDate, "yyyyMMdd", null);
if (dtime >= sTime && dtime <= eTime)
{
listFJName.Add(NextFile.Name);
}
}
}
if (listFJName.Count > 0)
return "Exists";
return "NotExists";
}
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(0, 8);
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 > 0 && listFJName.Count > 0)
{
string time = DateTime.Now.Ticks.ToString();
ZipFileMain(listFJ.ToArray(), listFJName.ToArray(), Server.MapPath("~/Content/ExcelFile/" + time + ".zip"), 9);
DownloadFile(Server.UrlEncode("附件.zip"), Server.MapPath("~/Content/ExcelFile/" + time + ".zip"));
}
}
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();
}
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);
try
{
int m = 0;
foreach (string file in filenames)
{
FileStream fs = System.IO.File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, 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, 0, buffer.Length);
m++;
}
}
catch
{
throw;
}
finally
{
s.Finish();
s.Close();
}
}