Delphi ZipMaster:如何从流创建zip文件并将其作为流保存

时间:2019-03-11 16:12:08

标签: delphi zip zipstream

专家

我有以下问题。我试图找到一种将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;

0 个答案:

没有答案