DBMNG数据库管理与应用

科学是实事求是的学问,来不得半点虚假。
当前位置:首页 > 经验分享 > Delphi

VclZip用法详解

VclZip用法详

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

网上的转帖用法:

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

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

 

function AddZipFile(ZipFileName,FileName:pchar):integer;stdcall;  
  1. var  
  2.    ziper:TVclZip;  
  3. begin  
  4.    result:=0;  
  5.  try  
  6.    try  
  7.     ziper:=TVclZip.Create(nil);  
  8.     ziper.OverwriteMode:=Always;//总是覆盖   
  9.     ziper.DoAll:=true;//压缩所有文件   
  10.     ziper.RelativePaths:=true;//是否保持目录结构   
  11.     ziper.AddDirEntriesOnRecurse:=true;  
  12.     ziper.RecreateDirs:=true;//创建目录   
  13.     ziper.StorePaths:=true;//保存目录信息   
  14.     //ziper.Recurse:=true;   
  15.    except  
  16.     exit;  
  17.    end;  
  18.     if FileExists(StrPas(ZipFileName)) then  
  19.     begin  
  20.       if UnZipFile(ZipFileName,TempDir)=1 then  
  21.         begin  
  22.           ziper.FilesList.Add(TempDir+StrPas(ZipFileName)+'\*.*');  
  23.           ziper.FilesList.Add(StrPas(FileName));  
  24.           ziper.ZipName:=strpas(ZipFileName);  
  25.           ziper.Zip;  
  26.           result:=1;  
  27.         end;  
  28.     end  
  29.     else  
  30.     begin  
  31.       ziper.FilesList.Add(FileName);  
  32.       ziper.ZipName:=StrPas(ZipFileName);  
  33.       ziper.zip;  
  34.       result:=1;  
  35.     end;  
  36.  finally  
  37.    ziper.Free;  
  38.   end;  
 

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

 
function AddDirAll(Dir,ZipFileName:pchar):integer;stdcall;  
  1. var  
  2.    Ziper:TVclZip;  
  3.    FileRec: TSearchrec;  
  4.    TempDir:String;  
  5. begin  
  6.    if FindFirst(Strpas(Dir),faAnyFile,FileRec) = 0 then  
  7.       begin  
  8.         repeat  
  9.           if (FileRec.Attr and faDirectory) <> 0  then  
  10.             begin  
  11.               TempDir:=StrPas(Dir)+'\'+FileRec.Name;  
  12.               AddDirAll(PChar(TempDir),ZipFileName);  
  13.               end;  
  14.          if (FileRec.Attr and faAnyFile )<> 0 then  
  15.             begin  
  16.               result:=AddZipFile(ZipFileName,Pchar(TempDir+'\*.*'));  
  17.               end;  
  18.           until FindNext(FileRec) <> 0 ;  
  19.         end  
  20.   
  21.  end;  
本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号