Delphi VclZip用法详

 VclZip用法详
Delphi  VclZip用法详 - ╰★张志峰★╮ - zhangzhifeng688的博客转载
  


Vclzip控件主要的类为TVclUnZip 和TVclZip 其中,TVclZip继承自TVclUnZip。

网上的转帖用法:

function Zip(ZipMode,PackSize:Integer;ZipFile,UnzipDir:String):Boolean; //压缩或解压缩文件
var ziper:TVCLZip;
begin
//函数用法:Zip(压缩模式,压缩包大小,压缩文件,解压目录)
//ZipMode为0:压缩;为1:解压缩 PackSize为0则不分包;否则为分包的大小
try
if copy(UnzipDir, length(UnzipDir), 1) = '\' then
UnzipDir := copy(UnzipDir, 1, length(UnzipDir) - 1); //去除目录后的“\”
ziper:=TVCLZip.Create(application); //创建zipper
ziper.DoAll:=true; //加此设置将对分包文件解压缩有效
ziper.OverwriteMode:=Always; //总是覆盖模式
if PackSize<>0 then begin //如果为0则压缩成一个文件,否则压成多文件
ziper.MultiZipInfo.MultiMode:=mmBlocks; //设置分包模式
ziper.MultiZipInfo.SaveZipInfoOnFirstDisk:=True; //打包信息保存在第一文件中
ziper.MultiZipInfo.FirstBlockSize:=PackSize; //分包首文件大小
ziper.MultiZipInfo.BlockSize:=PackSize; //其他分包文件大小
end;
ziper.FilesList.Clear;
ziper.ZipName := ZipFile; //获取压缩文件名
if ZipMode=0 then begin //压缩文件处理
ziper.FilesList.Add(UnzipDir+'\*.*'); //添加解压缩文件列表
Application.ProcessMessages; //响应WINDOWS事件
ziper.Zip; //压缩
end else begin
ziper.DestDir:= UnzipDir; //解压缩的目标目录
ziper.UnZip; //解压缩
end;
ziper.Free; //释放压缩工具资源
Result:=True; //执行成功
except
Result:=False;//执行失败
end;
end;

制作带目录结构的压缩指定目录:

function AddZipFile(ZipFileName,FileName:pchar):integer;stdcall;
var
   ziper:TVclZip;
begin
   result:=0;
 try
   try
    ziper:=TVclZip.Create(nil);
    ziper.OverwriteMode:=Always;//总是覆盖
    ziper.DoAll:=true;//压缩所有文件
    ziper.RelativePaths:=true;//是否保持目录结构
    ziper.AddDirEntriesOnRecurse:=true;
    ziper.RecreateDirs:=true;//创建目录
    ziper.StorePaths:=true;//保存目录信息
    //ziper.Recurse:=true;
   except
    exit;
   end;
    if FileExists(StrPas(ZipFileName)) then
    begin
      if UnZipFile(ZipFileName,TempDir)=1 then
        begin
          ziper.FilesList.Add(TempDir+StrPas(ZipFileName)+'\*.*');
          ziper.FilesList.Add(StrPas(FileName));
          ziper.ZipName:=strpas(ZipFileName);
          ziper.Zip;
          result:=1;
        end;
    end
    else
    begin
      ziper.FilesList.Add(FileName);
      ziper.ZipName:=StrPas(ZipFileName);
      ziper.zip;
      result:=1;
    end;
 finally
   ziper.Free;
  end;

把指定目录(带子目录)的所有文件压缩到一个目录下:

function AddDirAll(Dir,ZipFileName:pchar):integer;stdcall;
var
   Ziper:TVclZip;
   FileRec: TSearchrec;
   TempDir:String;
begin
   if FindFirst(Strpas(Dir),faAnyFile,FileRec) = 0 then
      begin
        repeat
          if (FileRec.Attr and faDirectory) <> 0  then
            begin
              TempDir:=StrPas(Dir)+'\'+FileRec.Name;
              AddDirAll(PChar(TempDir),ZipFileName);
              end;
         if (FileRec.Attr and faAnyFile )<> 0 then
            begin
              result:=AddZipFile(ZipFileName,Pchar(TempDir+'\*.*'));
              end;
          until FindNext(FileRec) <> 0 ;
        end

 end;

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值