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;