/// <summary>
/// 指定路径打包下载
/// </summary>
/// <param name="fileName"></param>
/// <param name="filePath"></param>
public void ZIPfiles(DataTable dt)
{ //string filename = "开店日期(正常).xlsx";
//string filepath = "C:/ExportFolder/CADD87B4EDBE28A47669673F53DEC8458A998D5B84EEAA88EE6647CA6DF1D1E8.xlsx";
string ZipName = string.Empty;
MemoryStream ms = new MemoryStream();
Encoding gb2312 = Encoding.GetEncoding("gb2312"); //对方英文服务器 进行转码
ICSharpCode.SharpZipLib.Zip.ZipConstants.DefaultCodePage = gb2312.CodePage;//上面和这一句是为了防止真是坏境是英文服务器出现中文乱码的问题。
ZipOutputStream zos = new ZipOutputStream(ms);
FileStream fs = null;
// byte[] buffer = null;
System.IO.BinaryReader br = null; try
{
if (dt.Rows.Count > )
{
//防止名称重复 这一段肯定是有点消耗性能的 暂时没有找出更好的方法改进。 请各位大神有好的意见 说一下。
for (int i = dt.Rows.Count - ; i > ; i--)
{
int a = ;
string title = dt.Rows[]["fileName"].ToString();
Logger.Log.Debug(title);
for (int j = i + ; j > ; j--)
{
if (dt.Rows[j]["fileName"].ToString() == title)
{
a++;
dt.Rows[j]["fileName"] = title + "("+a+")"; }
Logger.Log.Debug(dt.Rows[j]["fileName"]);
} } foreach (DataRow dr in dt.Rows)
{
Logger.Log.Debug(dr["filePath"].ToString());
fs = new FileStream(dr["filePath"].ToString(), System.IO.FileMode.Open);//文件地址、
// fs = new FileStream(filepath, System.IO.FileMode.Open);//文件地址
br = new BinaryReader((Stream)fs);
byte[] buffer = br.ReadBytes((Int32)fs.Length);
ZipEntry entry = new ZipEntry(dr["fileName"].ToString());//文件名
//ZipEntry entry = new ZipEntry(filename);//文件名
zos.PutNextEntry(entry);//UTF-8
zos.Write(buffer, , buffer.Length);
fs.Close(); //每一个文件打开之后 然后执行完就必须在循环中关闭, 否则就会出现进程冲突
}
ZipName = DateTime.Now.ToString("yyyyMMdd");
Logger.Log.Debug(ZipName);
}
}
catch (Exception ex)
{
Logger.Log.Error("ZIP打包错误" + ex);
throw (ex);
} zos.Close(); HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(ZipName + ".zip", System.Text.Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", ms.ToArray().Length.ToString());
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}