public class ZipFileHelper
{
List<string> urls = new List<string>();
void Director(string dir)
{
DirectoryInfo d = new DirectoryInfo(dir);
FileSystemInfo[] fsinfos = d.GetFileSystemInfos();
foreach (FileSystemInfo fsinfo in fsinfos)
{
if (fsinfo is DirectoryInfo) //判断是否为文件夹
{
Director(fsinfo.FullName);//递归调用
}
else
{
// Console.WriteLine(fsinfo.FullName);//输出文件的全部路径
urls.Add(fsinfo.FullName);
} }
}
///<summary>
/// 创建ZIP文件
///</summary>
public void CreateZipFile(string dir, string sTempFile, string sPassWord)
{
try
{
Director(dir);
var files = urls.ToArray();
using (ZipOutputStream s = new ZipOutputStream(File.Create(sTempFile)))
{
s.SetLevel(); // 压缩级别 0-9
if (sPassWord != "")
{
s.Password = sPassWord; //Zip压缩文件密码
} byte[] buffer = new byte[]; //缓冲区大小 foreach (string file in files)
{
if (!string.IsNullOrEmpty(file))
{
if (File.Exists(file))
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry); using (FileStream fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, , buffer.Length);
s.Write(buffer, , sourceBytes);
} while (sourceBytes > );
}
}
else
{
//logger.Error("文件:" + file + "不存在。");
}
}
} s.Finish();
s.Close();
}
}
catch (Exception ex)
{
//logger.Error("压缩文件时异常!");
//logger.Error("异常描述:\t" + ex.Message);
//logger.Error("异常方法:\t" + ex.TargetSite);
//logger.Error("异常堆栈:\t" + ex.StackTrace);
}
} /// <summary>
///
/// </summary>
/// <param name="files">放入ZIP的文件路劲(含文件名)</param>
/// <param name="sTempFile">创建的ZIP文件路劲(含文件名)</param>
/// <param name="sPassWord">ZIP文件密码</param>
/// <param name="folderNames">存放到ZIP中的文件夹名,空代表放在顶级目录</param>
public void CreateZipFileMutilFolder(string[] files, string sTempFile, string sPassWord, string[] folderNames)
{
try
{
using (ZipOutputStream s = new ZipOutputStream(File.Create(sTempFile)))
{
s.SetLevel(); // 压缩级别 0-9
if (sPassWord != "")
{
s.Password = sPassWord; //Zip压缩文件密码
} byte[] buffer = new byte[]; //缓冲区大小 int i = ;
foreach (string file in files)
{
if (!string.IsNullOrEmpty(file))
{
if (File.Exists(file))
{
ZipEntry entry = new ZipEntry((string.IsNullOrEmpty(folderNames[i]) ? "" : (folderNames[i] + "\\")) + Path.GetFileName(file));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry); using (FileStream fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, , buffer.Length);
s.Write(buffer, , sourceBytes);
} while (sourceBytes > );
}
}
else
{
//logger.Error("文件:" + file + "不存在。");
}
} i++;
} s.Finish();
s.Close();
}
}
catch (Exception ex)
{
//logger.Error("压缩文件时异常!");
//logger.Error("异常描述:\t" + ex.Message);
//logger.Error("异常方法:\t" + ex.TargetSite);
//logger.Error("异常堆栈:\t" + ex.StackTrace);
}
}
}

ICSharpCode.SharpZipLib.Zip 压缩文件的更多相关文章

  1. 利用ICSharpCode.SharpZipLib.Zip进行文件压缩

    官网http://www.icsharpcode.net/ 支持文件和字符压缩. 创建全新的压缩包 第一步,创建压缩包 using ICSharpCode.SharpZipLib.Zip; ZipOu ...

  2. C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件

    大家可以到http://www.icsharpcode.net/opensource/sharpziplib/ 下载SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, ...

  3. c# ICSharpCode.SharpZipLib.Zip实现文件的压缩

    首先了解ZipOutPutStream和ZipEntry对象 ZipOutPutStream对象 如果要完成一个文件或文件夹的压缩,则要使用ZipOutputStream类.ZipOutputStre ...

  4. ICSharpCode.SharpZipLib工具压缩与解压缩zip文件

    using System; using System.Collections.Generic; using System.IO; using System.Text; using ICSharpCod ...

  5. C#使用ICSharpCode.SharpZipLib.dll压缩多个文件

    首先通过NuGet管理安装ICSharpCode.SharpZipLib.dll 以下是压缩的通用方法: using System; using System.IO; using System.Web ...

  6. 基于ICSharpCode.SharpZipLib.Zip的压缩解压缩

    原文:基于ICSharpCode.SharpZipLib.Zip的压缩解压缩 今天记压缩解压缩的使用,是基于开源项目ICSharpCode.SharpZipLib.Zip的使用. 一.压缩: /// ...

  7. C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  8. 使用ICSharpCode.SharpZipLib.Zip实现压缩与解压缩

    使用开源类库ICSharpCode.SharpZipLib.Zip可以实现压缩与解压缩功能,源代码和DLL可以从http://www.icsharpcode.net/OpenSource/SharpZ ...

  9. C#调用 ICSharpCode.SharpZipLib.Zip 实现解压缩功能公用类

    最近想用个解压缩功能 从网上找了找 加自己修改,个人感觉还是比较好用的,直接上代码如下 using System; using System.Linq; using System.IO; using ...

随机推荐

  1. Java拆箱装箱

    原文 http://www.cnblogs.com/dolphin0520/p/3780005.html

  2. gulp时发生错误---------const { Math, Object } = primordials;

    [问题描述] 执行完npm install后,对前台页面进行gulp操作时,报如下错误: const { Math, Object } = primordials; [错误日志] ***@**** M ...

  3. K8s开始

    部署前思考:

  4. Anaconda中安装pytorch

    Anaconda中安装pytorch 创建一个虚拟环境 conda create --name machinelearning python=3.7 激活虚拟环境 activate machinele ...

  5. python __dict__ 跟 dir()的区别

    __dict__:要是对象的话返回的是一个对象自身的实例属性.不包括类的属性:要是类的__dict__则不包括父类的属性,只包含自身类属性[方法.类变量],不包括实例属性.正是这样.每个实例的实例属性 ...

  6. CVE-2018-19985漏洞学习

    简介 4.19.8之前,在Linux内核中,hso_probe()函数中发现了一个缺陷,该函数从USB设备(作为u8)读取if_num值,并且不需要对数组进行长度检查就使用它来索引数组,从而导致在hs ...

  7. mysql 相关文章

    <58到家MySQL军规升级版> <数据库索引,到底是什么做的?> <MyISAM与InnoDB的索引差异究竟是啥?> <InnoDB,为何并发如此之高?&g ...

  8. 最好用linux版QQ

    这个版本的qq是见过linux下做好用的qq,希望对大家有用; 安装简单,qq易用,不卡死,可以接收文件. 安装过程如下: git clone https://gitee.com/wszqkzqk/d ...

  9. linux 下如何将网页版应用生成桌面图标

    使用linux mint已经两年了,很多国民应用,都没有Linux版,但是这些应用都有网页版,今天就说下最简单的将网页应用变成桌面应用,无需配置,安装任何插件.以微信为例; 首先,在谷歌浏览器打开网页 ...

  10. 外贸开发,用java调用速卖通api第一步,token的获取。

     第一步 定义速卖通api的常量  public String client_id;  public String client_key;  public String site;   第二步 获取登 ...