Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Generate gzipped Stream of Memorystream

Posted on 2003-03-31
5
Medium Priority
?
838 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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…

564 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