1. using Common;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using Quartz;
  5. using Quartz.Impl;
  6. using StarbucksMessageService.Common;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.Specialized;
  10. using System.Data;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using Topshelf;
  15. using System.IO;
  16. using ICSharpCode.SharpZipLib.Zip;
  17. using System.Web;
  18. using ICSharpCode.SharpZipLib.Checksums;
  19. namespace StarbucksMessageService
  20. {
  21. class Program
  22. {
  23. static void Main(string[] args)
  24. {
  25. DAL dal = new DAL();
  26. service ser=new service();
  27. Console.WriteLine("输入需要导出的分类code");
  28. //分类Code
  29. string code = Console.ReadLine();
  30. if (code == "")
  31. {
  32. code = "491dc61d999a414f86a4fe16933ce1df";
  33. }
  34. DataTable dt = dal.getDataSetfile(code).Tables[];
  35. Console.WriteLine("当前文件总数:"+dt.Rows.Count);
  36. ////导出数量
  37. //int classifyCount = int.Parse(Console.ReadLine());
  38. Console.WriteLine("按多少份分别导出?(输入0则导出全部)");
  39. //部分区间
  40. int part = int.Parse(Console.ReadLine());
  41. //获取数据库数据
  42.  
  43. //总数
  44. int DtCount = dt.Rows.Count;
  45. //需要分的次数
  46. int times =;
  47. if (part != )
  48. {
  49. times = (DtCount + part - ) / part;
  50. }
  51. string ZipedFile = "/测试ZIP.zip";// "C:\\ExportFolder\\测试ZIP.zip";
  52. string path= Path.GetFullPath("../ExportFolder");
  53. string ZipName = string.Empty;
  54. var crc = new Crc32();
  55. MemoryStream ms = new MemoryStream();
  56. Encoding gb2312 = Encoding.GetEncoding("gb2312"); //对方英文服务器 进行转码
  57. ICSharpCode.SharpZipLib.Zip.ZipConstants.DefaultCodePage = gb2312.CodePage;
  58. ZipOutputStream zos = null;
  59. FileStream fs = null;
  60. // byte[] buffer = null;
  61. System.IO.BinaryReader br = null;
  62.  
  63. try
  64. {
  65.  
  66. if (dt.Rows.Count > )
  67. {
  68. ////测试数据
  69. //DataRow dr1 = dt.NewRow();
  70. //for (int d = 0; d < 5000; d++)
  71. //{
  72. // dr1["number"] = dt.Rows[5][0].ToString();
  73. // dr1["Emp_Code"] = dt.Rows[5][1].ToString();
  74. // dr1["applicantDeptName"] = dt.Rows[5][2].ToString();
  75. // dr1["serialNumber"] = dt.Rows[5][3].ToString();
  76. // dr1["fileName"] = dt.Rows[5][4].ToString();
  77. // dr1["filePath"] = dt.Rows[5][5].ToString();
  78. // dt.Rows.Add(dr1.ItemArray);
  79.  
  80. //}
  81.  
  82. for (int i = ; i < dt.Rows.Count; i++)
  83. {
  84. string title = dt.Rows[i]["fileName"].ToString();
  85. string houzhui = Path.GetExtension(title);
  86. title = Path.GetFileNameWithoutExtension(title);
  87. //上海咖啡店_s57155_SJ201904160001(1).jpg
  88. string NewName = dt.Rows[i]["applicantDeptName"].ToString() + "_" + dt.Rows[i]["Emp_Code"].ToString() + "_" + dt.Rows[i]["serialNumber"].ToString() + "(" + (i + ) + ")" + houzhui;
  89. dt.Rows[i]["fileName"] = NewName;
  90. }
  91.  
  92. Logger.Log.Debug("datable总数" + dt.Rows.Count);
  93.  
  94. //part 等于0则直接导出全部
  95. if (part == )
  96. {
  97. //判断文件是否存在
  98. if (!Directory.Exists(path))
  99. {
  100. Directory.CreateDirectory(path);
  101. }
  102. //判断文件是否存在
  103. if (File.Exists((path + ZipedFile)))
  104. {
  105. File.Delete((path + ZipedFile));
  106. }
  107. zos = new ZipOutputStream(File.Create((path + ZipedFile)));
  108. int row = ;
  109. int a = ;
  110. //List<USP_getZIPlist> list = service.ListConvertToModel(dt);
  111. ////重命名 店名+店号+单号+流水号
  112. //Parallel.ForEach(list, (i) =>
  113. //{
  114. // try
  115. // {
  116. // i
  117. // Logger.Log.Debug(i["filePath"].ToString());
  118. // _error = i["fileName"].ToString();
  119. // fs = new FileStream(dr["filePath"].ToString(), System.IO.FileMode.Open);//文件地址、
  120. // // fs = new FileStream(filepath, System.IO.FileMode.Open);//文件地址
  121. // br = new BinaryReader((Stream)fs);
  122. // int size = 1024;
  123. // byte[] buffer = br.ReadBytes(size);
  124. // fs.Seek(size * (row - 1), System.IO.SeekOrigin.Begin);
  125. // ZipEntry entry = new ZipEntry(dr["fileName"].ToString());//文件名
  126. // //ZipEntry entry = new ZipEntry(filename);//文件名
  127. // zos.PutNextEntry(entry);//UTF-8
  128. // int extractCount = 0;
  129. // while (true)
  130. // {
  131. // size = fs.Read(buffer, 0, buffer.Length);
  132. // if (size > 0)
  133. // {
  134.  
  135. // zos.Write(buffer, 0, size);
  136. // zos.Flush();
  137. // }
  138. // else
  139. // {
  140. // break;
  141. // }
  142. // extractCount += size;
  143. // }
  144. // // zos.Write(buffer, 0, buffer.Length);
  145. // }
  146. // catch (Exception ex)
  147. // {
  148. // throw;
  149. // }
  150. // finally
  151. // {
  152. // if (fs != null) fs.Close();
  153. // }
  154. // Console.WriteLine(a++);
  155. // //row++;
  156.  
  157. //});
  158. foreach (DataRow dr in dt.Rows)
  159. {
  160. try
  161. {
  162. Logger.Log.Debug(dr["filePath"].ToString());
  163. _error = dr["fileName"].ToString();
  164. if (File.Exists(dr["filePath"].ToString()))
  165. {
  166. fs = new FileStream(dr["filePath"].ToString(), System.IO.FileMode.Open);//文件地址、
  167.  
  168. // fs = new FileStream(filepath, System.IO.FileMode.Open);//文件地址
  169. br = new BinaryReader((Stream)fs);
  170. int size = ;
  171. byte[] buffer = br.ReadBytes(size);
  172. fs.Seek(size * (row - ), System.IO.SeekOrigin.Begin);
  173. ZipEntry entry = new ZipEntry(dr["fileName"].ToString());//文件名
  174. //ZipEntry entry = new ZipEntry(filename);//文件名
  175. zos.PutNextEntry(entry);//UTF-8
  176. int extractCount = ;
  177. while (true)
  178. {
  179. size = fs.Read(buffer, , buffer.Length);
  180. if (size > )
  181. {
  182.  
  183. zos.Write(buffer, , size);
  184. zos.Flush();
  185. }
  186. else
  187. {
  188. break;
  189. }
  190. extractCount += size;
  191. }
  192. }
  193. else
  194. {
  195. Console.WriteLine("单号:" + dr["serialNumber"].ToString() + ",文件:" + dr["filePath"].ToString() + ",不存在");
  196. }
  197. // zos.Write(buffer, 0, buffer.Length);
  198. }
  199. catch (Exception ex)
  200. {
  201. throw;
  202. }
  203. finally
  204. {
  205. if (fs != null) fs.Close();
  206. }
  207. Console.WriteLine("序号:"+a++);
  208. //row++;
  209. }
  210.  
  211. if (zos != null) zos.Close();
  212.  
  213. //HttpContext.Current.Response.ContentType = "application/octet-stream";
  214. //HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(ZipName + ".zip", System.Text.Encoding.UTF8));
  215. //HttpContext.Current.Response.AddHeader("Content-Length", ms.ToArray().Length.ToString());
  216. //HttpContext.Current.Response.BinaryWrite(ms.ToArray());
  217. //HttpContext.Current.Response.Flush();
  218. // HttpContext.Current.Response.End();
  219. }
  220. else
  221. {
  222. int begin=;
  223. int end=part;
  224. int row = ;
  225. int a = ;
  226. //根据次数分别导出ZIP
  227. for (int item = ; item <= times; item++)
  228. {
  229. ZipedFile = "/测试ZIP(" + item + ").zip";// "C:\\ExportFolder\\测试ZIP(" + item + ").zip";
  230. //判断文件是否存在
  231. if (!Directory.Exists(path))
  232. {
  233. Directory.CreateDirectory(path);
  234. }
  235. //判断文件是否存在
  236. if (File.Exists((path + ZipedFile)))
  237. {
  238. File.Delete((path + ZipedFile));
  239. }
  240. zos = new ZipOutputStream(File.Create((path + ZipedFile)));
  241. //每次循环根据固定份数打包 part
  242. DataRow [] dRow = dt.Select("number>" + begin + " and number<=" + end + "");
  243. foreach (DataRow dr in dRow)
  244. {
  245. try
  246. {
  247. Logger.Log.Debug(dr["filePath"].ToString());
  248. _error = dr["fileName"].ToString();
  249. if (File.Exists(dr["filePath"].ToString()))
  250. {
  251. fs = new FileStream(dr["filePath"].ToString(), System.IO.FileMode.Open);//文件地址、
  252. // fs = new FileStream(filepath, System.IO.FileMode.Open);//文件地址
  253. br = new BinaryReader((Stream)fs);
  254. int size = ;
  255. byte[] buffer = br.ReadBytes(size);
  256. fs.Seek(size * (row - ), System.IO.SeekOrigin.Begin);
  257. //byte[] buffer = br.ReadBytes((Int32)fs.Length);
  258. ZipEntry entry = new ZipEntry(dr["fileName"].ToString());//文件名
  259. //ZipEntry entry = new ZipEntry(filename);//文件名
  260. zos.PutNextEntry(entry);//UTF-8
  261. zos.Write(buffer, , buffer.Length);
  262. }
  263. else
  264. {
  265. Console.WriteLine("单号:" + dr["serialNumber"].ToString() + ",文件:" + dr["filePath"].ToString() + ",不存在");
  266. }
  267. }
  268.  
  269. catch (Exception ex)
  270. {
  271. Console.WriteLine("ZIP打包错误" + ex);
  272. }
  273. finally
  274. {
  275. if (fs != null) fs.Close();
  276. }
  277. Console.WriteLine("序号:" + a++);
  278. }
  279.  
  280. if (zos != null) zos.Close();
  281.  
  282. //每次循环 加上份数
  283. begin=begin+part;
  284. end=end+part;
  285.  
  286. }
  287. }
  288. }
  289.  
  290. }
  291. catch (Exception ex)
  292. {
  293. // context.Response.Write("<script>alert('" + _error + "文件不存在或被另一进程占用,文件下载终止')</script>");
  294. Console.WriteLine("ZIP打包错误" + ex);
  295.  
  296. }
  297. finally
  298. {
  299.  
  300. if (zos != null) zos.Close();
  301. ms.Dispose();
  302. }
  303. Console.ReadKey();
  304. }
  305.  
  306. public static string _error { get; set; }
  307. }
  308. }

