admin管理员组

文章数量:1632159

常用的压缩格式rar、zip和7z

1在C#.NET中压缩解压rar文件

rar格式是有专利文件的商业压缩格式,不开源,对解码算法公开,但压缩算法私有,需付费,如需在商业软件中使用rar格式进行解压缩,需为rar付费,rar在国内很流行是由于盗版的存在,正因为算法是不开源的,所以压缩rar并没有第三方的开源库可供选择,只能另寻出路。针对rar的解压缩通常使用winrar,几乎每台机器都安装了winrar,对于普通用户来说它提供基于用户界面的解压缩方式,另外,它也提供基于命令行的解压缩方式,这为我们在程序中解压缩rar格式提供了一个入口,我们可以在C#程序中调用rar的命令行程序实现解压缩,思路:
1、判断注册表确认用户机器是否安装winrar程序,如果安装取回winrar安装目录。
2、创建一个命令行执行进程。
3、通过winrar的命令行参数实现解压缩。
首先我们通过下面的代码判断用户计算机是否安装了winrar压缩工具。
如果已经安装winrar可通过如下代码返回winrar的安装位置,未安装则返回空字符串,最后并关闭注册表:

public static string ExistsWinRar()
{
    string result = string.Empty;
    string key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe";
    RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(key);
    if (registryKey != null)
    {
        result = registryKey.GetValue("").ToString();
    }
    registryKey.Close();

    return result;
}
/// <summary>
/// 将格式为rar的压缩文件解压到指定的目录
/// </summary>
/// <param name="rarFileName">要解压rar文件的路径</param>
/// <param name="saveDir">解压后要保存到的目录</param>
public static void DeCompressRar(string rarFileName, string saveDir)
{
    string regKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe";
    RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(regKey);
    string winrarPath = registryKey.GetValue("").ToString();
    registryKey.Close();
    string winrarDir = System.IO.Path.GetDirectoryName(winrarPath);
    String commandOptions = string.Format("x {0} {1} -y", rarFileName, saveDir);
    ProcessStartInfo processStartInfo = new ProcessStartInfo();
    processStartInfo.FileName = System.IO.Path.Combine(winrarDir, "rar.exe");
    processStartInfo.Arguments = commandOptions;
    processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    Process process = new Process();
    process.StartInfo = processStartInfo;
    process.Start();
    process.WaitForExit();
    process.Close();
}
/// <summary>
/// 将目录和文件压缩为rar格式并保存到指定的目录
/// </summary>
/// <param name="soruceDir">要压缩的文件夹目录</param>
/// <param name="rarFileName">压缩后的rar保存路径</param>
public static void CompressRar(string soruceDir, string rarFileName)
{
    string regKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe";
    RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(regKey);
    string winrarPath = registryKey.GetValue("").ToString();
    registryKey.Close();
    string winrarDir = System.IO.Path.GetDirectoryName(winrarPath);
    String commandOptions = string.Format("a {0} {1} -r", rarFileName, soruceDir);

    ProcessStartInfo processStartInfo = new ProcessStartInfo();
    processStartInfo.FileName = System.IO.Path.Combine(winrarDir, "rar.exe");
    processStartInfo.Arguments = commandOptions;
    processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    Process process = new Process();
    process.StartInfo = processStartInfo;
    process.Start();
    process.WaitForExit();
    process.Close();
}

2.在C#.NET中压缩解压zip文件

zip是免费开源的压缩格式,windows平台自带zip压缩和解压工具,由于算法开源,所以基于zip的解压缩开源库也很多,SharpZipLib是一个很不错的C#库,它能够解压缩zip、gzip和tar格式的文件,首先下载SharpZipLib解压后,在项目中引用ICSharpCode.SharpZLib.dll程序集即可,下面是一些关于SharpZipLib压缩和解压的示例。

ZipOutputStream zipOutStream = new ZipOutputStream(File.Create("my.zip"));
CreateFileZipEntry(zipOutStream, "file1.txt", "file1.txt");
CreateFileZipEntry(zipOutStream, @"folder1\folder2\folder3\file2.txt", "file2.txt");
zipOutStream.Close();
Directory.CreateDirectory("ZipOutPut");
 ZipInputStream zipInputStream = new ZipInputStream(File.Open("my.zip", FileMode.Open));
 ZipEntry zipEntryFromZippedFile = zipInputStream.GetNextEntry();
 while (zipEntryFromZippedFile != null)
 {
     if (zipEntryFromZippedFile.IsFile)
     {
         FileInfo fInfo = new FileInfo(string.Format("ZipOutPut\\{0}", zipEntryFromZippedFile.Name));
         if (!fInfo.Directory.Exists) fInfo.Directory.Create();

         FileStream file = fInfo.Create();
         byte[] bufferFromZip = new byte[zipInputStream.Length];
         zipInputStream.Read(bufferFromZip, 0, bufferFromZip.Length);
         file.Write(bufferFromZip, 0, bufferFromZip.Length);
         file.Close();
     }
     zipEntryFromZippedFile = zipInputStream.GetNextEntry();
 }
 zipInputStream.Close();

3.使用.NET中自带的类解压缩zip文件

微软在System.IO.Compression命名空间有一些关于文件解压缩的类,如果只是希望压缩解压zip和gzip格式的文件,是个不错的选择,在NET Framework 4.5框架中,原生System.IO.Compression.FileSystem.dll程序集中新增了一个名为ZipFile的类,,让压缩和解压zip文件变得更简单,ZipFile的使用示例如下:

System.IO.Compression.ZipFile.CreateFromDirectory(@"e:\test", @"e:\test\test.zip"); //压缩
System.IO.Compression.ZipFile.ExtractToDirectory(@"e:\test\test.zip", @"e:\test"); //解压

4.支持格式最多的C#解压缩开源库

名为SharpCompress的C#框架被开源,可以在搜索引擎中找到SharpCompress框架的开源代码,它支持:rar 7zip, zip, tar, tzip和bzip2格式的压缩和解压,下面的示例直接从rar格式文件读取并解压文件。

using (Stream stream = File.OpenRead(@"C:\Code\sharpcompress.rar"))
{
    var reader = ReaderFactory.Open(stream);
    while (reader.MoveToNextEntry())
    {
        if (!reader.Entry.IsDirectory)
        {
            Console.WriteLine(reader.Entry.FilePath);
            reader.WriteEntryToDirectory(@"C:\temp");
        }
    }
}

5.总结

关于rar和zip格式相比,rar的压缩率比zip要高,而且支持分卷压缩,但rar是商业软件,需要付费,zip压缩率不如rar那么高,但开源免费,7zip格式开源免费,压缩率较为满意,这些压缩格式各有优势,就微软平台和一些开源平台来说,一般采用的都是zip格式,因为它更容易通过编程的方式实现,比rar更加可靠。

转自:http://blog.csdn/moonpure/article/details/46563017

本文标签: 文件zipRAR