Trying to navigate reading a TZipFile No 2

Saving files to a file system is so time consuming way if doing things
the delay of rewriting files becomes excessive on the file system and the user becomes waiting on the system over what is a simple task.
I have up to about 100 different File names - some files are short and some are long
If one file is lost or renamed so can become 20 get unusable.

Something you are seeing as complicated but you are not explaining

So I have many files each with clearly different names
no compression
no folders
different sizes
need to be updated regularly with the same name and is likely to be of another size
the data is loaded and saved to a TMemoryStream
nothing else

I have done it, there is no waiting at all. Your files are small, so it will be instantaneous.

Speaking from experience, you will be far better served to simplify each step and do that than try to do something youā€™ve never done before and complete the entire thing in one step.

The benefit of doing it so simply, is that each step becomes very very small, and you can then ask for help with a tiny little step. The solution to the issue you have will also be tiny, and quickly implementable.

If you persist in trying to do the entire application in one step, the problems you have will continue to be complex and arduous, and trying to help you with them will be correspondingly complex and arduous.

KISS principle every time for me. That is all I am suggesting for you.

I still have not been explained in Delphi terms what is a TBytes!!!
its not a PString its not a string and its not a byte

So how does some one put TMemoryStream data in a Tbytes and the data size is understood

this is simple or totally complicated?

The other reason I am suggesting you write the files to the filesystem is because when you generate your files, down the track, I can guarantee you will want to look at their contents.

If you have already done the simplest method of file generation - saving a stream to file - you will be able to load the resulting saved file in a proper editor vs trying to debug it in Delphi directly.

Look how simple my requirements are but I cannot use TBytes
and because their is not helps TZipFIle cannot save a single file into a Zip file

I still have not been explained in Delphi terms what is a TBytes!!!
its not a PString its not a string and its not a byte

So how does some one put TMemoryStream data in a Tbytes and the data size is understood

this is simple or totally complicated?

you are talking from a user perspective to do something
Iā€™m not working for the user to do it manually of what needs to have done for him automatically - that is not how it works

This is the point Iā€™m fighting with you which is so irrelevant
Why have the computer in the first place is your argument
Lets drive cars the Fred Flintstone way and computers too put the feet on the ground to push them as well as your self

I still have not been explained in Delphi terms what is a TBytes!!!
its not a PString its not a string and its not a byte

So how does some one put TMemoryStream data in a Tbytes and the data size is understood

this is simple or totally complicated?

No, Iā€™m not.

Neither am I.

You generate the file, then save it to the file system, then load it from the file system and add it to your zip file.

No TBytes required.
Stream.SaveToFIle (pathtofile)
Zipfile.Add(pathtofile)

Done.

Very simple for the programmer to implement, nothing to do with the user at all.

No Flintstones were invoked or harmed in this suggestion.

1 Like

my project looks flintstone to the user
If Delphi has a a TBtyes installed as a type why is their no documentation at all!!!
where else am I going to get TBytes in Delphi

So what is a TBytes

Zip Files arenā€™t designed for what you want to do.

Here is an example using a database, which is fit for purpose. Youā€™ll have to write some code yourself to check for an existing ā€˜fileā€™ under the name column and ā€˜editā€™, but apart from that the code reads/writes TMemoryStream as you requested into a single file thatā€™s fit for purpose.

uses
FireDAC.Comp.Client, FireDAC.Stan.Def, FireDAC.Stan.Async, FireDAC.Phys.SQLite, FireDAC.DApt, Data.DB;

procedure TForm1.createTableClick(Sender: TObject);
var
Connection: TFDConnection;
Query: TFDQuery;
begin
Connection := TFDConnection.Create(nil);
Query := TFDQuery.Create(nil);
try
Connection.DriverName := ā€˜SQLiteā€™;
Connection.Params.Add(ā€˜Database=mydatabase.dbā€™);
Connection.LoginPrompt := False;
Connection.Connected := True;

Query.Connection := Connection;
Query.SQL.Text := 'CREATE TABLE IF NOT EXISTS MyTable (' +
                  'Name TEXT NOT NULL, ' +
                  'Content BLOB NOT NULL)';
Query.ExecSQL;

finally
Query.Free;
Connection.Free;
end;
end;

procedure TForm1.addDataClick(Sender: TObject);
var
data: TStringStream;
ms: TMemoryStream;
Connection: TFDConnection;
Query: TFDQuery;
begin

data := TStringStream.Create(ā€˜Hello Worldā€™);
ms := TMemoryStream.Create;
ms.copyFrom(data,data.Size);

Connection := TFDConnection.Create(nil);
Query := TFDQuery.Create(nil);
try
Connection.DriverName := ā€˜SQLiteā€™;
Connection.Params.Add(ā€˜Database=mydatabase.dbā€™);
Connection.LoginPrompt := False;
Connection.Connected := True;

Query.Connection := Connection;
Query.SQL.Text := 'insert into MyTable (Name,Content) Values(:Name,:Content)';
Query.ParamByName('Name').asString := 'Test File';
Query.ParamByName('Content').LoadFromStream(ms,ftBlob);
Query.ExecSQL;

finally
ms.Free;
Query.Free;
Connection.Free;
end;
end;

procedure TForm1.getDataClick(Sender: TObject);
var
ms: TMemoryStream;
Connection: TFDConnection;
Query: TFDQuery;
begin

ms := TMemoryStream.Create;

Connection := TFDConnection.Create(nil);
Query := TFDQuery.Create(nil);
try
Connection.DriverName := ā€˜SQLiteā€™;
Connection.Params.Add(ā€˜Database=mydatabase.dbā€™);
Connection.LoginPrompt := False;
Connection.Connected := True;

Query.Connection := Connection;
Query.SQL.Text := 'select Content from MyTable where Name = :Name';
Query.ParamByName('Name').asString := 'Test File';
Query.open;
if not query.eof then
begin
  (Query.fieldByName('Content') as TBlobField).SaveToStream(ms);
  //ShowMessage( Query.fieldByName('Content').AsString );
end;

finally
ms.Free;
Query.Free;
Connection.Free;
end;
end;

https://docwiki.embarcadero.com/Libraries/Alexandria/e/index.php?search=TBytes&title=Special%3ASearch&go=Go

https://docwiki.embarcadero.com/Libraries/Alexandria/en/System.SysUtils.TBytes

Finally
So its a Dynamic array
So I SetLength(TBytes)
And I can CopyMemory(TBytes[1], TMemoryStream.Memory, TMemoryStream.Size);
done
good start

I canā€™t believe how hard it seems to be to type in ā€œTBytesā€ in the IDE editor and press F1.

2 Likes

PEBKAC

Alex

  • I canā€™t believe how hard it seems to be to type in ā€œTBytesā€ in the IDE editor and press F1.