使用C#压缩/解压缩7-zip文件

7-Zip 简介

 

7-Zip 是一款号称有着现今最高压缩比的压缩软件,它不仅支持独有的 7z 文件格式,而且还支持各种其它压缩文件格式,其中包括 ZIP, RAR, CAB, GZIP, BZIP2和 TAR 等等。此软件压缩的压缩比要比普通 ZIP 文件高 30-50% ,因此,它可以把 Zip 格式的文件再压缩 2-10% 。
7-Zip 主要特征
更新了算法来加大 7z 格式 的压缩比
支持格式:
压缩及解压缩:7z、ZIP、GZIP、BZIP2 和 TAR
仅解压缩:RAR、CAB、ISO、ARJ、LZH、CHM、WIM、Z、CPIO、RPM、DEB 和 NSIS
对于 ZIP 及 GZIP 格式,7-Zip 能提供比使用 PKZip 及 WinZip 高 2-10% 的压缩比
7z 格式支持创建自释放(SFX)压缩档案
集成 Windows 外壳扩展
强大的的文件管理
强大的命令行版本
支持 FAR Manager 插件
支持 69 种语言

 

C#中压缩/解压缩7-zip文件的方法

 

 1.控制台方式调用7z.exe文件

 

public static void Unzip(DirectoryInfo DirecInfo)
{
   if (DirectInfo.Exists)
   {
       foreach (FileInfo fileInfo in DirecInfo.GetFiles("*.zip"))
       {
           Process process = new Process();
           process.StartInfo.FileName = @"C:\Program Files\7-zip\7z.exe";
           process.StartInfo.Arguments = @" e C:\Directory\" + fileInfo.Name + @" -o C:\Directory";
           process.Start();
       }
   }
}

 

2.根据压缩算法LZMA SDK

LZMA SDK里面包括了压缩和解压缩算法的C#源码(当前版本是4.65)
下载地址: http://sourceforge.net/projects/sevenzip/files/LZMA%20SDK/4.65/lzma465.tar.bz2/download
 
3.在.NET应用程序中使用7-Zip的压缩/解压缩功能(作者 Abel Avram 译者 赵劼 )
开发人员Eugene Sichkar创建了一系列7-Zip动态链接库的C#接口,.NET应用程序中使用7-Zip的压缩/解压缩功能了。
据Eugene称,该项目实现了以下接口:
  • IProgress - 基本进度的回调
  • IArchiveOpenCallback - 打开压缩包的回调
  • ICryptoGetTextPassword - 为压缩提示密码的回调
  • IArchiveExtractCallback - 对压缩包进行解压的回调
  • IArchiveOpenVolumeCallback - 打开额外压缩卷的回调
  • ISequentialInStream - 基本的只读数据流接口
  • ISequentialOutStream - 基本的只写数据流的接口
  • IInStream - 可以随机读取的输入数据流接口
  • IOutStream - 输出数据流接口
  • IInArchive - 主要压缩接口
具体的使用方式可以参考源码中示例.
4.SevenZipSharp 

markhor 创建了SevenZipSharp 项目,SevenZipSharp 是开源的,里面实现了自解压和压缩所有7-ZIP支持的格式.它改进了7-Zip动态链接库的C#接口的一些方法.

常用压缩/解压缩示例(引自SevenZipSharp示例文件):

解压缩文件

using (SevenZipExtractor tmp = new SevenZipExtractor(@"d:\Temp\7z465_extra.7z")) 
{                
     for (int i = 0; i < tmp.ArchiveFileData.Count; i++) 
     { 
        tmp.ExtractFiles(@"d:\temp\!Пусто\", tmp.ArchiveFileData[i].Index); 
     } 
     //tmp.ExtractFiles(@"d:\temp\!Пусто\", 1, 3, 5); 
}

分卷压缩

SevenZipExtractor.SetLibraryPath(@"d:\Work\Misc\7zip\9.04\CPP\7zip\Bundles\Format7zF\7z.dll"); 
using (SevenZipExtractor tmp = new SevenZipExtractor(@"d:\Temp\SevenZip.7z.001")) 
{                 
    tmp.ExtractArchive(@"d:\Temp\!Пусто"); 
}

 

 压缩文件

SevenZipCompressor tmp = new SevenZipCompressor();             
tmp.CompressFiles(@"d:\Temp\arch.7z", @"d:\Temp\log.txt"); 
tmp.CompressDirectory(@"c:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\1033", @"D:\Temp\arch.7z");

 

 

压缩ZIP文件

 

SevenZipCompressor tmp = new SevenZipCompressor(); 
tmp.ArchiveFormat = OutArchiveFormat.Zip; 
tmp.CompressFiles(@"d:\Temp\arch.zip", @"d:\Temp\gpl.txt", @"d:\Temp\ru_office.txt");

 

多线程解压缩

Thread t1 = new Thread(() => 
{ 
    using (SevenZipExtractor tmp = new SevenZipExtractor(@"D:\Temp\7z465_extra.7z")) 
    { 
        tmp.FileExtractionStarted += new EventHandler<FileInfoEventArgs>((s, e) => 
        { 
            Console.WriteLine(String.Format("[{0}%] {1}", 
                e.PercentDone, e.FileInfo.FileName)); 
        }); 
        tmp.ExtractionFinished += new EventHandler((s, e) => { Console.WriteLine("Finished!"); }); 
        tmp.ExtractArchive(@"D:\Temp\t1"); 
    } 
}); 

 

Thread t2 = new Thread(() => 
{ 
    using (SevenZipExtractor tmp = new SevenZipExtractor(@"D:\Temp\7z465_extra.7z")) 
    { 
        tmp.FileExtractionStarted += new EventHandler<FileInfoEventArgs>((s, e) => 
        { 
            Console.WriteLine(String.Format("[{0}%] {1}", 
                e.PercentDone, e.FileInfo.FileName)); 
        }); 
        tmp.ExtractionFinished += new EventHandler((s, e) => { Console.WriteLine("Finished!"); }); 
        tmp.ExtractArchive(@"D:\Temp\t2"); 
    } 
}); 

 

t1.Start(); 
t2.Start(); 
t1.Join(); 
t2.Join();

 

 

多线程压缩

Thread t1 = new Thread(() => 
{ 
    SevenZipCompressor tmp = new SevenZipCompressor();              
    tmp.FileCompressionStarted += new EventHandler<FileNameEventArgs>((s, e) => 
    { 
        Console.WriteLine(String.Format("[{0}%] {1}", 
            e.PercentDone, e.FileName)); 
    }); 
    tmp.CompressDirectory(@"D:\Temp\t1", @"D:\Temp\arch1.7z"); 

});

 

Thread t2 = new Thread(() => 
{ 
    SevenZipCompressor tmp = new SevenZipCompressor(); 
    tmp.FileCompressionStarted += new EventHandler<FileNameEventArgs>((s, e) => 
    { 
        Console.WriteLine(String.Format("[{0}%] {1}", 
            e.PercentDone, e.FileName)); 
    }); 
    tmp.CompressDirectory(@"D:\Temp\t2", @"D:\Temp\arch2.7z"); 
}); 
t1.Start(); 
t2.Start(); 
t1.Join(); 
t2.Join(); 

 

posted @ 2010-04-03 19:25  逆时针  阅读(25742)  评论(3编辑  收藏  举报