using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MakePdf
{
/// <summary>
/// Zip压缩与解压缩
/// </summary>
public static class ZipHelper
{
/// <summary>
/// 压缩单个文件
/// </summary>
/// <param name="fileToZip">要压缩的文件</param>
/// <param name="zipedFile">压缩后的文件</param>
/// <param name="compressionLevel">压缩等级</param>
/// <param name="blockSize">每次写入大小</param>
public static void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)
{
//如果文件没有找到,则报错
if (!System.IO.File.Exists(fileToZip))
{
throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
} using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
{
using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
{
using (System.IO.FileStream StreamToZip = new System.IO.FileStream(fileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + ); ZipEntry ZipEntry = new ZipEntry(fileName); ZipStream.PutNextEntry(ZipEntry); ZipStream.SetLevel(compressionLevel); byte[] buffer = new byte[blockSize]; int sizeRead = ; try
{
do
{
sizeRead = StreamToZip.Read(buffer, , buffer.Length);
ZipStream.Write(buffer, , sizeRead);
}
while (sizeRead > );
}
catch (System.Exception ex)
{
throw ex;
} StreamToZip.Close();
} ZipStream.Finish();
ZipStream.Close();
} ZipFile.Close();
}
} /// <summary>
/// 压缩单个文件
/// </summary>
/// <param name="fileToZip">要进行压缩的文件名</param>
/// <param name="zipedFile">压缩后生成的压缩文件名</param>
public static void ZipFile(string fileToZip, string zipedFile)
{
//如果文件没有找到,则报错
if (!File.Exists(fileToZip))
{
throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
} using (FileStream fs = File.OpenRead(fileToZip))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length);
fs.Close(); using (FileStream ZipFile = File.Create(zipedFile))
{
using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
{
string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + );
ZipEntry ZipEntry = new ZipEntry(fileName);
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(); ZipStream.Write(buffer, , buffer.Length);
ZipStream.Finish();
ZipStream.Close();
}
}
}
} /// <summary>
/// 压缩多层目录
/// </summary>
/// <param name="strDirectory">The directory.</param>
/// <param name="zipedFile">The ziped file.</param>
public static void ZipFileDirectory(string strDirectory, string zipedFile)
{
using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
{
using (ZipOutputStream s = new ZipOutputStream(ZipFile))
{
ZipSetp(strDirectory, s, "");
}
}
} /// <summary>
/// 递归遍历目录
/// </summary>
/// <param name="strDirectory">The directory.</param>
/// <param name="s">The ZipOutputStream Object.</param>
/// <param name="parentPath">The parent path.</param>
private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
{
if (strDirectory[strDirectory.Length - ] != Path.DirectorySeparatorChar)
{
strDirectory += Path.DirectorySeparatorChar;
}
Crc32 crc = new Crc32(); string[] filenames = Directory.GetFileSystemEntries(strDirectory); foreach (string file in filenames)// 遍历所有的文件和目录
{ if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
{
string pPath = parentPath;
pPath += file.Substring(file.LastIndexOf("\\") + );
pPath += "\\";
ZipSetp(file, s, pPath);
} else // 否则直接压缩文件
{
//打开压缩文件
using (FileStream fs = File.OpenRead(file))
{ byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length); string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + );
ZipEntry entry = new ZipEntry(fileName); entry.DateTime = DateTime.Now;
entry.Size = fs.Length; fs.Close(); crc.Reset();
crc.Update(buffer); entry.Crc = crc.Value;
s.PutNextEntry(entry); s.Write(buffer, , buffer.Length);
}
}
}
} /// <summary>
/// 解压缩一个 zip 文件。
/// </summary>
/// <param name="zipedFile">The ziped file.</param>
/// <param name="strDirectory">The STR directory.</param>
/// <param name="password">zip 文件的密码。</param>
/// <param name="overWrite">是否覆盖已存在的文件。</param>
public static void UnZip(string zipedFile, string strDirectory, string password, bool overWrite)
{ if (strDirectory == "")
strDirectory = Directory.GetCurrentDirectory();
if (!strDirectory.EndsWith("\\"))
strDirectory = strDirectory + "\\"; using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile)))
{
s.Password = password;
ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = "";
string pathToZip = "";
pathToZip = theEntry.Name; if (pathToZip != "")
directoryName = Path.GetDirectoryName(pathToZip) + "\\"; string fileName = Path.GetFileName(pathToZip); Directory.CreateDirectory(strDirectory + directoryName); if (fileName != "")
{
if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName)))
{
using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName))
{
int size = ;
byte[] data = new byte[];
while (true)
{
size = s.Read(data, , data.Length); if (size > )
streamWriter.Write(data, , size);
else
break;
}
streamWriter.Close();
}
}
}
} s.Close();
}
} }
}

