您的位置:首页 > 编程语言 > Delphi

Delphi-VclZip用法详解 (2011-09-28 23:59:18)

2017-12-14 18:41 441 查看

转载▼

标签: 


分包

 


目录结构

 


压缩文件

 


目标目录

 


信息保存

分类: 编程语言
VclZip用法详

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[/b] 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;  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: