利用7z来分卷压缩文件

最近做一个发送Email的小工具,里面有个附件的功能,

如果附件太大了,一般都是用winara 分卷压缩的,然后在上传。

如何用程序来实现自动的分卷压缩的呢?因为RAR不是免费的,就上网找了7z

 

安装好7z,把7z.dll拷贝到程式的目录下,引用SevenZipSharp这个DLL

引进 using SevenZip.Sdk.Compression;
using SevenZip; 这两个命名空间

 

ExpandedBlockStart.gif 代码
  ///  
         
///  切割文件,当文件的大小大于配置文件的容量中,就切割文件
         
///  

         
///    User下PDF文件的路径eg User/2000/01/01/xxxx.pdf
         
///   User/2000/01/01/temp
         
///  
          public   bool  SplitFile( string  filePath, string  descDecPath)
         {
             FileInfo fInfo 
=   new  FileInfo(filePath);
             
string  copyFilePath  =  Path.Combine(descDecPath, fInfo.Name);
             
// if (fInfo.Length <= this.fileSize) return false;
             
             
if  ( ! Directory.Exists(descDecPath))
             {
                 Directory.CreateDirectory(descDecPath);
             }
             File.Copy(filePath, copyFilePath);
             Application.DoEvents();

             SevenZipCompressor tmp 
=   new  SevenZipCompressor();
             SevenZipCompressor.SetLibraryPath(Application.StartupPath 
+   " /7z.dll " );
             
// tmp.VolumeSize =fInfo.Length <=  this.fileSize ? 0 :this.fileSize;
              tmp.VolumeSize  =   this .fileSize; // 卷的大小,最大只能分离1000个文件
             tmp.ArchiveFormat  =  OutArchiveFormat.SevenZip;
             tmp.CompressDirectory(descDecPath,copyFilePath );

             File.Delete(copyFilePath);
//  删除Copy的文件

             
if  (Directory.GetFiles(descDecPath).Length  ==   1 )
             {
                 
foreach  ( string  path  in  Directory.GetFiles(descDecPath))
                 {
                     File.Delete(path);
                 }
                 tmp.VolumeSize 
=   0 ;
                 tmp.CompressFiles(copyFilePath
+ " .7z "  ,
                                   
new   string [] { filePath }); // 压缩文件

             }
             Application.DoEvents();
             
return   true ;


         }

 

由于没有文件的分卷压缩的类库,所以我求其次,用文件夹的分卷压缩,先把文件copy到一个临时的文件夹中,然后对这个临时的文件夹进行分卷压缩

见代码:

 

转载于:https://www.cnblogs.com/86188281/archive/2010/02/09/1666262.html

你可能感兴趣的:(利用7z来分卷压缩文件)