使用SevenZipSharp压缩/解压7z格式
7z格式采用的LZMA算法,号称具有现今最高压缩率。笔者在nuget上搜索7z,在搜索结果中最终选择了SevenZipSharp来进行压缩/解压。不得不说,SevenZipSharp的API设计得非常方便。
压缩调用:
using (FileStream ostream = new FileStream(outputpath, FileMode.Create, FileAccess.Write))
{
using (FileStream istream = new FileStream(inputpath, FileMode.Open, FileAccess.Read))
{
SevenZipCompressor compressor = new SevenZipCompressor();
// 这里可以输入多个文件名/流对
Dictionary<string, Stream> dict = new Dictionary<string, Stream> { { inputpath, istream } };
compressor.CompressStreamDictionary(dict, ostream);
}
}
解压调用:
using (FileStream istream = new FileStream(inputpath, FileMode.Open, FileAccess.Read))
{
SevenZip.SevenZipExtractor extractor = new SevenZip.SevenZipExtractor(istream);
extractor.ExtractArchive(outputpath); // 全部解压到指定目录
using (FileStream ostream = new FileStream(outputpath, FileMode.Create, FileAccess.Write))
{
extractor.ExtractFile(, ostream); // 流式解压指定文件
}
}
顺便附上zlib的压缩/解压(使用zlib.net库):
// 压缩
static void Compress(string inputpath, string outputpath)
{
using (FileStream ostream = new FileStream(outputpath, FileMode.Create, FileAccess.Write))
{
using (FileStream istream = new FileStream(inputpath, FileMode.Open, FileAccess.Read))
{
using (ZOutputStream zstream = new ZOutputStream(ostream, zlibConst.Z_BEST_COMPRESSION))
{
CopyStream(istream, zstream);
}
}
}
} // 解压
static void Decompress(string inputpath, string outputpath)
{
using (FileStream ostream = new FileStream(outputpath, FileMode.Create, FileAccess.Write))
{
using (FileStream istream = new FileStream(inputpath, FileMode.Open, FileAccess.Read))
{
using (ZOutputStream zstream = new ZOutputStream(ostream))
{
CopyStream(istream, zstream);
}
}
}
} static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[];
int len;
while ((len = input.Read(buffer, , )) > )
{
output.Write(buffer, , len);
}
output.Flush();
}
使用SevenZipSharp压缩/解压7z格式的更多相关文章
- [压缩]C#下使用SevenZipSharp压缩解压文本
using SevenZip; using System; using System.Collections.Generic; using System.IO; using System.Linq; ...
- mac解压7z格式文件
brew直接安装解压工具 $ brew search 7z p7zip $ brew install p7zip ==> Downloading https://downloads.source ...
- Linux压缩解压 tar.gz格式的文件.查看tomcat是否运行
tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用 ...
- 使用C#压缩解压rar和zip格式文件
为了便于文件在网络中的传输和保存,通常将文件进行压缩操作,常用的压缩格式有rar.zip和7z,本文将介绍在C#中如何对这几种类型的文件进行压缩和解压,并提供一些在C#中解压缩文件的开源库. 在C#. ...
- 【转载】.NET压缩/解压文件/夹组件
转自:http://www.cnblogs.com/asxinyu/archive/2013/03/05/2943696.html 阅读目录 1.前言 2.关于压缩格式和算法的基础 3.几种常见的.N ...
- Linux 压缩解压
压缩解压 ------------------------------------------ linux 下所有的压缩格式,WinRAR 都支持 gzip .gz 格式 压缩文件: gzip 文件名 ...
- SAPCAR 压缩解压软件的使用方法
SAPCAR 是 SAP 公司使用的压缩解压软件,从 SAP 网站下载的补丁包和小型软件基本都是扩展名为 car 或 sar 的,它们都可以用 SAPCAR 来解压.下面是它的使用说明: 用法: 创建 ...
- Linux学习笔记(7)Linux常用命令之压缩解压命令
(1)gzip gzip命令用于压缩文件,英文原意为GNU zip,所在路径/bin/gzip,其语法格式为: gzip [文件] 压缩后的文件格式为.gz. 例:将/etc目录下的services文 ...
- linux笔记:压缩解压命令gzip,gunzip,tar,zip,unzip,bzip2,bunzip2
命令名称:gzip功能:压缩文件命令所在路径:/bin/gzip用法:gzip 文件压缩后文件格式:.gz其他:压缩后不保留原文件:只能压缩文件,不能压缩目录 命令名称:gunzip功能:解压.gz格 ...
随机推荐
- jquery实现横向导航
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- NHibernate的常见问题及解决方案
问题1 : 异常:in expected: <end-of-text> (possibly an invalid or unmapped class name was used in th ...
- 实际情况来看,还是yield很爽
0 引言 最近公司有一个 php 的项目,要 port 到 node.js 来.我之前没有接触过这个项目,整个项目使用的是 yaf 框架.整个项目流程是调用服务端的业务数据,然后拼装数据,返回给前端: ...
- 关于hosts文件的原理与制作
由于需要整理的关于hosts的文件 关于hosts文件的原理与制作1.什么是hosts文件hosts文件是一个没有扩展名的系统文件,hosts文件用于存储计算机网络中节点信息的文件,它是可以将主机名映 ...
- appium desktop 版本发布
Appium Desktop is an open source app for Mac, Windows, and Linux which gives you the power of the Ap ...
- (Mac OS平台)升级.NetCore1.0正式版小记
昨天终于发布了.NetCore1.0正式版.昨晚回去就顺手把手里的一个.NetCore项目升级了一下.还是遇到了一些问题,这里记录下吧. 1.Restore问题 这个问题一直都有,一直放那没去解决.主 ...
- 经典网络还是VPC,开发者作何选择?
近两天,关于公有云经典网络(基础网络)与私有网络(VPC)的讨论引发技术圈极大关注,事件起因于有开发者将数据库限制在内网访问,但由于安全组设置的原因,阿里云邻居用户被黑后,牵连到了自己的业务.为此,开 ...
- Android--多线程之Handler 前言
前言 Android的消息传递机制是另外一种形式的“事件处理”,这种机制主要是为了解决Android应用中多线程的问题,在Android中不 允许Activity新启动的线程访问该Activity里的 ...
- Redis 11种Web应用场景举例
在"怎样让redis在你的系统中发挥作用"一文中,salvatore 'antirez' sanfilippo告诉我们如何利用redis独有的数据结构处理能力来解决一些常见问题.一 ...
- WebApi接口请求失败,找不到资源。
WebApi开发接口,实现同步数据库的数据给安卓. public class UserInfoController : ApiControllerBase { private UserBLL user ...