Delphi-VclZip用法详解

Vclzip控件主要的类为TVclUnZip 和TVclZip 其中,TVclZip继承自TVclUnZip。
网上的转帖用法:ruby

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;

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

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;
相关文章
相关标签/搜索