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 == "")
{
code = "491dc61d999a414f86a4fe16933ce1df";
}
DataTable dt = dal.getDataSetfile(code).Tables[];
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 =;
if (part != )
{
times = (DtCount + part - ) / 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 > )
{
////测试数据
//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 = ; 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 + ) + ")" + houzhui;
dt.Rows[i]["fileName"] = NewName;
} Logger.Log.Debug("datable总数" + dt.Rows.Count); //part 等于0则直接导出全部
if (part == )
{
//判断文件是否存在
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 = ;
int a = ;
//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 = ;
byte[] buffer = br.ReadBytes(size);
fs.Seek(size * (row - ), System.IO.SeekOrigin.Begin);
ZipEntry entry = new ZipEntry(dr["fileName"].ToString());//文件名
//ZipEntry entry = new ZipEntry(filename);//文件名
zos.PutNextEntry(entry);//UTF-8
int extractCount = ;
while (true)
{
size = fs.Read(buffer, , buffer.Length);
if (size > )
{ zos.Write(buffer, , 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=;
int end=part;
int row = ;
int a = ;
//根据次数分别导出ZIP
for (int item = ; 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 = ;
byte[] buffer = br.ReadBytes(size);
fs.Seek(size * (row - ), 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, , 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; }
}
}

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. ionic 3 icon和splash screen生成和设置

    官方文档中介绍 ionic cordova resources命令可以生成应用的图标和启动画面图片(前提是你必须在resources 目录下放icon源文件和splash源文件,格式可以为png, p ...

  2. C#使用NanUI或ChromiumFx碰到的坑(一)

    最近在花时间封装一个Razor模板+NanUI的Winform组件,发现了有个神奇地方,,由于需要使用CfxResourceHandler,用于把对cshtml文件的请求,编译成html并返回给CEF ...

  3. Zlib:error can't decompress data; zlib not available

    查看:yum list |grep zlib* 看到的是全部都安装好的: 版本为1.2.3,现在要升级为1.2.11 卸载 [root@biluos1 zlib-1.2.11]# rpm –nodep ...

  4. Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0 报错

    此报错经常出现,项目中使用的maven版本为3.2.5版本但是去写自动化脚本又需要去3.5.2版本.经常搞混,需要记录一下: 解决如下: 再次install如下: 验证成功!

  5. XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Saratov

    A. Three Arrays 枚举每个$a_i$,双指针出$b$和$c$的范围,对于$b$中每个预先双指针出$c$的范围,那么对于每个$b$,在对应$c$的区间加$1$,在$a$处区间求和即可. 树 ...

  6. webpack实现开发、测试、生产等环境的打包切换

    使用webpack构建的工程,在开发过程中不同环境的配置不同,在各种环境的打包切换过程中需要手动修改相关配置达到预期目的.但是每次都手动修改会比较麻烦,本文简单介绍如何通过对webpack进行配置,实 ...

  7. 对迭代器操作的python 模块

    import itertools import more_itertools 目前用到的more_itertools.ilen(range(10)) --->返回可迭代的数量.这回消耗迭代,小心 ...

  8. idea使用配置

    一,打开窗口多行显示, Window→Editor Tabs→Tabs Placement→Show Tabs in Single Row 取消选中后即可在多行显示 2 .还可以自行设置打开文件窗口数 ...

  9. Caused by: java.util.concurrent.RejectedExecutionException: Thread pool is EXHAUSTED! Thread Name:

    异常引发的问题: 线程模型 如果事件处理的逻辑能迅速完成,并且不会发起新的 IO 请求,⽐如只是在内存中记个标识,则直接在 IO 线程上处理更快,因为减少了线程池调度. 但如果事件处理逻辑较慢,或者需 ...

  10. Jmeter学习系列----3 配置元件之计数器

    在做测试时,会遇到一种需求:在大量数据的情况下,数据不能重复或者需要自增,基于这种形式,我们可以考虑使用计数器. 计数器(counter): 计数器配置允许用户配置起始点,最大值和增量. 计数器将从开 ...