linyawen

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

用 ZIPTV 控件包实现
用到其中的 TZip 和 TUnZip 控件
函数:
function DeCompressFile(sourceFile, targetPath: string): Boolean;
var
  FilesExtracted: Integer;
begin
  result := False;
  UnZIP1.ArchiveFile := sourceFile; // archive filename
  //   UnZIP1.Passwords.Add('123');
  UnZIP1.ConfirmOverwrites := false; // default = False
  UnZIP1.RecurseDirs := true; // default = False
  UnZIP1.FileSpec.Clear(); //
  UnZIP1.FileSpec.Add('*.*'); // *.* = extract all
  UnZIP1.ExtractDir := targetPath; //
  FilesExtracted := UnZIP1.Extract();
  if FilesExtracted = 0 then
    result := false
  else
    result := true;
end;

function CompressFile(sourcePath, targetFName: string): Boolean;
var
  FilesCompressed: Integer;
begin
  result := False;
  if FileExists(targetFName) then
    EraseFile(targetFName, doAllowUndo); // EraseFile is in ztvBase.pas
  Zip1.ArchiveFile := targetFName; // archive filename
  Zip1.DateAttribute := daFileDate; // default value
  Zip1.StoredDirNames := sdRelative; // default value
  Zip1.CompressMethod := cmDeflate; // default value
  Zip1.RecurseDirs := true; // default = False
  Zip1.Switch := swAdd; // default value
  Zip1.StoreEmptySubDirs := False; // default value
  Zip1.EncryptHeaders := false; // default = False
  Zip1.ExcludeSpec.Clear();
  Zip1.FileSpec.Clear();
  Zip1.FileSpec.Add(sourcePath + '*.*');
    // test with c:\windows\*.txt
  // ****************************************************************
  // NOTE: for a better understanding of how the Attributes property
  // works with file attributes see demo demos\filescan\fs_demo.dpr.
  // ****************************************************************
  // See the Attributes property in the object inspector
  // Set Zip1 Attributes property by calling the SetAttribute method
  Zip1.SetAttribute(fsZeroAttr, True); // default
  Zip1.SetAttribute(fsArchive, True); // default
  Zip1.SetAttribute(fsDirectory, True); // default = False
  Zip1.SetAttribute(fsHidden, True); // default = False
  Zip1.SetAttribute(fsReadOnly, True); // default
  Zip1.SetAttribute(fsSysFile, True); // default = False
  // See the AttributesEx property in teh object inspector
  // Set the AttributesEx property by calling the SetAttributeEx method.
  // Exclude none
  Zip1.SetAttributeEx(fsZeroAttr, False); // default
  Zip1.SetAttributeEx(fsArchive, False); // default
  Zip1.SetAttributeEx(fsDirectory, False); // default
  Zip1.SetAttributeEx(fsHidden, False); // default
  Zip1.SetAttributeEx(fsReadOnly, False); // default
  Zip1.SetAttributeEx(fsSysFile, False); // default
  //   UnZIP1.Password:='huaruan';
  FilesCompressed := Zip1.Compress();
  //   ShowMessage( 'Files Compressed: ' + IntToStr( FilesCompressed ) );
  result := true;
end;
调用例子:
   if not CompressFile( 'c:\temp\', 'c:\test.zip') then
     begin
       showmessage('压缩文件失败,请检查路径正确性!');
       exit;
     end;
//-------------------------------------
    if not deCompressFile('c:\test.zip', 'c:\temp\') then
    begin
      showmessage('解压压缩文件失败,请检查是否为该系统的压缩文件!');
      exit;
    end;

-----转自http://www.delphibbs.com/keylife/iblog_show.asp?xid=25386

posted on 2011-04-20 16:35  linyawen  阅读(1460)  评论(0编辑  收藏  举报