利用7z来分卷压缩文件

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

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

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

 

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

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

 

ExpandedBlockStart.gif 代码
  ///   <summary>
         
///  切割文件,当文件的大小大于配置文件的容量中,就切割文件
         
///   </summary>
         
///   <param name="filePath">  User下PDF文件的路径eg User/2000/01/01/xxxx.pdf </param>
         
///   <param name="descDecPath"> User/2000/01/01/temp </param>
         
///   <returns></returns>
          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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值