using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO; namespace Zhong.Core
{
/// <summary>
/// 压缩解压操作类,使用的是SharpZipLib
/// </summary>
public class DCompress
{
private static object operateLock = new object();
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="srcFile">要压缩的文件路径</param>
/// <param name="destFile">生成的压缩文件路径</param>
public static void CompressFile(string srcFile, string destFile)
{
lock (operateLock)
{
if (string.IsNullOrEmpty(srcFile) || string.IsNullOrEmpty(destFile))
{
throw new ArgumentException("参数错误");
}
FileStream fileStreamIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read);
FileStream fileStreamOut = new FileStream(destFile, FileMode.Create, FileAccess.Write);
ZipOutputStream zipOutStream = new ZipOutputStream(fileStreamOut);
//zipOutStream.SetLevel(6); //设置压缩等级,默认为6
byte[] buffer = new byte[];
ZipEntry entry = new ZipEntry(Path.GetFileName(srcFile));
zipOutStream.PutNextEntry(entry);
int size;
do
{
size = fileStreamIn.Read(buffer, , buffer.Length);
zipOutStream.Write(buffer, , size);
} while (size > );
zipOutStream.Dispose();
fileStreamOut.Dispose();
fileStreamIn.Dispose();
} }
/// <summary>
/// 压缩多个文件
/// </summary>
/// <param name="srcFiles">多个文件路径</param>
/// <param name="destFile">压缩文件的路径</param>
public static void ZipFiles(string[] srcFiles, string destFile)
{
if (srcFiles == null || string.IsNullOrEmpty(destFile))
{
throw new ArgumentException("参数错误");
}
using (ZipFile zip = ZipFile.Create(destFile))
{
zip.BeginUpdate();
//add file
foreach (string filePath in srcFiles)
{
zip.Add(filePath);
}
zip.CommitUpdate();
}
//(new FastZip()).CreateZip(@"E:\1.zip", @"E:\1\", true, "");
}
/// <summary>
/// 压缩目录
/// </summary>
/// <param name="dir">目录路径</param>
/// <param name="destFile">压缩文件路径</param>
public static void ZipDir(string dir, string destFile)
{
if (string.IsNullOrEmpty(dir) || string.IsNullOrEmpty(destFile))
{
throw new ArgumentException("参数错误");
}
string[] files = Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories);
ZipFiles(files, destFile);
}
/// <summary>
/// 列表压缩文件里的所有文件
/// </summary>
/// <param name="zipPath">压缩文件路径</param>
/// <returns></returns>
public static List<string> GetFileList(string zipPath)
{
List<string> files = new List<string>();
if (string.IsNullOrEmpty(zipPath))
{
throw new ArgumentException("参数错误");
}
using (ZipFile zip = new ZipFile(zipPath))
{
string list = string.Empty;
foreach (ZipEntry entry in zip)
{
if (entry.IsFile)
{
files.Add(entry.Name);
}
}
}
return files;
}
/// <summary>
/// 删除zip文件中的某个文件
/// </summary>
/// <param name="zipPath">压缩文件路径</param>
/// <param name="files">要删除的某个文件</param>
public static void DeleteFileFromZip(string zipPath, string[] files)
{
if (string.IsNullOrEmpty(zipPath) || files == null)
{
throw new ArgumentException("参数错误");
}
using (ZipFile zip = new ZipFile(zipPath))
{
zip.BeginUpdate();
foreach (string f in files)
{
zip.Delete(f);
}
zip.CommitUpdate();
}
}
/// <summary>
/// 解压文件
/// </summary>
/// <param name="zipPath">要解压的文件</param>
/// <param name="outputDir">解压后放置的目录</param>
public static void UnZipFile(string zipPath, string outputDir)
{
(new FastZip()).ExtractZip(zipPath, outputDir, "");
} /// <summary>
/// 解压文件
/// </summary>
/// <param name="srcFile">压缩文件路径</param>
/// <param name="destDir">解压后文件夹的路径</param>
public static void Decompress(string srcFile, string destDir)
{
lock (operateLock)
{
FileStream fileStreamIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read);
ZipInputStream zipInStream = new ZipInputStream(fileStreamIn);
if (!Directory.Exists(destDir))
{
Directory.CreateDirectory(destDir);
}
ZipEntry entry;
while ((entry = zipInStream.GetNextEntry()) != null)
{
FileStream fileStreamOut = new FileStream(destDir + @"\" + entry.Name, FileMode.Create, FileAccess.Write);
int size;
byte[] buffer = new byte[];
do
{
size = zipInStream.Read(buffer, , buffer.Length);
fileStreamOut.Write(buffer, , size);
} while (size > );
fileStreamOut.Dispose();
}
zipInStream.Dispose();
fileStreamIn.Dispose();
} }
}
}

