热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ZIP压缩指定路径

usingCommon;usingNewtonsoft.Json;usingNewtonsoft.Json.Linq;usingQuartz;usingQuartz.Impl;
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 )
                    {
                        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 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("");
                Console.WriteLine("ZIP打包错误" + ex);

            }
            finally
            {

                if (zos != null) zos.Close();
                ms.Dispose();
            }
            Console.ReadKey();
        }

        public static string _error { get; set; }
    }
}

 


推荐阅读
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文讨论了Kotlin中扩展函数的一些惯用用法以及其合理性。作者认为在某些情况下,定义扩展函数没有意义,但官方的编码约定支持这种方式。文章还介绍了在类之外定义扩展函数的具体用法,并讨论了避免使用扩展函数的边缘情况。作者提出了对于扩展函数的合理性的质疑,并给出了自己的反驳。最后,文章强调了在编写Kotlin代码时可以自由地使用扩展函数的重要性。 ... [详细]
  • 第四章高阶函数(参数传递、高阶函数、lambda表达式)(python进阶)的讲解和应用
    本文主要讲解了第四章高阶函数(参数传递、高阶函数、lambda表达式)的相关知识,包括函数参数传递机制和赋值机制、引用传递的概念和应用、默认参数的定义和使用等内容。同时介绍了高阶函数和lambda表达式的概念,并给出了一些实例代码进行演示。对于想要进一步提升python编程能力的读者来说,本文将是一个不错的学习资料。 ... [详细]
  • 本文讨论了编写可保护的代码的重要性,包括提高代码的可读性、可调试性和直观性。同时介绍了优化代码的方法,如代码格式化、解释函数和提炼函数等。还提到了一些常见的坏代码味道,如不规范的命名、重复代码、过长的函数和参数列表等。最后,介绍了如何处理数据泥团和进行函数重构,以提高代码质量和可维护性。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • 如何查询zone下的表的信息
    本文介绍了如何通过TcaplusDB知识库查询zone下的表的信息。包括请求地址、GET请求参数说明、返回参数说明等内容。通过curl方法发起请求,并提供了请求示例。 ... [详细]
  • 本文介绍了OpenStack的逻辑概念以及其构成简介,包括了软件开源项目、基础设施资源管理平台、三大核心组件等内容。同时还介绍了Horizon(UI模块)等相关信息。 ... [详细]
  • Android系统源码分析Zygote和SystemServer启动过程详解
    本文详细解析了Android系统源码中Zygote和SystemServer的启动过程。首先介绍了系统framework层启动的内容,帮助理解四大组件的启动和管理过程。接着介绍了AMS、PMS等系统服务的作用和调用方式。然后详细分析了Zygote的启动过程,解释了Zygote在Android启动过程中的决定作用。最后通过时序图展示了整个过程。 ... [详细]
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
author-avatar
mobiledu2502926443
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有