I would like to know how could i use Windows Zip API to compress a file in a
Delphi program.
I found an example in C# though
http://www.codeproject.com/KB/cs/compresswithwinshellapics.aspx
Hopefully some one could come out with a working example for Delphi.
Thank you.
> I would like to know how could i use Windows Zip API to
> compress a file in a Delphi program.
There is no Zip API in Windows. The article you mentioned is exploiting a
feature of XP and later systems where .zip files are treated as compresed
folders by the Windows Shell, and thus can take part in folder-based
operations. To duplicate the same behavior described in the article, use
the SHFileOperation() API function to copy your desired folders/files into
the .zip "folder", ie (untested):
const
EmptyZip: array[0..23] of Byte = ($50, $4B, $5, $6, $0, $0, $0, $0,
$0, $0, $0, $0, $0, $0, $0, $0, $0, $0, $0, $0, $0, $0, $0, $0);
var
fs: TFileStream;
op: SHFILEOPSTRUCT;
begin
// Create an empty zip file
fs := TFileStream.Create('myfile.zip', fmCreate);
try
fs.WriteBuffer(EmptyZip[0], 24);
finally
fs.Free;
end;
// Copy a folder and its contents into the newly created zip file
FillChar(op, 0, SizeOf(op);
op.wFunc := FO_COPY;
op.pFrom := 'some folder'#0;
op.pTo := 'myfile.zip';
if SHFileOperation(op) = 0 then
begin
// Ziping a file using the Windows Shell API
// creates another thread where the zipping is executed.
// This means that it is possible that this code would end
// before the zipping thread starts to execute which would
// cause the zip to never occur and you will end up with just
// an empty zip file. So wait a second and give the zipping
// thread time to get started
Sleep(1000);
end;
end;
Otherwise, you will have to use a third-party API instead, such as the
ZipForge VCL component. You will have to do this anyway if you want to
support pre-XP systems.
Gambit
Here is a very rough adaptation... It should get you started. I never used
the Windows Shell (until now) to compress files programmatically, but aside
from the annoying progress display, it worked well. You might want to look
at other readily available components to have a better control.
procedure TForm1.Button1Click(Sender: TObject);
const
Data: array[0..21] of Byte = ($50, $4B, $05, $06, $00, $00,
$00, $00, $00, $00, $00, $00,
$00, $00, $00, $00, $00, $00,
$00, $00, $00, $00);
var
F: File of Byte;
Shell, Zip: Variant;
begin
{ Create empty file }
AssignFile(F, 'C:\Test.zip');
Rewrite(F);
BlockWrite(F, Data, SizeOf(Data));
CloseFile(F);
{ Add files }
Shell := CreateOleObject('Shell.Application');
Zip := Shell.Namespace('C:\Test.zip');
Zip.CopyHere('C:\Unit1.pas');
repeat
Application.ProcessMessages;
until Zip.Items.Count = 1;
Zip := Unassigned;
Shell := Unassigned;
end;
Philippe
"Edward" <edwa...@yahoo.com> wrote in message
news:47e9...@newsgroups.borland.com...
> I would like to know how could i use Windows Zip API to compress a file in a
> Delphi program.
Wouldn't it be easier to use the ZLib unit which is already available in
Delphi?
Groetjes,
Bob Swart
--
Bob Swart Training & Consultancy (eBob42.com) Forever Loyal to Delphi
CodeGear Technology Partner -- CodeGear RAD Studio Reseller (BeNeLux)
Blog: http://www.drbob42.com/blog - RSS: http://eBob42.com/weblog.xml
Bob Swart wrote:
>> I would like to know how could i use Windows Zip API to compress a file
>> in a Delphi program.
> Wouldn't it be easier to use the ZLib unit which is already available in
> Delphi?
But ZLib does not create ZIP files, does it?
MfG
twm
--
With best regards, Mike Shkolnik
Scalabium Software
http://www.scalabium.com
mshk...@scalabium.com
Remy's version doesn't work, it will make a new copy of the source file
instead of copying it into the zip file.
Philippe's version work well but i found that it only support short file
name, must be less than 8 characters.
Thank you all for the examples.