专家们,
我有以下问题。我试图找到一种将 zip 文件创建为流的解决方案,但 zip 文件不应物理存储,而应仅作为流存在。
我已尝试使用以下代码示例,但它不起作用。如果我这样做,则会出现一条错误消息:
存档已损坏或不包含文件。
我使用的 zip 库是 ZipMaster - DelphiZip ( http://www.delphizip.org/ )。
function StreamToZipStream( const InStream: TStream; out OutStream: TStream). Boolean;
var
ZipMaster1 : TZipMaster;
begin
//wird nie benutzt: result := 0;
ZipMaster1 := TZipMaster.create(nil);
try
ZipMaster1.DLLDirectory := GetModuleDir;
ZipMaster1.AddCompLevel := 9; // highest compression
ZipMaster1.AddOptions := [AddHiddenFiles];
InStream.Position := 0;
ZipM.AddStreamToStream(TMemoryStream(InStream));
//after add the inStream to the ZipMaster1.ZipStream
// save it to the OutStream ?
ZipMaster.ZipStream.SaveToStream(OutStream);
result := ZipMaster1.ErrCode = 0;
finally
ZipMaster1.free;
end;
end;
...
InStream := TMemoryStream.Create;
OutStream := TMemoryStream.Create;
try
//load a file to MemoryStream
InStream.LoadFromFile('File.txt');
//ZipMaster operation to create a zip stream, that I can save as .zip from this stream
StreamToZipStream(aInStream, aOutStream ) ;
// I get a file 'File.zip' with the right file size, but I can not handle this file.
// an error message "the archive is corrupted" poped up if I try to do some operations with.
OutStream.SaveToFile('File.zip');
finally
InStream.Free;
OutStream.Free;
end;