using Common; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Quartz; using Quartz.Impl; using StarbucksMessageService.Common; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using Topshelf; using System.IO; using ICSharpCode.SharpZipLib.Zip; using System.Web; using ICSharpCode.SharpZipLib.Checksums; namespace StarbucksMessageService { class Program { static void Main(string[] args) { DAL dal = new DAL(); service ser=new service(); Console.WriteLine("輸入須要導出的分類code"); //分類Code string code = Console.ReadLine(); if (code == "1") { code = "491dc61d999a414f86a4fe16933ce1df"; } DataTable dt = dal.getDataSetfile(code).Tables[0]; Console.WriteLine("當前文件總數:"+dt.Rows.Count); ////導出數量 //int classifyCount = int.Parse(Console.ReadLine()); Console.WriteLine("按多少份分別導出?(輸入0則導出所有)"); //部分區間 int part = int.Parse(Console.ReadLine()); //獲取數據庫數據 //總數 int DtCount = dt.Rows.Count; //須要分的次數 int times =0; if (part != 0) { times = (DtCount + part - 1) / part; } string ZipedFile = "/測試ZIP.zip";// "C:\\ExportFolder\\測試ZIP.zip"; string path= Path.GetFullPath("../ExportFolder"); string ZipName = string.Empty; var crc = new Crc32(); MemoryStream ms = new MemoryStream(); Encoding gb2312 = Encoding.GetEncoding("gb2312"); //對方英文服務器 進行轉碼 ICSharpCode.SharpZipLib.Zip.ZipConstants.DefaultCodePage = gb2312.CodePage; ZipOutputStream zos = null; FileStream fs = null; // byte[] buffer = null; System.IO.BinaryReader br = null; try { if (dt.Rows.Count > 0) { ////測試數據 //DataRow dr1 = dt.NewRow(); //for (int d = 0; d < 5000; d++) //{ // dr1["number"] = dt.Rows[5][0].ToString(); // dr1["Emp_Code"] = dt.Rows[5][1].ToString(); // dr1["applicantDeptName"] = dt.Rows[5][2].ToString(); // dr1["serialNumber"] = dt.Rows[5][3].ToString(); // dr1["fileName"] = dt.Rows[5][4].ToString(); // dr1["filePath"] = dt.Rows[5][5].ToString(); // dt.Rows.Add(dr1.ItemArray); //} for (int i = 0; i < dt.Rows.Count; i++) { string title = dt.Rows[i]["fileName"].ToString(); string houzhui = Path.GetExtension(title); title = Path.GetFileNameWithoutExtension(title); //上海咖啡店_s57155_SJ201904160001(1).jpg string NewName = dt.Rows[i]["applicantDeptName"].ToString() + "_" + dt.Rows[i]["Emp_Code"].ToString() + "_" + dt.Rows[i]["serialNumber"].ToString() + "(" + (i + 1) + ")" + houzhui; dt.Rows[i]["fileName"] = NewName; } Logger.Log.Debug("datable總數" + dt.Rows.Count); //part 等於0則直接導出所有 if (part == 0) { //判斷文件是否存在 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //判斷文件是否存在 if (File.Exists((path + ZipedFile))) { File.Delete((path + ZipedFile)); } zos = new ZipOutputStream(File.Create((path + ZipedFile))); int row = 1; int a = 1; //List<USP_getZIPlist> list = service.ListConvertToModel(dt); ////重命名 店名+店號+單號+流水號 //Parallel.ForEach(list, (i) => //{ // try // { // i // Logger.Log.Debug(i["filePath"].ToString()); // _error = i["fileName"].ToString(); // fs = new FileStream(dr["filePath"].ToString(), System.IO.FileMode.Open);//文件地址、 // // fs = new FileStream(filepath, System.IO.FileMode.Open);//文件地址 // br = new BinaryReader((Stream)fs); // int size = 1024; // byte[] buffer = br.ReadBytes(size); // fs.Seek(size * (row - 1), System.IO.SeekOrigin.Begin); // ZipEntry entry = new ZipEntry(dr["fileName"].ToString());//文件名 // //ZipEntry entry = new ZipEntry(filename);//文件名 // zos.PutNextEntry(entry);//UTF-8 // int extractCount = 0; // while (true) // { // size = fs.Read(buffer, 0, buffer.Length); // if (size > 0) // { // zos.Write(buffer, 0, size); // zos.Flush(); // } // else // { // break; // } // extractCount += size; // } // // zos.Write(buffer, 0, buffer.Length); // } // catch (Exception ex) // { // throw; // } // finally // { // if (fs != null) fs.Close(); // } // Console.WriteLine(a++); // //row++; //}); foreach (DataRow dr in dt.Rows) { try { Logger.Log.Debug(dr["filePath"].ToString()); _error = dr["fileName"].ToString(); if (File.Exists(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); int size = 1024; byte[] buffer = br.ReadBytes(size); fs.Seek(size * (row - 1), System.IO.SeekOrigin.Begin); ZipEntry entry = new ZipEntry(dr["fileName"].ToString());//文件名 //ZipEntry entry = new ZipEntry(filename);//文件名 zos.PutNextEntry(entry);//UTF-8 int extractCount = 0; while (true) { size = fs.Read(buffer, 0, buffer.Length); if (size > 0) { zos.Write(buffer, 0, size); zos.Flush(); } else { break; } extractCount += size; } } else { Console.WriteLine("單號:" + dr["serialNumber"].ToString() + ",文件:" + dr["filePath"].ToString() + ",不存在"); } // zos.Write(buffer, 0, buffer.Length); } catch (Exception ex) { throw; } finally { if (fs != null) fs.Close(); } Console.WriteLine("序號:"+a++); //row++; } if (zos != null) 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(); } else { int begin=0; int end=part; int row = 1; int a = 1; //根據次數分別導出ZIP for (int item = 1; item <= times; item++) { ZipedFile = "/測試ZIP(" + item + ").zip";// "C:\\ExportFolder\\測試ZIP(" + item + ").zip"; //判斷文件是否存在 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //判斷文件是否存在 if (File.Exists((path + ZipedFile))) { File.Delete((path + ZipedFile)); } zos = new ZipOutputStream(File.Create((path + ZipedFile))); //每次循環根據固定份數打包 part DataRow [] dRow = dt.Select("number>" + begin + " and number<=" + end + ""); foreach (DataRow dr in dRow) { try { Logger.Log.Debug(dr["filePath"].ToString()); _error = dr["fileName"].ToString(); if (File.Exists(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); int size = 1024; byte[] buffer = br.ReadBytes(size); fs.Seek(size * (row - 1), System.IO.SeekOrigin.Begin); //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, 0, buffer.Length); } else { Console.WriteLine("單號:" + dr["serialNumber"].ToString() + ",文件:" + dr["filePath"].ToString() + ",不存在"); } } catch (Exception ex) { Console.WriteLine("ZIP打包錯誤" + ex); } finally { if (fs != null) fs.Close(); } Console.WriteLine("序號:" + a++); } if (zos != null) zos.Close(); //每次循環 加上份數 begin=begin+part; end=end+part; } } } } catch (Exception ex) { // context.Response.Write("<script>alert('" + _error + "文件不存在或被另外一進程佔用,文件下載終止')</script>"); Console.WriteLine("ZIP打包錯誤" + ex); } finally { if (zos != null) zos.Close(); ms.Dispose(); } Console.ReadKey(); } public static string _error { get; set; } } }