ZIP压缩指定路径的更多相关文章

  1. Zip文件压缩(加密||非加密||压缩指定目录||压缩目录下的单个文件||根据路径压缩||根据流压缩)

    1.写入Excel,并加密压缩.不保存文件 String dcxh = String.format("%03d", keyValue); String folderFileName ...

  2. Python压缩指定文件及文件夹为zip

    Python压缩指定的文件及文件夹为.zip 代码: def zipDir(dirpath,outFullName): """ 压缩指定文件夹 :param dirpat ...

  3. 利用WebUploader进行图片批量上传,在页面显示后选择多张图片压缩至指定路径【java】

    WebUploader是由Baidu WebFE(FEX)团队开发的一个简单的以HTML5为主,FLASH为辅的现代文件上传组件.在现代的浏览器里面能充分发挥HTML5的优势,同时又不摒弃主流IE浏览 ...

  4. java 压缩文件 传入文件数组,压缩文件,在指定路径下生成指定文件名的压缩文件

    /** * 传入文件数组,压缩文件,在指定路径下生成指定文件名的压缩文件 * * @param files * 文件数组 * @param strZipName * 压缩文件路径及文件名 * @thr ...

  5. Zip 压缩和解压技术在 HTML5 中的应用

    JSZip 是一款可以创建.读取.修改 .zip 文件的 javaScript 工具.在 web 应用中,免不了需要从 web 服务器中获取资源,如果可以将所有的资源都合并到一个 .zip 文件中,这 ...

  6. Zip 压缩、解压技术在 HTML5 浏览器中的应用

    JSZip 是一款可以创建.读取.修改 .zip 文件的 javaScript 工具.在 web 应用中,免不了需要从 web 服务器中获取资源,如果可以将所有的资源都合并到一个 .zip 文件中,这 ...

  7. C#实现Zip压缩解压实例【转】

    本文只列举一个压缩帮助类,使用的是有要添加一个dll引用ICSharpCode.SharpZipLib.dll[下载地址]. 另外说明一下的是,这个类压缩格式是ZIP的,所以文件的后缀写成 .zip. ...

  8. ZIP压缩文件夹中上个月的文件,并将备份文件拷贝到服务器

    遍历文件夹的子文件夹下的所有文件,将上个月的文件集中到一起,然互压缩,并copy到服务器的映射磁盘. static void Main(string[] args) { //原始文件存放的位置 Dir ...

  9. PHP zip压缩文件及解压

    PHP zip压缩文件及解压 利用ZipArchive 类实现 只有有函数.界面大家自己写 ZipArchive(PHP 5.3 + 已自带不需要安装dll) /** * 文件解压 * @param ...

随机推荐

  1. python实现Hbase

    1. 下载thrift 作用:翻译python语言为hbase语言的工具 2. 运行时先启动hbase 再启动thrift,最后在pycharm中通过happybase包连接hbase 在hbase目 ...

  2. Beta(1/7)

    鐵鍋燉腯鱻 项目:小鱼记账 团队成员 项目燃尽图 冲刺情况描述 站立式会议照片 各成员情况 团队成员 学号 姓名 git地址 博客地址 031602240 许郁杨 (组长) https://githu ...

  3. ISP PIPLINE (二) LensShading Correct

    what is the LSC? lens shading 分为:Y-shading , color shading. 在讲LSC之前,我们先来理解一个重要的术语--CRA(Chief ray ang ...

  4. 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest

    A. Automatic Door 对于规律的点可以推公式计算,对于噪点则暴力计算,时间复杂度$O(m\log m)$. #include<stdio.h> #include<ios ...

  5. TypeScript系列 - 什么是TypeScript

    看了很多关于TypeScript的文章,总体说来没有很好的,一个系统的学习TypeScript的资源. 接下来,我将给大家带来TypeScript的系列,让你和我一样,一步一步的学习TypeScrip ...

  6. 移动端滑动效果 swiper 4.0.7

    <!DOCTYPE html><html lang="en"><head> <meta charset="utf-8" ...

  7. python的array初识

    from array import array """ 'b' signed char int 1 'B' unsigned char int 1 'u' Py_UNIC ...

  8. List GroupBy真实用法,Reflection(反射)用法,Enum用法,正则,搜索下拉布局

    1.List  GroupBy 用法 var _roomProducts = homesingProducts.GroupBy(t => t.RoomName); RoomedProducts ...

  9. MyISAM和Innodb区别,为什么?

    事务支持 MyISAM不支持事务,而InnoDB支持. InnoDB的AUTOCOMMIT默认是打开的,即每条SQL语句会默认被封装成一个事务,自动提交,这样会影响速度, 所以最好是把多条SQL语句显 ...

  10. Wireshark简单使用教程3——附视频

    视频https://www.bilibili.com/video/av35763613?from=search&seid=10176480091153063668 目录 抓取干净流量包的用处所 ...