使用C#压缩解压rar和zip格式文件
"
为了便于文件在网络中的传输和保存,通常将文件进行压缩操作,常用的压缩格式有rar、zip和7z,本文将介绍在C#中如何对这几种类型的文件进行压缩和解压,并提供一些在C#中解压缩文件的开源库。
在C#.NET中压缩解压rar文件
rar格式是一种具有专利文件的压缩格式,是一种商业压缩格式,不开源,对解码算法是公开的,但压缩算法是私有的,需要付费,如果需要在您的商业软件中使用rar格式进行解压缩,那么你需要为rar付费,rar在国内很流行是由于盗版的存在,正因为算法是不开源的,所以我们压缩rar并没有第三方的开源库可供选择,只能另寻出路。
针对rar的解压缩,我们通常使用winrar,几乎每台机器都安装了winrar,对于普通用户来说它提供基于用户界面的解压缩方式,另外,它也提供基于命令行的解压缩方式,这为我们在程序中解压缩rar格式提供了一个入口,我们可以在C#程序中调用rar的命令行程序实现解压缩,思路是这样的:
1、判断注册表确认用户机器是否安装winrar程序,如果安装取回winrar安装目录。
2、创建一个命令行执行进程。
3、通过winrar的命令行参数实现解压缩。
首先我们通过下面的代码判断用户计算机是否安装了winrar压缩工具:
如果已经安装winrar可通过如下代码返回winrar的安装位置,未安装则返回空字符串,最后并关闭注册表:
<span style=color: blue;>public static string ExistsWinRar() { <span style=color: blue;>string result = <span style=color: blue;>string.Empty;
<span style=""color: blue;"">string </span>key = <span style=""color: #a31515;"">@""SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe""</span>;
<span style=""color: #2b91af;"">RegistryKey </span>registryKey = <span style=""color: #2b91af;"">Registry</span>.LocalMachine.OpenSubKey(key);
<span style=""color: blue;"">if </span>(registryKey != <span style=""color: blue;"">null</span>)
{
result = registryKey.GetValue(<span style=""color: #a31515;"">""""</span>).ToString();
}
registryKey.Close();
<span style=""color: blue;"">return </span>result;
}
<span style=color: gray;>///
<span style=""color: #2b91af;"">ProcessStartInfo </span>processStartInfo = <span style=""color: blue;"">new </span><span style=""color: #2b91af;"">ProcessStartInfo</span>();
processStartInfo.FileName = System.IO.<span style=""color: #2b91af;"">Path</span>.Combine(winrarDir, <span style=""color: #a31515;"">""rar.exe""</span>);
processStartInfo.Arguments = commandOptions;
processStartInfo.WindowStyle = <span style=""color: #2b91af;"">ProcessWindowStyle</span>.Hidden;
<span style=""color: #2b91af;"">Process </span>process = <span style=""color: blue;"">new </span><span style=""color: #2b91af;"">Process</span>();
process.StartInfo = processStartInfo;
process.Start();
process.WaitForExit();
process.Close();
}
<span style=color: gray;>///
<span style=""color: #2b91af;"">ProcessStartInfo </span>processStartInfo = <span style=""color: blue;"">new </span><span style=""color: #2b91af;"">ProcessStartInfo</span>();
processStartInfo.FileName = System.IO.<span style=""color: #2b91af;"">Path</span>.Combine(winrarDir, <span style=""color: #a31515;"">""rar.exe""</span>);
processStartInfo.Arguments = commandOptions;
processStartInfo.WindowStyle = <span style=""color: #2b91af;"">ProcessWindowStyle</span>.Hidden;
<span style=""color: #2b91af;"">Process </span>process = <span style=""color: blue;"">new </span><span style=""color: #2b91af;"">Process</span>();
process.StartInfo = processStartInfo;
process.Start();
process.WaitForExit();
process.Close();
}
在C#.NET中压缩解压zip文件
zip是一种免费开源的压缩格式,windows平台自带zip压缩和解压工具,由于算法是开源的,所以基于zip的解压缩开源库也很多,SharpZipLib是一个很不错的C#库,它能够解压缩zip、gzip和tar格式的文件,首先下载SharpZipLib解压后,在您的项目中引用ICSharpCode.SharpZLib.dll程序集即可,下面是一些关于SharpZipLib压缩和解压的示例。
<span style=color: #2b91af;>ZipOutputStream <span style=color: black;>zipOutStream = <span style=color: blue;>new <span style=color: #2b91af;>ZipOutputStream<span style=color: black;>(<span style=color: #2b91af;>File<span style=color: black;>.Create(<span style=color: #a31515;>my.zip<span style=color: black;>)); CreateFileZipEntry(zipOutStream, <span style=color: #a31515;>file1.txt<span style=color: black;>, <span style=color: #a31515;>file1.txt<span style=color: black;>); CreateFileZipEntry(zipOutStream, <span style=color: #a31515;>@folder1\folder2\folder3\file2.txt<span style=color: black;>, <span style=color: #a31515;>file2.txt<span style=color: black;>); zipOutStream.Close();
<span style=color: #2b91af;>Directory<span style=color: black;>.CreateDirectory(<span style=color: #a31515;>ZipOutPut<span style=color: black;>); <span style=color: #2b91af;>ZipInputStream <span style=color: black;>zipInputStream = <span style=color: blue;>new <span style=color: #2b91af;>ZipInputStream<span style=color: black;>(<span style=color: #2b91af;>File<span style=color: black;>.Open(<span style=color: #a31515;>my.zip<span style=color: black;>, <span style=color: #2b91af;>FileMode<span style=color: black;>.Open)); <span style=color: #2b91af;>ZipEntry <span style=color: black;>zipEntryFromZippedFile = zipInputStream.GetNextEntry(); <span style=color: blue;>while <span style=color: black;>(zipEntryFromZippedFile != <span style=color: blue;>null<span style=color: black;>) { <span style=color: blue;>if <span style=color: black;>(zipEntryFromZippedFile.IsFile) { <span style=color: #2b91af;>FileInfo <span style=color: black;>fInfo = <span style=color: blue;>new <span style=color: #2b91af;>FileInfo<span style=color: black;>(<span style=color: blue;>string<span style=color: black;>.Format(<span style=color: #a31515;>ZipOutPut\{0}<span style=color: black;>, zipEntryFromZippedFile.Name)); <span style=color: blue;>if <span style=color: black;>(!fInfo.Directory.Exists) fInfo.Directory.Create();
</span><span style=""color: #2b91af;"">FileStream </span><span style=""color: black;"">file = fInfo.Create();
</span><span style=""color: blue;"">byte</span><span style=""color: black;"">[] bufferFromZip = </span><span style=""color: blue;"">new byte</span><span style=""color: black;"">[zipInputStream.Length];
zipInputStream.Read(bufferFromZip, 0, bufferFromZip.Length);
file.Write(bufferFromZip, 0, bufferFromZip.Length);
file.Close();
}
zipEntryFromZippedFile = zipInputStream.GetNextEntry();
} zipInputStream.Close();
使用.NET中自带的类解压缩zip文件
微软在System.IO.Compression命名空间有一些关于文件解压缩的类,如果只是希望压缩解压zip和gzip格式的文件,是个不错的选择,在NET Framework 4.5框架中,原生System.IO.Compression.FileSystem.dll程序集中新增了一个名为ZipFile的类,,让压缩和解压zip文件变得更简单,ZipFile的使用示例如下:
<span style=color: black;>System.IO.Compression.<span style=color: #2b91af;>ZipFile<span style=color: black;>.CreateFromDirectory(<span style=color: #a31515;>@e:\test<span style=color: black;>, <span style=color: #a31515;>@e:\test\test.zip<span style=color: black;>); <span style=color: green;>//压缩
<span style=color: black;>System.IO.Compression.<span style=color: #2b91af;>ZipFile<span style=color: black;>.ExtractToDirectory(<span style=color: #a31515;>@e:\test\test.zip<span style=color: black;>, <span style=color: #a31515;>@e:\test<span style=color: black;>); <span style=color: green;>//解压
支持格式最多的C#解压缩开源库
当您还苦苦在为上面的各种压缩格式发愁的时候,一个名为SharpCompress的C#框架被开源,您可以在搜索引擎中找到SharpCompress框架的开源代码,它支持:rar 7zip, zip, tar, tzip和bzip2格式的压缩和解压,下面的示例直接从rar格式文件读取并解压文件。
<span style=color: blue;>using <span style=color: black;>(<span style=color: #2b91af;>Stream <span style=color: black;>stream = <span style=color: #2b91af;>File<span style=color: black;>.OpenRead(<span style=color: #a31515;>@C:\Code\sharpcompress.rar<span style=color: black;>)) { <span style=color: blue;>var <span style=color: black;>reader = ReaderFactory.Open(stream); <span style=color: blue;>while <span style=color: black;>(reader.MoveToNextEntry()) { <span style=color: blue;>if <span style=color: black;>(!reader.Entry.IsDirectory) { <span style=color: #2b91af;>Console<span style=color: black;>.WriteLine(reader.Entry.FilePath); reader.WriteEntryToDirectory(<span style=color: #a31515;>@C:\temp<span style=color: black;>); } } }
零度最后的总结
关于rar和zip格式相比,rar的压缩率比zip要高,而且支持分卷压缩,但rar是商业软件,需要付费,zip压缩率不如rar那么高,但开源免费,7zip格式开源免费,压缩率较为满意,这些压缩格式各有优势,就微软平台和一些开源平台来说,一般采用的都是zip格式,因为它更容易通过编程的方式实现,比rar更加可靠,以上就是零度为您推荐的C#解压缩框架,感谢阅读,希望对您有所帮助。"