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;