Delphi ZipMaster: как создать zip-файл из потока и сохранить его в виде потока - PullRequest
0 голосов
/ 11 марта 2019

Эксперты

У меня следующая проблема. Я пытаюсь найти решение для создания 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;
...