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.
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, 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.
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.