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

I’m not looking for compression, just the storage of many small files in the Zip file
1/ I need to 1st be able to create my file ‘*.LB’ if its not already created
2/ Zip.IsValid(OpenDia.FileName); is to test if its able to use the file
3/ then to put my files in the Zip file and extract them
4/ close and tidy things up
No 2 Zip.IsValid(OpenDia.FileName); is not liked so have I gone about things in a wrong way?

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
     else Strm.SaveToFile(OpenDia.FileName);
  Zip.Open(OpenDia.FileName, zmReadWrite);
  RadioBut.Checked := Zip.IsValid(OpenDia.FileName);
  end;
end;

what I’m reading - System.Zip or TZipFile does not support the creation of a new zip file

I can create a Zip file from windows explorer
I but can create a Zip file from Linux explorer

So can I create a Zip file copy what contents that is in the Zip file
Create a new file and put the contents in the file, the open the file?
is their a better way?

Sorry, I’m not really understanding your question. If System.Zip does not meet your requirement you might want to consider the Abbrevia by TurboPack toolkit. You can obtain that via GetIt Package manager.

I haven’t done it in a while, but I thought that using the Open method with zmWrite as the OpenMode, then Add to add the files, then Close would create it if it didn’t already exist.

Is that not working?

I will try replacing

 Zip.Open(OpenDia.FileName, zmReadWrite);

for

 Zip.Open(OpenDia.FileName, zmWrite);

that is what you are saying
I will give it a try
but my logic is this is about the data in the file? not creating a file

No, that’s not what I’m saying. Can you show the more complete code? Including where you are creating and destroying your TZipfile instance and also your Stream?

yes sure

  Zip: TZipFile;
  Strm: TMemoryStream;

implementation

{$R *.dfm}

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

and in my uses I have System.Zip,

OK, so it seems you’re not using TZipFile to create the zip. In this code you’re only using it to validate the zip. Is that right?

So is there somewhere where Strm gets loaded with a zipfile?

Also, the Strm.Setfile(0) just before doing SaveToFile seems a bit odd. My guess would be that would save an zero byte file to disk.

I use the Strm.size(0) to open an empty file correct - because Zip I cannot use to do that

Hi Lex,

Yes, you can create a new zip file using Zip.Open(OpenDia.FileName, zmWrite). If the file exists then the contents will be replaced with whatever you add.

There is no need to call IsValid if you are creating a new zip file. You would use it to check if an existing zip file on disk is valid, before adding additional files using Open(..., zmReadWrite) for example.

I don’t understand your original requirement 3/ to add files to the zip file and then extract them. Why use a zip file at all? If all you want to do is create a bunch of separate files filled with some content from a stream for example, then just use TFileStream (or TBufferedFileStream), not TZipFile.

Cheers,
Jarrod

an empty file, sure. But not a zip file.

If you’re creating it from scratch, I would have thought you’d do a sequence like:

Zip.Open(
Zip.Add(
Zip.Add(

Zip.Close

Can you show this doesn’t work first. I highly recommend you pay $20-30/month for chat gpt it will help you with so many of your recent questions.

uses System.Zip;
procedure CreateZipWithFiles; 
var Zip: TZipFile; 
begin 
  Zip := TZipFile.Create; 
  try 
  Zip.Open('C:\Path\To\YourArchive.zip', zmWrite); 
  Zip.Add('C:\Path\To\File1.txt'); 
  Zip.Add('C:\Path\To\File2.jpg'); // Add more files as needed finally 
  Zip.Free; 
  end;
end;
1 Like

Yes

Zip.Open(OpenDia.FileName, zmWrite);

does make a file
but as an empty file IsValid says it an error!!!
so I don’t know if it’s working or ready to operate ?

Where did you call Zip.Close though? Note in the docs for Zip.Close it says:

Closes this TZipFile, writing additional metadata that is required for reading the file.

Closing is required to write the TZipFile’s Central Directory to disk.

If Zip.Open(..., zmWrite) does not raise an exception then the zip file is ready to add content to. Remove the IsValid.

1 Like

I’m only tracing code at the moment
I will put a form destroy to do that
Do I need to use a try statement in load a stream with data and then use the IsValid to know its working?

As Jarrod said, you don’t need IsValid in your scenario. Just get rid of it and follow @hsvandrew’s code sample (Freeing the TZipFile calls close)

You might use TZipFile.IsValid (note it’s a class method) if you wanted to check say a zip file on disk was valid without creating an instance and calling Open.

so how do I know its set up to insert and read my streams/files?

Because you didn’t get an exception when you called Open.