ZipHelper類及SharpZipLib.dll

C#壓縮文件幫助類 使用ICSharpCode.SharpZipLib.dll的更多相关文章

  1. C#文件或文件夹压缩和解压方法(通过ICSharpCode.SharpZipLib.dll)

    我在网上收集一下文件的压缩和解压的方法,是通过ICSharpCode.SharpZipLib.dll 来实现的 一.介绍的目录 第一步:下载压缩和解压的 ICSharpCode.SharpZipLib ...

  2. C# ICSharpCode.SharpZipLib.dll文件压缩和解压功能类整理,上传文件或下载文件很常用

    工作中我们很多时候需要进行对文件进行压缩,比较通用的压缩的dll就是ICSharpCode.SharpZipLib.dll,废话不多了,网上也有很多的资料,我将其最常用的两个函数整理了一下,提供了一个 ...

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

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

  4. C# 压缩文件 ICSharpCode.SharpZipLib.dll

    效果: 代码只能压缩文件夹里面的文件,不能压缩文件夹. 压缩前: 压缩后: 代码: 需要引用ICSharpCode.SharpZipLib.dll public ActionResult Index( ...

  5. C# 下利用ICSharpCode.SharpZipLib.dll实现文件/目录压缩、解压缩

    ICSharpCode.SharpZipLib.dll下载地址 1.压缩某个指定文件夹下日志,将日志压缩到CompressionDirectory文件夹中,并清除原来未压缩日志. #region 压缩 ...

  6. zip (ICSharpCode.SharpZipLib.dll文件需要下载)

    ZipClass zc=new ZipClass (); zc.ZipDir(@"E:\1\新建文件夹", @"E:\1\新建文件夹.zip", 1);//压缩 ...

  7. C# 利用ICSharpCode.SharpZipLib.dll 实现压缩和解压缩文件

    我们 开发时经常会遇到需要压缩文件的需求,利用C#的开源组件ICSharpCode.SharpZipLib, 就可以很容易的实现压缩和解压缩功能. 压缩文件: /// <summary> ...

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

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

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

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

随机推荐

  1. Dynamic CRM 2013学习笔记(三)快速创建实体 EntityCreater

    一.实体简介 实体用于在 Microsoft Dynamics CRM 中建立业务数据模型和管理业务数据.例如,可以使用客户.市场活动和事件(案例)等实体跟踪和支持销售.市场营销和服务活动.实体具有一 ...

  2. django rest_framework

    环境 django 1.6,rest_framework 3.3 ubuntu采用pip安装的rest_framework 按照例子一步步做下来 运行 提示filters.py第119行有错误form ...

  3. C#课外实践——校园二手平台(技术篇1)

    前面分享了这次的课外实践的心得,这次,就分享一下从这次的课外实践的过程中学到的知识技能吧.虽然有句话说的好,不要做没有准备的战争,但是,我想说的是,生活中有很多的事情是不允许我们有准备的.遇到事情必须 ...

  4. paip.不同目录结构哈的文件批量比较

    paip.不同目录结构哈的文件批量比较 作者Attilax 艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/att ...

  5. VS 2008 创建MFC程序对话框的步骤

    用过不少编程语言,可是刚开始学的时候最容易忘记一些简单的流程或者生疏了.那么这里就说说VS 2008 创建MFC程序对话框的步骤.我主要是android开发方面的.平时使用jni调用不少c++代码.所 ...

  6. 出现Bad command or the file name的原因

    出现Bad command or file name的原因 中文释义:错误的命令或文件名 . 错误原因:不能识别输入的命令 . 方法:检查所输入的指令是否正确,包括拼写和大小写等情况.

  7. 对TCP说三道四

    夜朦胧,人方静,无聊的人打开了无聊的电脑看到了一张无聊的图,想着想着就睡着了,梦到了人a和人b的一次聊天.        有一天,a有事情想跟b商量就问b"有时间么,想和你聊一下天" ...

  8. 10TSQL语言概述-脚本调试-命名规范-天轰穿数据库2014

    关键字:sqlserver 数据库脚本 数据库 编码规范大纲:sql概念,TSQL脚本调试,数据库编码规范 优酷超清地址 腾讯超清地址 土豆超清地址

  9. Objective-C 高性能的循环

    Cocoa编程的一个通常的任务是要去循环遍历一个对象的集合  (例如,一个 NSArray, NSSet 或者是 NSDictionary). 这个看似简单的问题有广泛数量的解决方案,它们中的许多不乏 ...

  10. AngularJS中实现日志服务

    本篇体验使用AngularJS自定义一个记录日志的服务. 在AngularJS中,服务的一些写法是这样的: var app = angular.module('app',[]); app.provid ...