?
Solved

Generate gzipped Stream of Memorystream

Posted on 2003-03-31
5
Medium Priority
?
826 Views
Last Modified: 2010-04-16
Short:
I'd like to generate a stream (memorystream) containing a gzipped file of a cleartext stream (memorystream, too).

What I did:
I have been using Delphizlib http://gewis.win.tue.nl/info/delphizlib/ for files, but it seems that this is unable to work with streams.

What I want:
I am generating a stream that contains a file (like from memorystream.loadfromfile) and want to compress it. The resulting stream should contain the original file in gzipped form, that I could open as file.gz if I did memorystream.savetofile. However I DON'T want to save it to a file, because I can't guarantee that I have the right to write to any Disk.

Please help.
0
Comment
Question by:Spida_81
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
emadat earned 240 total points
ID: 8239200
Using Delphi's ZLIB library; These functions will help you compress and decompress data contained in Streams


//*******************************************************************
procedure DecompressBuffer(const pFrom: Pointer; const piFromSize: Integer; out pTo: Pointer; out piToSize: Integer);
begin
     ZLib.DecompressBuf(pFrom, piFromSize, piFromSize*2, pTo, piToSize);
end;
//*******************************************************************
function CompressStream(pFrom, pTo: TMemoryStream): real;
var
     cs: TCompressionStream;
begin
    pTo.Clear;
     cs := TCompressionStream.Create(clDefault, pTo);
     pFrom.SaveToStream(cs);
     cs.Free;
     if pTo.Size <> 0
          then result := pTo.Size / pFrom.Size * 100
          else result := 0;
end;
//*******************************************************************
procedure DecompressStream(pFrom, pTo: TMemoryStream);
var
     liToSize : integer ;
     lBufTo   : Pointer ;
begin
     pTo.Size := 0;
     try
          if pFrom.Size = 0 then Exit; //==>
          pFrom.Position := 0;
          try
               DecompressBuffer(PFrom.Memory, PFrom.Size, lBufTo, liToSize);
               pTo.WriteBuffer(lBufTo^, liToSize);
          finally
               FreeMem(lBufTo);
          end;
          pFrom.Position := 0;
          pTo.Position := 0;
     except
          on e:exception do
               raise exception.Create( 'Error in DeCompressStream. Message: ' + e.message ) ;
     end;
end;
//*******************************************************************
0
 
LVL 1

Author Comment

by:Spida_81
ID: 8243809
Where do I find this library and what do I have to put in my uses to use it?
Does it conflict with delphizlib?
Is it possible to do this with delphizlib, too?
0
 
LVL 3

Expert Comment

by:emadat
ID: 8246049
My code uses Delphi's ZLib.
If you are using delphi 6 or 7 then you should be having it
I'm not sure whether this is what you need or not.
Please try the code and let me know.
0
 
LVL 1

Author Comment

by:Spida_81
ID: 8252178
If I can find the library (where?) and the output of CompressStream() is, when saved to a file with tostream.savetofile, readable with winzip, then it is what I need.
0
 

Expert Comment

by:CleanupPing
ID: 9316532
Spida_81:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question