Jump to content
Stéphane Wierzbicki

How can I delete an archive Item using TZipFile class

Recommended Posts



Anyone knows how I can delete an archive Item using TZipFile class ? 

I found a TZipFileHelper class helper on the net. It is useless since we cannot access TZipFile private members anymore.


procedure TZipFileHelper.Delete(FileName: string);
  i, j: Integer;
  StartOffset, EndOffset, Size: UInt32;
  Header: TZipHeader;
  Buf: TBytes;
  i := IndexOf(FileName);
  if i <> -1 then begin
    // Find extents for existing file in the file stream
    StartOffset := Self.FFiles[i].LocalHeaderOffset;
    EndOffset := Self.FEndFileData;
    for j := 0 to Self.FFiles.Count - 1 do begin
      if (Self.FFiles[j].LocalHeaderOffset > StartOffset) and
         (Self.FFiles[j].LocalHeaderOffset <= EndOffset) then
        EndOffset := Self.FFiles[j].LocalHeaderOffset;
    Size := EndOffset - StartOffset;
    // Update central directory header data
    for j := 0 to Self.FFiles.Count - 1 do begin
      Header := Self.FFiles[j];
      if Header.LocalHeaderOffset > StartOffset then begin
        Header.LocalHeaderOffset := Header.LocalHeaderOffset - Size;
        Self.FFiles[j] := Header;
    // Remove existing file stream
    SetLength(Buf, Self.FEndFileData - EndOffset);
    Self.FStream.Position := EndOffset;
    if Length(Buf) > 0 then
      Self.FStream.Read(Buf[0], Length(Buf));
    Self.FStream.Size := StartOffset;
    if Length(Buf) > 0 then
      Self.FStream.Write(Buf[0], Length(Buf));
    Self.FEndFileData := Self.FStream.Position;



ps: I'm using Delphi RIO

Edited by Stéphane Wierzbicki

Share this post

Link to post
5 hours ago, Stéphane Wierzbicki said:

access TZipFile private members

WITH is your friend:

// from https://stackoverflow.com
procedure TSomeClassHelper.CheckAccessToPrivate;
 With Self do begin // access via with works
   FInt :=1;
// Declared in another unit as:
type TSomeClass = class
      FInt : integer;
      procedure SomeMethod;


Share this post

Link to post
On 8/31/2019 at 10:37 AM, Stéphane Wierzbicki said:

That is clearly a bug ! I'm surprised EMB did not fixed it!.

They are aware of it. Just as they have already fixed other access issues related to helpers, it is likely only a matter of time before they do fix this one too. 

Edited by Remy Lebeau

Share this post

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now