Recent

Author Topic: zipper.pas empty zipped up memorystream ?  (Read 4030 times)

Mike J

  • Jr. Member
  • **
  • Posts: 54
  • Computer Programmer/www.NewsRx.com
    • ᏣᎳᎩ ᎦᏬᏂᎯᏍᏗ ᏗᏕᎶᏆᏍᏗ (Cherokee Language Lessons)
zipper.pas empty zipped up memorystream ?
« on: December 16, 2011, 09:51:44 pm »
I was trying to zip up Tmemorystreams, instead of saving the contents out as temp files then zipping the contents up, the resulting zip (on inspection), shows the file added with the correct size in bytes being reported, but extracts as *empty* (zero bytes) ?

Code fragment

Code: Pascal  [Select][+][-]
  1. zipFile.Filename:='test.zip';
  2. tmpMemFile:=mimetypeWrite();
  3. zfe:=zipFile.Entries.AddFileEntry(tmpMemFile, 'mimetype');
  4. zipFile.ZipAllFiles;                                      
  5.  

Code: Pascal  [Select][+][-]
  1. function TePub.mimetypeWrite():TMemoryStream;
  2.   var
  3.     meta: TStringList;
  4.     memFile: TMemoryStream;
  5.   begin
  6.     memFile:=TMemoryStream.Create;
  7.     meta := TStringList.Create;
  8.     meta.Add('application/epub+zip');
  9.     meta.SaveToStream(memFile);
  10.     meta.Free;
  11.     Result:=memFile;
  12.   end;            
  13.  
ᏙᎯ

Laksen

  • Hero Member
  • *****
  • Posts: 652
    • J-Software
Re: zipper.pas empty zipped up memorystream ?
« Reply #1 on: December 17, 2011, 12:40:24 am »
Remember to do a memFile.Seek(0, soFromBeginning); after writing to it. Otherwise you'll be reading from the end of the memory stream

Mike J

  • Jr. Member
  • **
  • Posts: 54
  • Computer Programmer/www.NewsRx.com
    • ᏣᎳᎩ ᎦᏬᏂᎯᏍᏗ ᏗᏕᎶᏆᏍᏗ (Cherokee Language Lessons)
Re: zipper.pas empty zipped up memorystream ?
« Reply #2 on: December 17, 2011, 01:15:45 am »
Nothing there to remember, didn't know that.

Guess the question is... why wouldn't the zip class do that automatically?
ᏙᎯ

Laksen

  • Hero Member
  • *****
  • Posts: 652
    • J-Software
Re: zipper.pas empty zipped up memorystream ?
« Reply #3 on: December 17, 2011, 02:21:15 am »
It's just writing some bunch to a stream, so should it rewind to the start of the memory stream or to where it started to write?

It's a well known paradigm, where you can then very easily store something after other things. And mostly this is an "issue" with memory streams, so people most often just have to learn the hard way :)

Mike J

  • Jr. Member
  • **
  • Posts: 54
  • Computer Programmer/www.NewsRx.com
    • ᏣᎳᎩ ᎦᏬᏂᎯᏍᏗ ᏗᏕᎶᏆᏍᏗ (Cherokee Language Lessons)
Re: zipper.pas empty zipped up memorystream ?
« Reply #4 on: December 17, 2011, 02:45:22 am »
It's just writing some bunch to a stream, so should it rewind to the start of the memory stream or to where it started to write?

It's a well known paradigm, where you can then very easily store something after other things. And mostly this is an "issue" with memory streams, so people most often just have to learn the hard way :)

hrmmm, something to keep in mind for sure.

ᏩᏙ for your helping me
ᏙᎯ

 

TinyPortal © 2005-2018