Trying to navigate the use of the TZipFile object - creating a new Zip file

correct no exception message but that’s not available to Delphi

Sorry? What’s not available?

1 Like

You have access to the source code for System.zip. Put break points in isValid & LocateEndOfCentralHeader and see what happens with a blank file of zero size. Maybe the logic fails for blank files.
If you aren’t familiar with how to debug code, let us know. This allows you to watch the variables and function results and determine why the outcome isn’t as you expect.

Ok
I have a look at the TZipFile object and see if I can make my own override or new Instruction to isValid some how but not at this moment - as it will take a while to figure out.

Thanks for the understanding

I’ve just tested .isValid, creation of a TZipFile using zmReadWrite zmWrite, with a non-existent file and a zero size file and all functions work 100% as they should.
Maybe you want code that looks something like this

procedure TForm1.Button1Click(Sender: TObject);
begin
  OpenDia.Filter := 'LexBase *.LB| *.LB';
  OpenDia.DefaultExt := 'LexBase *.LB| *.LB';
  OpenDia.FilterIndex := 1;
  Strm.SetSize(0);
  If OpenDia.Execute then
  begin
    If FileExists(OpenDia.FileName) then
      Zip.Open(OpenDia.FileName, zmReadWrite)
    else
      Zip.Open(OpenDia.FileName, zmWrite);
    RadioBut.Checked := TZipFile.IsValid(OpenDia.FileName);
  end;
end;
1 Like

been thinking more to remove

RadioBut.Checked := TZipFile.IsValid(OpenDia.FileName);

and use below as my check

RadioBut.Checked := FileExists(OpenDia.FileName)

because the zip code is stable and well tested - so if the file is created is a better thinking to know its working i think

The point is nothing put in the zip file means a whole lot of user input is wasted as there is no check in my code as when I put a stream in the zip file I could measure file size is over zero afterwards.

Hi,

This is sort of off topic.

If you are only adding files to the zip, it is likely fine.
But if you are using the zip like a filesystem ie. adding, removing, changing files.
You could perhaps consider using a sqlite database to store the files in.

1 Like

The project I’m doing is a type of registry targeted for application uses that contains many stream types (types of streams are named for their use) for Linux and Windows applications and other data. It nearly finished now front end is now basically running - I just need to store the data to file and debug a bit. - I have built it around a C++ DLL access standard so dependency of the windows registry is gone and windows/Linux cross platform is easier for developers.
I see what you are saying if the database records were able to be put in an application for application use yes I could do that.

I wrote the stuff below and realized TZipFile must be demanding exclusive access to the file - so my TFileStream cannot read a file size when shearing the file
this code has done a full check of access availability. and I notice the file jumps up 160 bytes so it works.

var
Form1: TForm1;
Zip: TZipFile;
FStrm: TFileStream;
ZipFileName: String;
ZipFileAccessed: Boolean;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Zip := TZipFile.Create;
end;

procedure TForm1.Button1Click(Sender: TObject);
Var Size: Int64;
 MStrm: TMemoryStream;
Const
  Str: ShortString = '458wefbnw4tf7898df723mwef9034b3t6';
begin
OpenDia.Filter := 'LexBase *.LB| *.LB';
OpenDia.DefaultExt := 'LexBase *.LB| *.LB';
OpenDia.FilterIndex := 1;
ZipFileAccessed := False;
If OpenDia.Execute then
 begin
  ZipFileName := OpenDia.FileName;
  FStrm := TFileStream.Create(ZipFileName, fmOpenReadWrite);
  Size := FStrm.Size; // get file size
  FStrm.Free;
  MStrm := TMemoryStream.Create;
  MStrm.Read(Size, 8);  // make data to save
  Zip.Open(ZipFileName, zmReadWrite);
  Zip.Add(MStrm, Str, zcStored); // store data to file
  MStrm.Free;
  Zip.Free;
  FStrm := TFileStream.Create(ZipFileName, fmOpenReadWrite);
  If Size <> FStrm.Size then ZipFileAccessed := True;
  FStrm.Free; // check if file size changed
   If ZipFileAccessed then else
    begin
      MessageDlg('Was not able to access your ''LB''File',
                    mtConfirmation, [mbOk], 0);
      Application.Destroy;
    end;
  Zip.Open(ZipFileName, zmReadWrite);
  Zip.Delete(Str); // did get file access
 end;
end;