TZipper - adding to existing archive lazarus / 21-General ---------------------------------------------------------------------- geraldholdsworth · 2025-06-04 16:44 This has been covered a few times in the past, but no solution (apart from using Abrevia) have been forthcoming...that I can see. So, I looked into this. Essentially, a ZIP file is just a collection of compressed files, each file having it's own file detail entry. Therefore, it should just be a simple case of combining two or more ZIP files to create one big ZIP file. All that needs to be done is to combine the central library at the end and re-create the End of Central Library entry. With this in mind, and my code already using TZipper to ZIP files, I changed it so that the method ZIPs the file to a temporary file, rather than the existing one. I've now written a ZIP combiner method: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type  { TForm1 }  TForm1 = class(TForm)  Button1: TButton;  Button2: TButton;  Memo1: TMemo;  procedure Button1Click(Sender: TObject);  procedure CombineZIP(files: array of String);  procedure Button2Click(Sender: TObject); private  public  const   Lfolder='/Path/To/Your/Folder'; end; var Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin Memo1.Lines.Clear; CombineZIP([LFolder+'/Test- empty.zip',LFolder+'/Test-archive 1.zip']); CombineZIP([LFolder+'/Output.zip'    ,LFolder+'/Test-archive 2.zip']);end; procedure TForm1.CombineZIP(files: array of String); //Finds the central library and returns the end of central library function FindCL(var EoCL: Integer;var buffer: array of Byte): Integer; var  i: Integer=0; begin  //Start with a default (i.e., not found)  Result:=-1;  EoCL:=-1;  //Can't have a file smaller than 22 bytes  if Length(buffer)<22 then exit;  //Start here  i:=Length(buffer)-3;  //And work backwards until we find the EoCL marker  repeat    dec(i);  until((buffer[i  ]=$50)     and(buffer[i+1]=$4B)     and(buffer[i+2]=$05)     and(buffer[i+3]=$06))     or (i=0);  //Found OK?  if (buffer[i  ]=$50)  and(buffer[i+1]=$4B)  and(buffer[i+2]=$05)  and(buffer[i+3]=$06) then  begin   //Mark it   EoCL:=i;   //Retreive where the central library starts   Result:=buffer[i+$10]          +buffer[i+$11]<<8          +buffer[i+$12]<<16          +buffer[i+$13]<<24;  end; end;//Main method definitionsvar input     : array[0..1] of TFileStream; output    : TFileStream; inbuffer  : array[0..1] of array of Byte; outbuffer : array of Byte; ptr       : Integer=0;     cnt       : Integer=0; fileptr   : Cardinal=0; CL      : array[0..1] of Integer; EoCL      : array[0..1] of Integer; temp    : Cardinal=0; numfiles  : Cardinal=0; CLsize    : Cardinal=0; filesize  : Cardinal=0; ok        : Boolean=True;//Main method starts herebegin //Read in the files for ptr:=0 to 1 do begin  Memo1.Lines.Add('Opening '+files[ptr]);  input[ptr]:=TFileStream.Create(files[ptr],fmOpenRead OR fmShareDenyNone);  input[ptr].Position:=0;  SetLength(inbuffer[ptr],input[ptr].Size);  input[ptr].Read(inbuffer[ptr][0],input[ptr].Size);  input[ptr].Free;  //Get the position of the central library for each  CL[ptr]:=FindCL(EoCL[ptr],inbuffer[ptr]);  ok:=(ok)AND(CL[ptr]<>-1)AND(EoCL[ptr]<>-1);  Memo1.Lines.Add('File '+IntToStr(ptr+1)+' size                  : '+IntToHex(Length(inbuffer[ptr]),8));  if CL[ptr]<>-1 then   Memo1.Lines.Add('Central Library file '+IntToStr(ptr+1)+'       : '+IntToHex(CL[ptr],8));  if EoCL[ptr]<>-1 then  begin   Memo1.Lines.Add('End of Central Library file '+IntToStr(ptr+1)+': '+IntToHex(EoCL[ptr],8));   //Count the number of files stored   inc(n umfiles,inbuffer[ptr][EoCL[ptr]+$A]+inbuffer[ptr][EoCL[ptr]+$B]<<8);  end; end; //Create the output file if ok then begin  Memo1.Lines.Add('Total number of files        : '+IntToStr(numfiles));  //This will be the eventual central library size  CLsize:=(EoCL[0]-CL[0])+(EoCL[1]-CL[1]);  Memo1.Lines.Add('Eventual Central Library size: '+IntToHex(CLsize,8));  //This will be the eventual file size  filesize:=CL[0]+CL[1]+CLsize+22;  Memo1.Lines.Add('Eventual file size         : '+IntToHex(filesize,8));  SetLength(outbuffer,filesize);  //Write the files. The files from the second ZIP goes where the first CL was  fileptr:=0;  for cnt:=0 to 1 do  begin   Memo1.Lines.Add('Files from ZIP '+IntToStr(cnt+1)+' at          : '+IntToHex(fileptr,8));   for ptr:=0 to CL[cnt]-1 do outbuffer[fileptr+ptr]:=inbuffer[cnt][ptr];   inc(fileptr,CL[cnt]);  end;  //Write the CLs  for cnt:=0 to 1 do  begin   Memo1.Lines.Add('CL from ZIP '+IntToStr(cnt+1)+' at             : '+IntToHex(fileptr,8));   //We'll need to find each file entry and adjust by adding CL1 to the adddress   for ptr:=CL[cnt] to EoCL[cnt]-1 do   begin    outbuffer[fileptr-CL[cnt]+ptr]:=inbuffer[cnt][ptr];    if cnt>0 then   //Found a file?     if (inbuffer[cnt][ptr-$2E]=$50)     and(inbuffer[cnt][ptr-$2D]=$4B)     and(inbuffer[cnt][ptr-$2C]=$01)   and(inbuffer[cnt][ptr-$2B]=$02)then     begin      //Get the data offset      temp:=inbuffer[cnt][ptr-4]           +inbuffer[cnt][ptr-3]<<8           +inbuffer[cnt][ptr-2]<<16         +inbuffer[cnt][ptr-1]<<24;      Memo1.Lines.Add('CL file found at           : '+IntToHex((fileptr-CL[cnt]+ptr)-$2E,8));      Memo1.Lines.Add('Data offset                  : '+IntToHex(temp,8));    //Adjust the data offset      inc(temp,CL[cnt-1]);      Memo1.Lines.Add('New data offset              : '+IntToHex(temp,8));    //Save back      outbuffer[(fileptr-CL[cnt]+ptr)-4]:= temp AND $000000FF;      outbuffer[(fileptr-CL[cnt]+ptr)-3]:=(temp AND $0000FF00)>>8;      outbuffer[(fileptr-CL[cnt]+ptr)-2]:=(temp AND $00FF0000)>>16;      outbuffer[(fileptr-CL[cnt]+ptr)-1]:=(temp AND $FF000000)>>24;     end;   end;   inc(fileptr,CL[cnt]);  end;  //Write the central directory  fileptr:=filesize-22;  outbuffer[fileptr    ]:=$50;  outbuffer[fileptr+$01]:=$4B;  outbuffer[fileptr+$02]:=$05;  outbuffer[fileptr+$03]:=$06;  outbuffer[fileptr+$08]:= numfiles AND $00FF;  outbuffer[fileptr+$09]:=(numfiles AND $FF00)>>8;  outbuffer[fileptr+$0A]:= numfiles AND $00FF;  outbuffer[fileptr+$0B]:=(numfiles AND $FF00)>>8;  outbuffer[fileptr+$0C]:= CLsize AND $000000FF;  outbuffer[fileptr+$0D]:=(CLsize AND $0000FF00)>>8;  outbuffer[fileptr+$0E]:=(CLsize AND $00FF0000)>>16;  outbuffer[fileptr+$0F]:=(CLsize AND $FF000000)>>24;  outbuffer[fileptr+$10]:= (CL[0]+CL[1]) AND $000000FF;  outbuffer[fileptr+$11]:=((CL[0]+CL[1]) AND $0000FF00)>>8;  outbuffer[fileptr+$12]:=((CL[0]+CL[1]) AND $00FF0000)>>16;  outbuffer[fileptr+$13]:=((CL[0]+CL[1]) AND $FF000000)>>24;  //Save the data to a file  output:=TFileStream.Create(Lfolder+'/Output.zip',fmCreate OR fmShareDenyNone);  output.Position:=0;  output.Write(outbuffer[0],Length(outbuffer));  output.Free;  //Output the notes  Memo1.Lines.SaveToFile(LFolder+'/Output.txt'); end;end; procedure TForm1.Button2Click(Sender: TObject);//Create an empty ZIP filevar outbuffer: array of Byte; output   : TFileStream;begin Memo1.C lear; SetLength(outbuffer,22); outbuffer[$00]:=$50; outbuffer[$01]:=$4 B; outbuffer[$02]:=$05; outbuffer[$03]:=$06; output:=TFileStream.Creat e(Lfolder+'/Test-empty.zip',fmCreate OR fmShareDenyNone); output.Posit ion:=0; output.Write(outbuffer[0],Length(outbuffer)); output.Free; Mem o1.Lines.Add('Empty ZIP file created');end; end. When Button1 is clicked, it first combines the empty ZIP file (created by Button2) with one of the prepared ZIP files (which would be the temporary ZIP created as described), and outputs to a separate file. It then does it again, but this time using the newly created ZIP file and adding another file. This simulates adding two files to an empty ZIP, one by one. Of course, there are no checks to ensure the ZIP files are valid, aside from checking for where the End of Central Library and Central Library are. And, yes, there are probably better ways of doing this. Apologies if something like this has already been posted before. I'll probably add to this later on with a method to validate a ZIP file. ---------------------------------------------------------------------- geraldholdsworth · 2025-06-05 08:51 And, as promised, something to validate a ZIP file: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; type  { TForm1 }  TForm1 = class(TForm)  Panel1: TPanel;  Memo1: TMemo;  function FindCL(var EoCL: Integer;var buffer: array of Byte): Integer;  procedure FormDropFiles(Sender: TObject; const FileNames: array of string);  procedure ValidateZIPFile(filename: String); private  public  end; var Form1: TForm1; implementation {$R *.lfm} { TForm1 }  //Finds the central library and returns the end of central libraryfunction TForm1.FindCL(var EoCL: Integer;var buffer: array of Byte): Integer;var i: Integer=0;begin //Start with a default (i.e., not found) Result:=-1; EoCL:=-1; //Can't have a file smaller than 22 bytes if Length(buffer)<22 then exit; //Start here i:=Length(buffer)-3; //And work backwards until we find the EoCL marker repeat   dec(i); until((buffer[i  ]=$50)    and(buffer[i+1]=$4B)    and(buffer[i+2]=$05)    and(buffer[i+3]=$06))    or (i=0); //Found OK? if (buffer[i  ]=$50) and(buffer[i+1]=$4B) and(buffer[i+2]=$05) and(buffer[i+3]=$06) then begin  //Mark it  EoCL:=i;  //Retreive where the central library starts  Result:=buffer[i+$10]         +buffer[i+$11]<<8         +buffer[i+$12]<<16         +buffer[i+$13]<<24; end;end; procedure TForm1.FormDropFiles(Sender: TObject; const FileNames: array of string );var i: Integer=0;begin Memo1.Lines.Clear; for i:=0 to Length(FileNames)-1 do begin  ValidateZIPFile(FileNames[i]);  if i<>Length(FileNames)-1 then   Memo1.Lines.Add('----------------------- -----------------------------------'); end;end; procedure TForm1.ValidateZIPFile(filename: String);var Lfile   : TFileStream; Lbuffer : array of Byte; CL      : Integer=-1; EoCL    : Integer=-1; valid   : Boolean=False; check   : Boolean=False; temp    : Cardinal=0; numfiles: Integer=0; index   : Integer=0; ptr     : Cardinal=0; data    : Cardinal=0; size    : Cardinal=0; usize   : Cardinal=0; //Check the length of the extra field function CheckExtraField(addr,len: Cardinal): Boolean; var  total: Cardinal=0; begin  Result:=False;  Memo1.Lines.Add('Extra field offset           : '+IntToHex(addr,8));  Memo1.Lines.Add('Extra field given length     : '+IntToHex(len,8));  while(total-1)and(EoCL<>-1)then begin  valid:=True;  Memo1.Lines.Add('Central Library              : '+IntToHex(CL,8));  Memo1.Lines.Add('End of Central Library       : '+IntToHex(EoCL,8));  //Size of Central Library  temp:=Lbuffer[EoCL+$C]       +Lbuffer[EoCL+$D]<<8       +Lbuffer[EoCL+$E]<<16       +Lbuffer[EoCL+$F]<<24;  Memo1.Lines.Add('Central Library size       : '+IntToHex(temp,8));  //Test to see if it matches the references already found  check:=temp=EoCL-CL;  if check then Memo1.Lines.Add('Matches references')  else Memo1.Lines.Add('Does not match references');  valid:=(check)AND(valid);  //Total number of files  numfiles:=Lbuffer[EoCL+$A]           +Lbuffer[EoCL+$B]<<8;  Memo1.Lines.Add('Total number of files        : '+IntToStr(numfiles));  Memo1.Lines.Add('Checking files');  ptr:=CL;  index:=0;  while(index0 then    check:=CheckExtraField(ptr+$2E+Lbuffer[ptr+$1C]+Lbuffer [ptr+$1D]<<8,temp);   if check then Memo1.Lines.Add('Extra field size match')            else Memo1.Lines.Add('Extra field size mis- match');   valid:=(check)AND(valid);   //Compressed size   size:=Lbuffer[ptr+$14]        +Lbuffer[ptr+$15]<<8        +Lbuffer[ptr+$16]<<16        +Lbuffer[ptr+$17]<<24;   Memo1.Lines.Add('Compressed size              : '+IntToHex(size,8));   //Uncompressed size   usize:=Lbuffer[ptr+$18]         +Lbuffer[ptr+$19]<<8         +Lbuffer[ptr+$1A]<<16         +Lbuffer[ptr+$1B]<<24;   Memo1.Lines.Add('Uncompressed size          : '+IntToHex(usize,8));   //Data pointer   data:=Lbuffer[ptr+$2A]      +Lbuffer[ptr+$2B]<<8        +Lbuffer[ptr+$2C]<<16        +Lbuffer[ptr+$2D]<<24;   Memo1.Lines.Add('Data pointer               : '+IntToHex(data,8));   //Check the local file entry   Memo1.Lines.Add('Checking local file entry');   check:=(Lbuffer[data]=$50)       and(Lbuffer[data+1]=$4B)       and(Lbuffer[data+2]=$03)       and(Lbuffer[data+3]=$04);   if check then Memo1.Lines.Add('Valid signature')            else Memo1.Lines.Add('Invalid signature');   valid:=(check)AND(valid);   //Check compressed size   check:=Lbuffer[data+$12]         +Lbuffer[data+$13]<<8         +Lbuffer[data+$14]<<16         +Lbuffer[data+$15]<<24=size;   if check then Memo1.Lines.Add('Compressed size match')            else Memo1.Lines.Add('Compressed size mis-match');   valid:=(check)AND(valid);   //Check uncompressed size   check:=Lbuffer[data+$16]         +Lbuffer[data+$17]<<8         +Lbuffer[data+$18]<<16         +Lbuffer[data+$19]<<24=usize;   if check then Memo1.Lines.Add('Uncompressed size match')            else Memo1.Lines.Add('Uncompressed size mis-match');   valid:=(check)AND(valid);   //Check the extra field   temp:=Lbuffer[data+$1C]+Lbuffer[data+$1D]<<8;   check:=True;   if temp>0 then    check:=CheckExtraField(data+$1E+Lbuffer[data+$1A]+Lbuff er[data+$1B]<<8,temp);   if check then Memo1.Lines.Add('Extra field size match')            else Memo1.Lines.Add('Extra field size mis- match');   valid:=(check)AND(valid);   //Next entry - here we work out where it should be   temp:=ptr+$2E            +Lbuffer[ptr+$1C]+Lbuffer[ptr+$1D]<<8            +Lbuffer[ptr+$1E]+Lbuffer[ptr+$1F]<<8            +Lbuffer[ptr+$20]+Lbuffer[ptr+$21]<<8;   //Now we find the next marker   repeat    inc(ptr,1);   until(ptr=EoCL)or((Lbuffer[ptr  ]=$50)                  and(Lbuffer[ptr+1]=$4B)                  and(Lbuffer[ptr+2]=$01)                  and(Lbuffer[ptr+3]=$02));   //And check if they match   check:=ptr=temp;   if check then Memo1.Lines.Add('Central Library entry size match')            else Memo1.Lines.Add('Central Library entry size mis-match');   valid:=(check)AND(valid);   //Next file   inc(index);  end;  //Check we got all the files  check:=index=numfiles;  if check then Memo1.Lines.Add('Correct number of files found')           else Memo1.Lines.Add('Incorrect number of files');  valid:=(check)AND(valid); end; if valid then Memo1.Lines.Add('File is a valid ZIP file')          else Memo1.Lines.Add('File is an invalid ZIP file');end; end. ---------------------------------------------------------------------- gues1 · 2025-06-05 12:07 I'll start by saying that I don't know the internal path of a zip, but does it work even if the two files use two different compression methods (such as "deflate" and "LZMA")? ---------------------------------------------------------------------- geraldholdsworth · 2025-06-05 13:17 Should do. These routines don't touch the compressed data at all, and only touch the Local File Entry (just before each data), and the Central Library (or directory) at the end, which holds all of the file details and where to find the data. What they won't do is deal with ZIP64 or mulit-disc ZIP files...yet. ---------------------------------------------------------------------- geraldholdsworth · 2025-06-05 13:50 If you want some methods without the reporting to Memo, etc., I've put them into a single unit: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit ZIPTools;{ZIP Tools V1.00Written by Gerald J Holdsworth This source is free software; you can redistribute it and/or modify it underthe terms of the GNU General Public Licence as published by the FreeSoftware Foundation; either version 3 of the Licence, or (at your option)any later version. This code is distributed in the hope that it will be useful, but WITHOUT ANYWARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESSFOR A PARTICULAR PURPOSE.  See the GNU General Public Licence for moredetails. A copy of the GNU General Public Licence is available on the World Wide Webat . You can also obtain it by writingto the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,Boston, MA 02110-1335, USA.} {$mode ObjFPC}{$H+} interface uses Classes,SysUtils; function FindCL(var EoCL: Integer;var buffer: array of Byte): Integer;function ValidateZIPFile(var buffer: array of Byte): Boolean;function CombineZIP(files: array of String; outputfile: String): Boolean; implementation //Finds the central library and returns the end of central libraryfunction FindCL(var EoCL: Integer;var buffer: array of Byte): Integer;var i: Integer=0;begin //Start with a default (i.e., not found) Result:=-1; EoCL:=-1; //Can't have a file smaller than 22 bytes if Length(buffer)<22 then exit; //Start here i:=Length(buffer)-3; //And work backwards until we find the EoCL marker repeat   dec(i); until((buffer[i  ]=$50)    and(buffer[i+1]=$4B)    and(buffer[i+2]=$05)    and(buffer[i+3]=$06))    or (i=0); //Found OK? if (buffer[i  ]=$50) and(buffer[i+1]=$4B) and(buffer[i+2]=$05) and(buffer[i+3]=$06) then begin  //Mark it  EoCL:=i;  //Retreive where the central library starts  Result:=buffer[i+$10]         +buffer[i+$11]<<8         +buffer[i+$12]<<16         +buffer[i+$13]<<24; end;end; //Validate a ZIP filefunction ValidateZIPFile(var buffer: array of Byte): Boolean;var Lfile   : TFileStream; CL      : Integer=-1; EoCL    : Integer=-1; check   : Boolean=False; temp    : Cardinal=0; numfiles: Integer=0; index   : Integer=0; ptr     : Cardinal=0; data    : Cardinal=0; size    : Cardinal=0; usize   : Cardinal=0; //Check the length of the extra field function CheckExtraField(addr,len: Cardinal): Boolean; var  total: Cardinal=0; begin  Result:=False;  while(total=22 then begin  //Find the central library  CL:=FindCL(EoCL,buffer);  if(CL<>-1)and(EoCL<>-1)then  begin   Result:=True;   //Size of Central Library   temp:=buffer[EoCL+$C]      +buffer[EoCL+$D]<<8        +buffer[EoCL+$E]<<16        +buffer[EoCL+$F]<<24;   //Test to see if it matches the references already found   Result:=(temp=EoCL-CL)AND(Result);   //Total number of files   numfiles:=buffer[EoCL+$A]            +buffer[EoCL+$B]<<8;   ptr:=CL;   index:=0;   while(index0 then   Result:=(CheckExtraField(ptr+$2E+buffer[ptr+$1C]+buffer[ptr+$1D]<<8, temp))         AND(Result);    //Compressed size    size:=buffer[ptr+$14]         +buffer[ptr+$15]<<8         +buffer[ptr+$16]<<16         +buffer[ptr+$17]<<24;    //Uncompressed size    usize:=buffer[ptr+$18]          +buffer[ptr+$19]<<8          +buffer[ptr+$1A]<<16          +buffer[ptr+$1B]<<24;    //Data pointer    data:=buffer[ptr+$2A]         +buffer[ptr+$2B]<<8         +buffer[ptr+$2C]<<16         +buffer[ptr+$2D]<<24;    //Check the local file entry    Result:=(buffer[data]=$50)         and(buffer[data+1]=$4B)         and(buffer[data+2]=$03)         and(buffer[data+3]=$04)         AND(Result);    //Check compressed size    Result:=(buffer[data+$12]            +buffer[data+$13]<<8          +buffer[data+$14]<<16            +buffer[data+$15]<<24=size)       AND(Result);    //Check uncompressed size    Result:=(buffer[data+$16]            +buffer[data+$17]<<8            +buffer[data+$18]<<16            +buffer[data+$19]<<24=usize)         AND(Result);    //Check the extra field    temp:=buffer[data+$1C]+buffer[data+$1D]<<8;    if temp>0 then     Resu lt:=(CheckExtraField(data+$1E+buffer[data+$1A]+buffer[data+$1B]<<8,tem p))          AND(Result);    //Next entry - here we work out where it should be    temp:=ptr+$2E             +buffer[ptr+$1C]+buffer[ptr+$1D]<<8             +buffer[ptr+$1E]+buffer[ptr+$1F]<<8             +buffer[ptr+$20]+buffer[ptr+$21]<<8;    //Now we find the next marker    repeat     inc(ptr);    until(ptr=EoCL)or((buffer[ptr  ]=$50)                   and(buffer[ptr+1]=$4B)                   and(buffer[ptr+2]=$01)                   and(buffer[ptr+3]=$02));    //And check if they match    Result:=(ptr=temp)AND(Result);    //Next file    inc(index);   end;   //Check we got all the files   Result:=(index=numfiles)AND(Result);  end; end;end; //Merges two ZIP filesfunction CombineZIP(files: array of String; outputfile: String): Boolean;var input     : array[0..1] of TFileStream; output    : TFileStream; inbuffer  : array[0..1] of array of Byte; outbuffer : array of Byte; ptr       : Integer=0; cnt       : Integer=0; fileptr : Cardinal=0; CL        : array[0..1] of Integer; EoCL      : array[0..1] of Integer; temp      : Cardinal=0; numfiles  : Cardinal=0; CLsize    : Cardinal=0; filesize  : Cardinal=0;begin Result:=False; if Length(files)>=2 then //Only works with two files, the rest are ignored begin  Result:=True;  //Read in the files  for ptr:=0 to 1 do  begin   input[ptr]:=TFileStream.Create(files[ptr],fmOpenRead OR fmShareDenyNone);   input[ptr].Position:=0;   SetLength(inbuffer[ptr],input[ptr].Size);   input[ptr].Read(inbuffer[ptr][0],input[ptr].Size);   input[ptr].Free;   //Get the position of the central library for each   CL[ptr]:=FindCL(EoCL[ptr],inbuffer[ptr]);   Result:=(Result)AND(CL[ptr]<>-1)AND(EoCL[ptr]<>-1);   //Count the number of files stored   if EoCL[ptr]<>-1 then    inc(numfiles,inbuffe r[ptr][EoCL[ptr]+$A]+inbuffer[ptr][EoCL[ptr]+$B]<<8);  end;  //Create the output file  if Result then  begin   //This will be the eventual central library size   CLsize:=(EoCL[0]-CL[0])+(EoCL[1]-CL[1]);   //This will be the eventual file size   filesize:=CL[0]+CL[1]+CLsize+22;   SetLength(outbuffer,filesize);   //Write the files. The files from the second ZIP goes where the first CL was   fileptr:=0;   for cnt:=0 to 1 do   begin    for ptr:=0 to CL[cnt]-1 do outbuffer[fileptr+ptr]:=inbuffer[cnt][ptr];    inc(fileptr,CL[cnt]);   end;   //Write the CLs   for cnt:=0 to 1 do   begin    //We'll need to find each file entry and adjust by adding CL1 to the adddress    for ptr:=CL[cnt] to EoCL[cnt]-1 do    begin     outbuffer[fileptr-CL[cnt]+ptr]:=inbuffer[cnt][ptr];     if cnt>0 then      //Found a file?      if (inbuffer[cnt][ptr-$2E]=$50)      and(inbuffer[cnt][ptr-$2D]=$4B)      and(inbuffer[cnt][ptr-$2C]=$01)    and(inbuffer[cnt][ptr-$2B]=$02)then      begin       //Get the data offset       temp:=inbuffer[cnt][ptr-4]            +inbuffer[cnt][ptr-3]<<8            +inbuffer[cnt][ptr-2]<<16          +inbuffer[cnt][ptr-1]<<24;       //Adjust the data offset       inc(temp,CL[cnt-1]);       //Save back       outbuffer[(fileptr- CL[cnt]+ptr)-4]:= temp AND $000000FF;       outbuffer[(fileptr- CL[cnt]+ptr)-3]:=(temp AND $0000FF00)>>8;       outbuffer[(fileptr- CL[cnt]+ptr)-2]:=(temp AND $00FF0000)>>16;       outbuffer[(fileptr- CL[cnt]+ptr)-1]:=(temp AND $FF000000)>>24;      end;    end;    inc(fileptr,CL[cnt]);   end;   //Write the central directory   fileptr:=filesize-22;   outbuffer[fileptr    ]:=$50;   outbuffer[fileptr+$01]:=$4B;   outbuffer[fileptr+$02]:=$05;   outbuffer[fileptr+$03]:=$06;   outbuffer[fileptr+$08]:= numfiles AND $00FF;   outbuffer[fileptr+$09]:=(numfiles AND $FF00)>>8;   outbuffer[fileptr+$0A]:= numfiles AND $00FF;   outbuffer[fileptr+$0B]:=(numfiles AND $FF00)>>8;   outbuffer[fileptr+$0C]:= CLsize AND $000000FF;   outbuffer[fileptr+$0D]:=(CLsize AND $0000FF00)>>8;   outbuffer[fileptr+$0E]:=(CLsize AND $00FF0000)>>16;   outbuffer[fileptr+$0F]:=(CLsize AND $FF000000)>>24;   outbuffer[fileptr+$10]:= (CL[0]+CL[1]) AND $000000FF;   outbuffer[fileptr+$11]:=((CL[0]+CL[1]) AND $0000FF00)>>8;   outbuffer[fileptr+$12]:=((CL[0]+CL[1]) AND $00FF0000)>>16;   outbuffer[fileptr+$13]:=((CL[0]+CL[1]) AND $FF000000)>>24;   //Save the data to a file   output:=TFileStream.Create(outputfile,fmCreate OR fmShareDenyNone);   output.Position:=0;   output.Write(outbuffer[0],Length(outbuffer));   output.Free;  end; end;end; end.Note that the ValidateZIPFile method can take a long time if there are lots of files in the archive. ---------------------------------------------------------------------- geraldholdsworth · 2025-06-06 16:06 Slight error in the code. Line 171 (in the top post), or line 274 (in my last post) , which reads: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i inc(fileptr,CL[cnt]);works better if you change it to: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i inc(fileptr,EoCL[cnt]-CL[cnt]);I think... ---------------------------------------------------------------------- domasz · 2025-06-07 10:53 Quote from: geraldholdsworth on June 05, 2025, 03:50:21 pmGNU General Public Licence Thanks, but useless. ---------------------------------------------------------------------- jamie · 2025-06-07 13:29 I think I once did this by using an UnZip stream going to a Zip Stream to build the existing and then add to etc. I may look that up to see where I did this. Jamie