博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
压缩解压缩
阅读量:4035 次
发布时间:2019-05-24

本文共 2664 字,大约阅读时间需要 8 分钟。

核心库NewLife.Core内置了压缩相关功能扩展,并且独立实现的ZipFile还支持.NET2.0和.NET4.0,该类在.NET4.5以后成为.NET Framework框架标配。

数据流压缩

IOHelper类扩展了字节数组和数据流的压缩扩展方法

Byte[] Compress(this Byte[] data);Byte[] Decompress(this Byte[] data);Stream Compress(this Stream inStream, Stream outStream = null);Stream Decompress(this Stream inStream, Stream outStream = null);Stream CompressGZip(this Stream inStream, Stream outStream = null);Stream DecompressGZip(this Stream inStream, Stream outStream = null);

从.NET2.0开始,就内置了Deflate压缩算法,这是很常见的一种数据压缩算法,HTTP常用它进行压缩,压缩比一般,但是压缩速度很好。上述字节数组和数据流的扩展就是对DeflateStream的简单封装,以兼容.NET2.0之上的所有应用(不同NFX版本的DeflateStream用法有所不同)。

而GZip则是一种数据格式,多了个头部,数据体部分还是Deflate压缩。因此,数据流压缩直接用Deflate算法,而压缩为文件时,则使用GZip,一般用gz后缀。

例如,XCode导出整表数据时,为了减小文件大小,会直接输出到用GZipStream包装的文件流。

文件压缩

PathHelper类扩展了文件和目录的压缩及解压缩。

void Extract(this FileInfo fi, String destDir, Boolean overwrite = false);void Compress(this FileInfo fi, String destFile);void Compress(this DirectoryInfo di, String destFile = null);

Extract把压缩文件解压缩到目标目录,支持覆盖已存在文件。一般用于压缩备份和数据恢复场合。

注意:.zip后缀文件采用ZipFile类处理,其它后缀仅支持在Windows上调用7z处理,下同

Compress把文件压缩到目标文件,一般用于压缩备份日志等文本文件,有很大的压缩比。

Compress另一个重载,把目录压缩到目标文件,一般用于压缩备份整个目录,如配置文件目录等。

ZipFile压缩

Zip压缩文件实现类ZipArchive,而ZipFile扩展了常用方法。

// 打开压缩文档ZipArchive Open(String archiveFileName, ZipArchiveMode mode, Encoding entryNameEncoding = null);// 从目录创建压缩文档void CreateFromDirectory(String sourceDirectoryName, String destinationArchiveFileName, CompressionLevel compressionLevel, Boolean includeBaseDirectory);

压缩文件:

using var zip = ZipFile.Open(destFile, ZipArchiveMode.Create);zip.CreateEntryFromFile(fi.FullName, fi.Name, CompressionLevel.Optimal);

解压缩文件:

using var zip = ZipFile.Open(fi.FullName, ZipArchiveMode.Read, null);var di = Directory.CreateDirectory(destDir);var fullName = di.FullName;foreach (var item in zip.Entries){    var fullPath = Path.GetFullPath(Path.Combine(fullName, item.FullName));    if (!fullPath.StartsWith(fullName, StringComparison.OrdinalIgnoreCase))        throw new IOException("IO_ExtractingResultsInOutside");    if (Path.GetFileName(fullPath).Length == 0)    {        if (item.Length != 0L) throw new IOException("IO_DirectoryNameWithData");        Directory.CreateDirectory(fullPath);    }    else    {        Directory.CreateDirectory(Path.GetDirectoryName(fullPath));        try        {            item.ExtractToFile(fullPath, overwrite);        }        catch { }    }}

Zip文件本质上就是一个档案数据库,头部有目录,后面就是各个被压缩文件的数据。通过扩展方法或者构造函数拿到ZipArchive后,直接CreateEntry就可以加入要压缩的文件,或者遍历Entries集合就得到内部每个被压缩文件。

7z压缩

SevenZip类用于对7z.exe进行包装,在Windows上处理各种类型压缩文件,它拥有极高的压缩比,在某些维护性系统中具有无可比拟的优势。

SevenZip首先在附近目录查找7z.exe,然后在注册表查找安装路径,实在找不到就从X组件资源站下载 http://x.newlifex.com/7z_v16.04.zip 。

转载地址:http://ftudi.baihongyu.com/

你可能感兴趣的文章
iphone开发基础之objective-c学习
查看>>
iphone开发之SDK研究(待续)
查看>>
计算机网络复习要点
查看>>
Variable property attributes or Modifiers in iOS
查看>>
NSNotificationCenter 用法总结
查看>>
C primer plus 基础总结(一)
查看>>
剑指offer算法题分析与整理(一)
查看>>
剑指offer算法题分析与整理(三)
查看>>
Ubuntu 13.10使用fcitx输入法
查看>>
pidgin-lwqq 安装
查看>>
mint/ubuntu安装搜狗输入法
查看>>
C++动态申请数组和参数传递问题
查看>>
opencv学习——在MFC中读取和显示图像
查看>>
retext出现Could not parse file contents, check if you have the necessary module installed解决方案
查看>>
pyQt不同窗体间的值传递(一)——对话框关闭时返回值给主窗口
查看>>
linux mint下使用外部SMTP(如网易yeah.net)发邮件
查看>>
北京联通华为光猫HG8346R破解改桥接
查看>>
python使用win32*模块模拟人工操作——城通网盘下载器(一)
查看>>
python append 与浅拷贝
查看>>
Matlab与CUDA C的混合编程配置出现的问题及解决方案
查看>>