使用SharpZipLib压缩与解压文件的更多相关文章

  1. ICSharpCode.SharpZipLib 压缩、解压文件 附源码

    http://www.icsharpcode.net/opensource/sharpziplib/ 有SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, GZip, ...

  2. C# 压缩、解压文件夹或文件(带密码)

    今天梳理一下项目中用到的压缩.解压文件夹或文件的方法,发现因为需求不同,已经用了好几个不同组件.今天就好好整理记录下,别下次遇到需求又重头开始了. DotNetZip DotNetZip是一个开源的免 ...

  3. AIX系统上压缩与解压文件

    压缩. 命令格式: #tar -cvf (或xvf)+文件名+设备 C:是本地到其他设备 x:是其他设备到本地 r:是追加,比如打包时,将其他文件追加进来使用该参数. t:显示tar包里的内容,但还原 ...

  4. Linux学习笔记之AIX系统上压缩与解压文件

    0x00 概述 AIX机器真难用,一时半会还真适应不了.   0x01 压缩tar 命令格式: # tar -cvf (或xvf)+文件名+设备 C:是本地到其他设备 x:是其他设备到本地 r:是追加 ...

  5. C#工具类:使用SharpZipLib进行压缩、解压文件

    SharpZipLib是一个开源的C#压缩解压库,应用非常广泛.就像用ADO.NET操作数据库要打开连接.执行命令.关闭连接等多个步骤一样,用SharpZipLib进行压缩和解压也需要多个步骤.Sha ...

  6. Linux命令(16)压缩,解压文件

    tar: 简介:tar命令只是把目录打包成一个归档(文件),并不负责压缩.在tar命令中可以带参数调用gzip或bzip2压缩.因为gzip和bzip2只能压缩单个文件. 在linux下是不需要后缀名 ...

  7. 本地上传文件至服务器的技巧(linux文件压缩及解压文件)

    linux(ubuntu)文件解压及压缩文件 ubuntu支持文件的解压及压缩功能, 如果ubuntu上面没有安装过unzip工具的话,可以通过下面命令安装: sudo apt-get install ...

  8. (转)使用 linux tar 命令压缩与解压文件

    原文链接 http://www.cnblogs.com/qq78292959/archive/2011/07/06/2099427.html tar -c: 建立压缩档案-x:解压-t:查看内容-r: ...

  9. 【转】iOS开发之压缩与解压文件

    ziparchive是基于开源代码”MiniZip”的zip压缩与解压的Objective-C 的Class,使用起来非常的简单方法:从http://code.google.com/p/ziparch ...

随机推荐

  1. Mac在终端用命令装载dmg文件

    今天碰到个问题,下载了一个dmg文件,然后双击/右键安装,一点反应都没有.一开始以为是电脑的缘故,重启,依旧没有反应,然后想到用终端装载试试. 打开终端,输入命令: hdiutil attach we ...

  2. Eth 部署智能合约

    首先要开发以太坊的智能合约,需要EVM(以太坊虚拟机),也就是需要运行的环境,我们可以通过 geth 来设置开发环境: geth --datadir testNet --dev console 2&g ...

  3. Pascal的3种注释

    1.单行注释 {大括号里的内容都被注释掉} 2.单行注释2 //这种注释几乎是绝大部分语言中通用的了 3.多行注释 (*pascal的多行注释有点奇怪,使用的是:括号+星号的方式*)       其他 ...

  4. Oracle 12c 操作 CDB PDB

    CREATE TRIGGER open_all_pdbs AFTER STARTUP ON DATABASE BEGIN EXECUTE IMMEDIATE 'alter pluggable data ...

  5. [转]COPY OR MOVE FILES AND FOLDERS USING OLE AUTOMATION

    本文转自:http://sqlindia.com/copy-move-files-folders-using-ole-automation-sql-server/ I love playing aro ...

  6. FacebookFriendAdderPro

    Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\Fe ...

  7. [日常] crontab的秒执行和串行化和多进程实现

    1. crontab的最低运行频率是,按照每分钟执行一次,通过在脚本中简单实现按秒级别运行 比如这条cron规则 , 每分钟执行一次脚本 * * * * * php /var/www/html/tes ...

  8. Java多态-如何理解父类引用指向子类对象

    java多态,如何理解父类引用指向子类对象 要理解多态性,首先要知道什么是“向上转型”. 我定义了一个子类Cat,它继承了Animal类,那么后者就是前者是父类.我可以通过   Cat c = new ...

  9. mysql数据库定时任务

    应用系统运行中,经常需要定时执行一些任务,例如:定时更新汇总数据,定时更新状态数据等,目前 Treesoft数据库管理系统 增加[定时任务]功能,直接通过页面简单配置,即可按调度规则定时执行SQL任务 ...

  10. WCF使用net.tcp传输文件

    摘要:今天看了一些官方的资料和配置,简单写了一个WCF服务来传递一个文件,借此看看WCF传输大文件的能力,这里采用的是NetTcp绑定,之所以没有采用 basicHttpBinding是因为考虑这种方 ...