We help IT Professionals succeed at work.

Delphi zipping files using KAZip - how do i add it to the existing code i have

Garydene
Garydene asked
on
Delphi - zipping files using KAZip

i need to zip the files. So i have decided to add zipping procedure in the Delph exe i am calling from javascript.
Can someone assist me to add in the lines of code i need to tell Delpi to use KAZIP and to create a zip file called Mirczip and add the contents of the 'C:\send2Mirc\casebin\ to it.


 program Project2;

uses
  SysUtils, Classes, Dialogs,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdHTTP;

{$R *.res}

var
    fs : tfilestream;
    xx : widestring;
    dirname : string;
    zipfile : string;

procedure DoPost;
var
  IdHTTP1: TIdHTTP;
begin
  IdHTTP1:= TIdHTTP.Create(nil);
  try
    dirname:= 'C:\send2Mirc\casebin\';
    zipfile:= dirname + '\Mirczip.zip';
    fs:=tfilestream.create(zipfile,fmOpenRead);
    IdHTTP1.Request.ContentType:='application/x-zip-compressed';
    IdHTTP1.Request.BasicAuthentication := True;
    IdHTTP1.Request.Username:='username';
    IdHTTP1.Request.Password:='password';
    showmessage('Posting now file -> ' + zipfile )  ;
    xx:=IdHTTP1.Post('http://10.0.1.1:8080/TeachFile/zip?overwrite=no',fs);
    showmessage(xx);
    IdHTTP1.Free;
  except
    on e: exception do
    begin
      IdHTTP1.Free;
      if pos('Connection Closed Gracefully.',e.message)>0 then
      begin
        // it was just an exception that must be trapped or compiled version will crash
      end
      else
      begin
        // just continue on without interruption
      end;
    end;
  end;
end;

begin
  DoPost;
end.
 
Comment
Watch Question

Author

Commented:
A javascript calls the above Delphi procedure to send the zipped file to the server.
now i need it to zip the contents of a folder BEFORE posting it.
Can a Delphi expert look at the code i posted and show me what must go where to get it to do one simple thing create a zip file and add the contents of a particular folder.
some things i've researched that i think i need but don't know where goes what.
uses     KaZip;
FS : TFileStream;
KFilename : TFileName ;
KaZip : TKaZip:
KaZip : TKaZip.Create(nil);.
Top Expert 2010
Commented:
Hi Garydene. Try this code:
program Project2;

uses
  SysUtils, Classes, Dialogs, 
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdHTTP,
  KaZip;

{$R *.res}

var
    fs : tfilestream;
    xx : widestring;
    dirname : string;
    zipfile : string;

procedure DoPost;
var
  IdHTTP1: TIdHTTP;
begin
  IdHTTP1:= TIdHTTP.Create(nil);
  try
    dirname:= 'C:\send2Mirc\casebin\';
    zipfile:= dirname + '\Mirczip.zip';
    
    // creating ZIP
    with TKAZip.Create(nil) do
      try
        CreateZip( zipfile );
        Open (zipfile);
        AddFolder(dirname, dirname, '*.*', True);
        Remove(ExtractFileName(zipfile));
        Close;
      finally
        Free;
      end;
    // end of ZIP

    fs:=tfilestream.create(zipfile,fmOpenRead);
    IdHTTP1.Request.ContentType:='application/x-zip-compressed';
    IdHTTP1.Request.BasicAuthentication := True;
    IdHTTP1.Request.Username:='username';
    IdHTTP1.Request.Password:='password';
    showmessage('Posting now file -> ' + zipfile )  ;
    xx:=IdHTTP1.Post('http://10.0.1.1:8080/TeachFile/zip?overwrite=no',fs);
    showmessage(xx);
    IdHTTP1.Free;
  except
    on e: exception do
    begin
      IdHTTP1.Free;
      if pos('Connection Closed Gracefully.',e.message)>0 then
      begin
        // it was just an exception that must be trapped or compiled version will crash
      end
      else
      begin
        // just continue on without interruption
      end;
    end;
  end;
end;

begin
  DoPost;
end.

Open in new window

Author

Commented:

Excellent Solution aflarin it works perfect
many thanks