unit ShellZip;
interface
function NumProcessThreads: integer;
function ShellZip(zipfile, sourcefolder:string; filter: string = ''): boolean;
function ShellUnzip(zipfile, targetfolder: string; filter: string = ''): boolean;
implementation
uses Comobj, Windows,Tlhelp32;
const
SHCONTCH_NOPROGRESSBOX = 4;
SHCONTCH_AUTORENAME = 8;
SHCONTCH_RESPONDYESTOALL = 16;
SHCONTF_INCLUDEHIDDEN = 128;
SHCONTF_FOLDERS = 32;
SHCONTF_NONFOLDERS = 64;
function ShellUnzip(zipfile, targetfolder: string; filter: string = ''): boolean;
var
shellobj: variant;
srcfldr, destfldr: variant;
shellfldritems: variant;
begin
shellobj := CreateOleObject('Shell.Application');
srcfldr := shellobj.NameSpace(zipfile);
destfldr := shellobj.NameSpace(targetfolder);
shellfldritems := srcfldr.Items;
if (filter <> '') then
shellfldritems.Filter(SHCONTF_INCLUDEHIDDEN or SHCONTF_NONFOLDERS or SHCONTF_FOLDERS,filter);
destfldr.CopyHere(shellfldritems, SHCONTCH_NOPROGRESSBOX or SHCONTCH_RESPONDYESTOALL);
end;
function NumProcessThreads: integer;
var
hsnapshot: THandle;
Te32: TTHREADENTRY32;
proch: dword;
procthreads: integer;
begin
procthreads := 0;
proch := GetCurrentProcessID;
hSnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
Te32.dwSize := sizeof(TTHREADENTRY32);
if Thread32First(hSnapShot, Te32) then
begin
if te32.th32OwnerProcessID = proch then
inc(procthreads);
while Thread32Next(hSnapShot, Te32) do
begin
if te32.th32OwnerProcessID = proch then
inc(procthreads);
end;
end;
CloseHandle (hSnapShot);
Result := procthreads;
end;
function ShellZip(zipfile, sourcefolder:string; filter: string = ''): boolean;
const
emptyzip: array[0..23] of byte = (80,75,5,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
var
ms: TMemoryStream;
shellobj: variant;
srcfldr, destfldr: variant;
shellfldritems: variant;
numt: integer;
begin
if not FileExists(zipfile) then
begin
// create a new empty ZIP file
ms := TMemoryStream.Create;
ms.WriteBuffer(emptyzip, sizeof(emptyzip));
ms.SaveToFile(zipfile);
ms.Free;
end;
numt := NumProcessThreads;
shellobj := CreateOleObject('Shell.Application');
srcfldr := shellobj.NameSpace(sourcefolder);
destfldr := shellobj.NameSpace(zipfile);
shellfldritems := srcfldr.Items;
if (filter <> '') then
shellfldritems.Filter(SHCONTF_INCLUDEHIDDEN or SHCONTF_NONFOLDERS or SHCONTF_FOLDERS,filter);
destfldr.CopyHere(shellfldritems, 0);
// wait till all shell threads are terminated
while NumProcessThreads <> numt do
begin
sleep(100);
end;
end;
end.