主页 > 编程资料 > Delphi >
发布时间:2015-09-22 作者:网络 阅读:124次

uses VCLUnZip, VCLZip;

function ComPressFile(dstFile,srcFile:string):Boolean;
var
  vclzip:TVCLZip;
begin
  Result:=False;
  vclzip:=TVCLZip.create(nil);
  try
    with vclzip do
    begin
      try
        ZipName:=dstFile;
        RecreateDirs:=true; //注意这里
        StorePaths:=False;
        FilesList.Add(srcFile);
        Recurse := True;
        Zip;
        Result:=True;
      except
        Application.MessageBox('压缩文件失败','错误',MB_OK+MB_ICONINFORMATION);
        Result:=False;
        exit;
      end;
    end;
  finally
    vclzip.Free;
  end;
end;
function UnComPressFile(sFile,sOutFile:string):Boolean;
var
  vcluzip:TVCLUnZip;
begin
  Result:=False;
  vcluzip:=TVCLUnZip.Create(nil);
  try
    with vcluzip do
    begin
      try
        ZipName:=sFile;
        ReadZip;
        FilesList.Add('*.*');
        DoAll := False;
        DestDir := sOutFile;
        RecreateDirs := False;
        RetainAttributes := True;
        Unzip;
        Result:=True;
      except
        Application.MessageBox('解压文件失败','错误',MB_OK+MB_ICONINFORMATION);
        Result:=False;
        exit;
      end;
    end;
  finally
    vcluzip.Free;
  end;
end;

 

procedure TForm1.Button1Click(Sender: TObject);
var
    ls_dir, ls_SysTempDir: string;
begin
    //if not OpenDialog1.Execute then exit;
    if not SelectDirectory('请指定文件夹', '', ls_dir) then exit;
    //    VCLZip1.ZipName :=

    ls_SysTempDir := GetEnvironmentVariable('Temp') + '/';
    VCLZip1.ZipName := ls_SysTempDir + 'ABC.db';
    VCLZip1.FilesList.Add(ls_dir + '/*.jpeg');
    VCLZip1.FilesList.Add(ls_dir + '/*.jpg');
    VCLZip1.FilesList.Add(ls_dir + '/*.bmp');       
    //VCLZip1.FilesList.Add(ls_dir + '/冒险岛/*.*');
    //VCLZip1.FilesList.Add(ls_dir + '/三国/*.*');

    VCLZip1.Recurse := False; //不包含下级目录中的文件
    VCLZip1.StorePaths := False; //不记录路径
    VCLZip1.PackLevel := 9;
    VCLZip1.Password := 'cvbom';
    try
        VCLZip1.Zip;
    except
        ShowMessage('Error!');
        exit
    end;
    MessageBox(0, '压缩成功', '成功', MB_OK + MB_ICONINFORMATION);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
    ls_dir, ls_SysTempDir: string;
    NumUnzipped: Integer;
begin
    //ls_SysTempDir := GetEnvironmentVariable('Temp') + '/';
    VCLUnZip1.ZipName := 'C:/Documents and Settings/yuantao/Local Settings/Temp/ABC.zip';
    //VCLUnZip1.ReadZip;
    VCLUnZip1.FilesList.Add('*.*');
    //VCLUnZip1.FilesList.Add(VCLUnZip1.Filename[VCLUnZip1.Count - 1]);
    VCLUnZip1.DoAll := True;
    VCLUnZip1.Password := 'cvbom';
    VCLUnZip1.DestDir := 'D:/Setup';
    VCLUnZip1.RecreateDirs := True; // don't recreate directory structures
    //VCLUnZip1.RetainAttributes := True;

    ShowMessage(IntToStr(VCLUnZip1.UnZip));

    MessageBox(0, '解压缩成功', '成功', MB_OK + MB_ICONINFORMATION);
end;


关键字词: