使用SharpZipLib压缩与解压文件
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压缩与解压文件的更多相关文章
- ICSharpCode.SharpZipLib 压缩、解压文件 附源码
http://www.icsharpcode.net/opensource/sharpziplib/ 有SharpZiplib的最新版本,本文使用的版本为0.86.0.518,支持Zip, GZip, ...
- C# 压缩、解压文件夹或文件(带密码)
今天梳理一下项目中用到的压缩.解压文件夹或文件的方法,发现因为需求不同,已经用了好几个不同组件.今天就好好整理记录下,别下次遇到需求又重头开始了. DotNetZip DotNetZip是一个开源的免 ...
- AIX系统上压缩与解压文件
压缩. 命令格式: #tar -cvf (或xvf)+文件名+设备 C:是本地到其他设备 x:是其他设备到本地 r:是追加,比如打包时,将其他文件追加进来使用该参数. t:显示tar包里的内容,但还原 ...
- Linux学习笔记之AIX系统上压缩与解压文件
0x00 概述 AIX机器真难用,一时半会还真适应不了. 0x01 压缩tar 命令格式: # tar -cvf (或xvf)+文件名+设备 C:是本地到其他设备 x:是其他设备到本地 r:是追加 ...
- C#工具类:使用SharpZipLib进行压缩、解压文件
SharpZipLib是一个开源的C#压缩解压库,应用非常广泛.就像用ADO.NET操作数据库要打开连接.执行命令.关闭连接等多个步骤一样,用SharpZipLib进行压缩和解压也需要多个步骤.Sha ...
- Linux命令(16)压缩,解压文件
tar: 简介:tar命令只是把目录打包成一个归档(文件),并不负责压缩.在tar命令中可以带参数调用gzip或bzip2压缩.因为gzip和bzip2只能压缩单个文件. 在linux下是不需要后缀名 ...
- 本地上传文件至服务器的技巧(linux文件压缩及解压文件)
linux(ubuntu)文件解压及压缩文件 ubuntu支持文件的解压及压缩功能, 如果ubuntu上面没有安装过unzip工具的话,可以通过下面命令安装: sudo apt-get install ...
- (转)使用 linux tar 命令压缩与解压文件
原文链接 http://www.cnblogs.com/qq78292959/archive/2011/07/06/2099427.html tar -c: 建立压缩档案-x:解压-t:查看内容-r: ...
- 【转】iOS开发之压缩与解压文件
ziparchive是基于开源代码”MiniZip”的zip压缩与解压的Objective-C 的Class,使用起来非常的简单方法:从http://code.google.com/p/ziparch ...
随机推荐
- Cloudera Impala源码分析: SimpleScheduler调度策略详解包括作用、接口及实现等
问题导读:1.Scheduler任务中Distributed Plan.Scan Range是什么?2.Scheduler基本接口有哪些?3.QuerySchedule这个类如何理解?4.Simple ...
- Linux-(ps,grep)
grep命令 1.命令格式: grep [option] pattern file 2.命令功能: 用于过滤/搜索的特定字符.可使用正则表达式能多种命令配合使用,使用上十分灵活. Linux系统中gr ...
- OOAD之创建型模式之工厂模式
首先我们为什么要学习设计模式呢? 1)模式从经验中总结出来的,经过证实的方案,模式只有在实际系统中经过多次验证之后才能成为模式. 2) 提高系统的可维护性, 通过使用设计模式,在系统面临升级和维护时, ...
- [codeup] 1943 进制转换
题目描述 将一个长度最多为30位数字的十进制非负整数转换为二进制数输出. 输入 多组数据,每行为一个长度不超过30位的十进制非负整数.(注意是10进制数字的个数可能有30个,而非30bits的整数) ...
- php的ajax简单实例
很早就听闻ajax的名声,但是却一直不知道怎么用,今天自己捣鼓了一下,竟然会用了,哈哈哈哈. 为了防止我自己忘记,现在把这个简单的实例记录下.这个实例是网上搜的,文末附上链接. 首先你得有自己的服务器 ...
- spark集群搭建(java)未完待续
环境 操作系统:windows10 虚拟机工具:VMware14.1 NUX版本:Centos7.2(64) JDK:1.8(64) 一.安装linux,master(桥接模式上网),slave(na ...
- java JDBC 数据库链接
1.准备环境搭建: myeclipse,sql2005,jdbc. 2.都下载完之后开始进行安装 ,前两个是属于数据库软件,正常安装即可(注意数据库登陆不要使用windows验证) <1> ...
- SpringMVC 工作原理详解
本文Github开源项目https://github.com/Snailclimb/JavaGuide,只供自己学习总结无商业用途,如有侵权,联系删除 先来看一下什么是 MVC 模式 MVC 是一种设 ...
- Hadoop shell 一查就会
Hadoop shell 命令有三种格式 hdfs + dfs (必须是dfs) Hadoop + dfs Hadoop + df 命令 说明 hadoop 版本查看 hadoop version h ...
- zoj Calculate the Function
Calculate the Function Time Limit: 2 Seconds Memory Limit: 65536 KB You are given a list of num ...