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格式的更多相关文章

  1. [压缩]C#下使用SevenZipSharp压缩解压文本

    using SevenZip; using System; using System.Collections.Generic; using System.IO; using System.Linq; ...

  2. mac解压7z格式文件

    brew直接安装解压工具 $ brew search 7z p7zip $ brew install p7zip ==> Downloading https://downloads.source ...

  3. Linux压缩解压 tar.gz格式的文件.查看tomcat是否运行

    tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用 ...

  4. 使用C#压缩解压rar和zip格式文件

    为了便于文件在网络中的传输和保存,通常将文件进行压缩操作,常用的压缩格式有rar.zip和7z,本文将介绍在C#中如何对这几种类型的文件进行压缩和解压,并提供一些在C#中解压缩文件的开源库. 在C#. ...

  5. 【转载】.NET压缩/解压文件/夹组件

    转自:http://www.cnblogs.com/asxinyu/archive/2013/03/05/2943696.html 阅读目录 1.前言 2.关于压缩格式和算法的基础 3.几种常见的.N ...

  6. Linux 压缩解压

    压缩解压 ------------------------------------------ linux 下所有的压缩格式,WinRAR 都支持 gzip .gz 格式 压缩文件: gzip 文件名 ...

  7. SAPCAR 压缩解压软件的使用方法

    SAPCAR 是 SAP 公司使用的压缩解压软件,从 SAP 网站下载的补丁包和小型软件基本都是扩展名为 car 或 sar 的,它们都可以用 SAPCAR 来解压.下面是它的使用说明: 用法: 创建 ...

  8. Linux学习笔记(7)Linux常用命令之压缩解压命令

    (1)gzip gzip命令用于压缩文件,英文原意为GNU zip,所在路径/bin/gzip,其语法格式为: gzip [文件] 压缩后的文件格式为.gz. 例:将/etc目录下的services文 ...

  9. linux笔记:压缩解压命令gzip,gunzip,tar,zip,unzip,bzip2,bunzip2

    命令名称:gzip功能:压缩文件命令所在路径:/bin/gzip用法:gzip 文件压缩后文件格式:.gz其他:压缩后不保留原文件:只能压缩文件,不能压缩目录 命令名称:gunzip功能:解压.gz格 ...

随机推荐

  1. javascript的方法

    1. decodeURIComponent() decodeURIComponent() 函数可对 encodeURIComponent() 函数编码的 URI 进行解码. 语法: decodeURI ...

  2. idea常用设置

    Idea删除当前行的快捷键是Ctrl+y,复制当前行的快捷键是Ctrl+d,和eclipse的习惯不一样.虽然可以一键把idea的快捷键映射成eclipse,但是这样做代价太大,如果这样,idea的官 ...

  3. mongodb c api编译

    1. autoconf-latest.tar.gz http://ftp.gnu.org/gnu/autoconf/ tar xzvf autoconf-latest.tar.gz ./configu ...

  4. sicily 1063. Who's the Boss 排序+递推

    #include <cstdio> #include <algorithm> using namespace std; struct Emp{ int id, salary, ...

  5. SVN服务搭建

    yum方式搭建: 1,安装SVN服务端 直接用apt-get或yum安装subversion即可(当然也可以自己去官方下载安装) sudo apt-get install subversion 2,创 ...

  6. jQuery给CheckBox全选与不全选

    $(function(){ $("#checkAll").click(function() {//全选 $('input[name="DATA"]').prop ...

  7. 利用jink的驱动软件j-flash 合并两个hex的方法,bootloader+app

    由于前几天要给工厂app和bootloader的hex的文件,网上很多都是bin的合并方法,bin的方法不再赘述,相信大家都能找到,现在将hex合并的方法写下来: 第一步:先打开第一个hex文件, 第 ...

  8. angular ui-router 正则

    {id:[0-9a-fA-F]{1,8}} 如果在找ui-router 时,会看到以上的正则 我解释一下 id是param,可以把必备的结果收起来 []里面的必配的正则,但只是给一个string {} ...

  9. WebStorm 自定义字体+颜色+语法高亮+导入导出用户设置

    WebStorm :是jetbrains公司旗下一款JavaScript 开发工具.被广大中国JS开发者誉为“Web前端开发神器”.“最强大的HTML5编辑器”.“最智能的JavaScript IDE ...

  10. 使用GDI绘制文本

    /// <summary>        /// 定义一个绘制文本        /// </summary>        public void Texts()       ...