如何删除 MemoryStream 中的起始 header 并将其余部分保存到文件中?
How to remove the starting header in a MemoryStream and save the rest to a file?
我目前有一个 TMemoryStream
,它通过读取一个 varbinary 字段来填充,该字段最初是由来自 MS SQL 服务器数据库 table.[=14= 的 UTF8 标准编码的]
varbinary 字段包含一个 header 部分,在将流的其余部分保存为文件之前需要将其删除。
我已经成功提取了所需的header信息,其中包括文件名,但还没有
找到了将流的剩余部分保存为有效文件的解决方案。
数据最初被读入 TdxMemData
数据集。
procedure TBaseDataPump.SaveAttachments(mtblAttachmentInfo: TdxMemData);
var
AFile: TMemoryStream;
ANewFile: TStringStream;
sFullPath: String;
wsImageBlob, wsHeader, wsBody: WideString;
iposition: Integer;
begin
if mtblAttachmentInfo.Active then // TdxMemData filled with data
begin
with mtblAttachmentInfo do
begin
First;
while NOT EOF do
begin
// Below code works fine to save the file without removing the data
AFile := TMemoryStream.Create; // Create the memory stream
TBlobField(FieldByName('ImageBlob')).SaveToStream(AFile);
// load the file image from the data to the memory strem
// Get the header content
ANewFile := TStringStream.Create('');
TBlobField(FieldByName('ImageBlob')).SaveToStream(ANewFile);
// read the filed again to a TStringStream
wsImageBlob := UTF8ToString(ANewFile.DataString);
// I can see the data as wide string and can find the header bit and its ending position on the
// its ending position on the string
iposition := AnsiPos(#13#13#10, wsImageBlob); // find the header ending position
// getting the header to another widestring
wsHeader := AnsiLeftStr(wsImageBlob,position);
wsHeader := AnsiLeftStr(wsImageBlob,POS(#9,wsHeader)-1);
wsHeader := StringReplace(wsHeader,'''','',[rfReplaceall]);
// need to save the stream from the position of the end of header as file
end;
end;
end;
end;
用copyfrom方法找到了解决方案。它如下:
// Create the TStringStream
ANewFile := TStringStream.Create('');
try
TBlobField(FieldByName('ImageBlob')).SaveToStream(ANewFile);
// get the blob as a string so we can extract the header information
wsImageBlob := UTF8ToString(ANewFile.DataString);
position := AnsiPos(#13#13#10, wsImageBlob);
if position <> 0 then begin
// have header information to extract
position := position + 2;
wsHeader := AnsiLeftStr(wsImageBlob, position);
wsHeader := AnsiLeftStr(wsImageBlob, POS(#9, wsHeader) - 1);
wsHeader := StringReplace(wsHeader, '''', '', [rfReplaceall]);
sFilePath := sDirectorypath + wsHeader;
ANewFile.Position := position;
AFile := TMemoryStream.Create;
try
AFile.CopyFrom(ANewFile, ANewFile.Size - position);
AFile.SaveToFile(sFilePath);
finally
FreeAndNil(AFile);
end;
end
finally
FreeAndNil(ANewFile);
end;
我目前有一个 TMemoryStream
,它通过读取一个 varbinary 字段来填充,该字段最初是由来自 MS SQL 服务器数据库 table.[=14= 的 UTF8 标准编码的]
varbinary 字段包含一个 header 部分,在将流的其余部分保存为文件之前需要将其删除。
我已经成功提取了所需的header信息,其中包括文件名,但还没有 找到了将流的剩余部分保存为有效文件的解决方案。
数据最初被读入 TdxMemData
数据集。
procedure TBaseDataPump.SaveAttachments(mtblAttachmentInfo: TdxMemData);
var
AFile: TMemoryStream;
ANewFile: TStringStream;
sFullPath: String;
wsImageBlob, wsHeader, wsBody: WideString;
iposition: Integer;
begin
if mtblAttachmentInfo.Active then // TdxMemData filled with data
begin
with mtblAttachmentInfo do
begin
First;
while NOT EOF do
begin
// Below code works fine to save the file without removing the data
AFile := TMemoryStream.Create; // Create the memory stream
TBlobField(FieldByName('ImageBlob')).SaveToStream(AFile);
// load the file image from the data to the memory strem
// Get the header content
ANewFile := TStringStream.Create('');
TBlobField(FieldByName('ImageBlob')).SaveToStream(ANewFile);
// read the filed again to a TStringStream
wsImageBlob := UTF8ToString(ANewFile.DataString);
// I can see the data as wide string and can find the header bit and its ending position on the
// its ending position on the string
iposition := AnsiPos(#13#13#10, wsImageBlob); // find the header ending position
// getting the header to another widestring
wsHeader := AnsiLeftStr(wsImageBlob,position);
wsHeader := AnsiLeftStr(wsImageBlob,POS(#9,wsHeader)-1);
wsHeader := StringReplace(wsHeader,'''','',[rfReplaceall]);
// need to save the stream from the position of the end of header as file
end;
end;
end;
end;
用copyfrom方法找到了解决方案。它如下:
// Create the TStringStream
ANewFile := TStringStream.Create('');
try
TBlobField(FieldByName('ImageBlob')).SaveToStream(ANewFile);
// get the blob as a string so we can extract the header information
wsImageBlob := UTF8ToString(ANewFile.DataString);
position := AnsiPos(#13#13#10, wsImageBlob);
if position <> 0 then begin
// have header information to extract
position := position + 2;
wsHeader := AnsiLeftStr(wsImageBlob, position);
wsHeader := AnsiLeftStr(wsImageBlob, POS(#9, wsHeader) - 1);
wsHeader := StringReplace(wsHeader, '''', '', [rfReplaceall]);
sFilePath := sDirectorypath + wsHeader;
ANewFile.Position := position;
AFile := TMemoryStream.Create;
try
AFile.CopyFrom(ANewFile, ANewFile.Size - position);
AFile.SaveToFile(sFilePath);
finally
FreeAndNil(AFile);
end;
end
finally
FreeAndNil(ANewFile);
end;