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. C/C++ -- Gui编程 -- Qt库的使用 -- 对话框QDialog

    模态对话框 -----源文件main.cpp(工程QtDialog)----- #include "qtdialog.h" #include <QApplication> ...

  2. 2017年Android百大框架排行榜

    框架:提供一定能力的小段程序 >随意转载,标注作者"金诚"即可 >本文已授权微信公众号:鸿洋(hongyangAndroid)原创首发. >本文已经开源到Gith ...

  3. AAAI2019 | 基于区域分解集成的目标检测 论文解读

    Object Detection based on Region Decomposition and Assembly AAAI2019 | 基于区域分解集成的目标检测 论文解读 作者 | 文永亮 学 ...

  4. apache的rewrite规则来实现URL末尾是否带斜杠

    1.url: http://www.test.com/user/ 跟:http://www.test.com/user 这两个URL对于用户来说应该是一样的,但从编程的角度来说,它们可以不相同 但我们 ...

  5. Java : 传值or传引用?

    那看看这句经典名言:O'Reilly's Java in a Nutshell by David Flanagan (see Resources) puts it best: "Java m ...

  6. Shell脚本编写4-----Shell 流程控制

    没啥好说的,直接从demo里看吧!(1) if 语句shell脚本的if语句格式如下: 判断输入两个参数的大小,执行结果如下 (2)for 循环for循环语法格式如下: 执行结果如下 (3)while ...

  7. [转]数据库中Schema(模式)概念的理解

    在学习数据库时,会遇到一个让人迷糊的Schema的概念.实际上,schema就是数据库对象的集合,这个集合包含了各种对象如:表.视图.存储过程.索引等. 如果把database看作是一个仓库,仓库很多 ...

  8. UIWebView 展示GIF/image

    代码: [web loadData:self.gifDataArr[index] MIMEType:@"image/gif" textEncodingName:@"&qu ...

  9. Storm框架:如何根据业务条件选择不同的bolt进行下发消息

    Strom框架基本概念就不提了,这里主要讲的是Stream自定义ID的消息流.默认spout.bolt都需实现接口方法declareOutputFields,代码如下: @Override publi ...

  10. 规范大于编码-我的javaWeb开发规范

    1.应用名称和数据库名称一致 2.javaBean类名称+s和数据库表名一致 3.返回一条数据时,变量名为javaBean类名称的小写;返回多条数据时,变量名为javaBean类名称的大写 4.jav ...