使用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 ...
随机推荐
- SPSS学习系列之SPSS Modeler Server是什么?
不多说,直接上干货! SPSS Modeler 使用客户端/服务器体系结构将资源集约型操作的请求分发给功能强大的服务器软件,因而使大数据集的传输速度大大加快.除了此处所列的产品和更新,也可能还有其他可 ...
- mysql Inoodb 内核
MySQL从5.5版本开始将InnoDB作为默认存储引擎,该存储引擎是第一个完整支持事务ACID特性的存储引擎,且支持数据行锁,多版本并发控制(MVCC),外键,以及一致性非锁定读. 作为默认存储引擎 ...
- tensorflow语法笔记
1.如何理解 tf.reduce_max或者 tf.reduce_mean中对Tensor和高维矩阵的坐标轴axis的选择的操作 tf.reduce_mean( input_tensor, axis= ...
- WPF Style和Template
WPF中的Style类似于Web应用程序中的CSS,它是控件的一个属性,属于资源的一种. ControlTemplate和DataTemplate区别: ControlTemplate用于改变控件原来 ...
- redis实战笔记(4)-第4章 数据安全与性能保障
本章主要内容 4.1 将数据持久化至硬盘 4.2 将数据复制至其他机器 4.3 处理系统故障 4.4 Redis事务 4.5 非事务型流水线( non-transactional pipeline) ...
- ABP实战--项目结构
学习完毕With ASP.NET Core & Entity Framework Core Part-1及Part-2后,只实现了基本的功能,使用该工程继续学习ABP的更多功能. 更改项目结构 ...
- vue cli+axios踩坑记录+拦截器使用,代理跨域proxy
1.首先axios不支持vue.use()方式声明使用,看了所有近乎相同的axios文档都没有提到这一点 建议方式 在main.js中如下声明使用 import axios from 'axios'; ...
- ios10系统以下原生传来的base64图片无法转化为二进制
最近在做和原生ios交互上传图片的时候,遇到原生传来的以base64图片位无法转化为二进制.因为前端上传图片的方式是以二进制的方式上传,在ios10 和安卓上,上传图片是可以的:在ios10以下,可以 ...
- NOI2018 退役记
退役预订... upd 果然就这么不光荣的退役了... 我居然考出了一场只有两题得分的比赛,我好菜啊... 不过高三充(tui)实(fei)的生活应该很有意思... 大家一起加油吧!!!
- Node.js之Express一
前面也了解了HTTP模块,但它并不支持session.cookie等.Express是对HTTP模块的封装,同时也支持session这些,使用起来也更好用.Express更有点像IIS服务器.它也是属 ...