Delphi uses zlib to compress files

Source: Internet
Author: User
Zlib. PAS and zlibconst. the PAS files are stored in the infoextraszlib directory on the installation disc of Delphi 5.0. In addition, zlib is saved in the infoextraszlibobj directory. copy the OBJ file referenced by the PAS unit to the lib directory of Delphi. You can modify the directory compression and file splitting by adding a description structure before the file stream. Use zlib units. The two functions are as follows: Procedure compressit (VAR compressedstream: tmemorystream; const compressionlevel: tcompressionlevel );
// Parameters are transmitted streams and compression methods
VaR
Sourcestream: tcompressionstream;
Deststream: tmemorystream;
Count: int64; // note: the value is changed here. It turns out to be Int.
Begin
// Obtain the original size of the stream
Count: = compressedstream. size;
Deststream: = tmemorystream. Create;
Sourcestream: = tcompressionstream. Create (compressionlevel, deststream );
Try
// The original stream is stored in sourcestream
Compressedstream. savetostream (sourcestream );
// Compress the original stream, and save the compressed stream in deststream.
Sourcestream. Free;
Compressedstream. Clear;
// Write the size of the original image
Compressedstream. writebuffer (count, sizeof (count ));
// Write the compressed stream
Compressedstream. copyfrom (deststream, 0 );
Finally
Deststream. Free;
End;
End;

procedure uncompressit (const compressedstream: tmemorystream; var uncompressedstream: tmemorystream);
// The compressed stream with parameters, decompressed stream
var
sourcestream: tdecompressionstream;
deststream: tmemorystream;
Buffer: pchar;
count: int64;
begin
// read the original size from the compressed image stream
compressedstream. readbuffer (count, sizeof (count);
// allocate memory blocks for the original stream to be read Based on the Size
getmem (buffer, count );
deststream: = tmemorystream. create;
sourcestream: = tdecompressionstream. create (compressedstream);
try
// decompress the compressed stream and store it in the buffer memory block
sourcestream. readbuffer (buffer ^, count);
// Save the original stream to the deststream stream
deststream. writebuffer (buffer ^, count);
deststream. position: = 0; // reset the stream pointer
deststream. position: = length (ver_info);
// load the image stream from the deststream stream
uncompressedstream. loadfromstream (deststream);
finally
freemem (buffer);
deststream. free;
end;

example:
procedure tform1.button5click (Sender: tobject);
// compress the specified file and save it as another compressed package,
// when I use it, I change the suffix to cab. Can I accept some people?
var
SM: tmemorystream;
begin
If opendialog1.execute then
begin
If savedialog1.execute then
begin
SM: = tmemorystream. create;
try
SM. loadfromfile (opendialog1.filename);
SM. position: = 0;
compressit (SM, cldefault);
SM. savetofile (savedialog1.filename);
finally
SM. free;
end;

procedure tform1.bitbtn2click (Sender: tobject);
// decompress the specified package into the original file.
var
SM, DM: tmemorystream;
begin
If opendialog1.execute then
begin
If savedialog1.execute then
begin
SM: = tmemorystream. create;
DM: = tmemorystream. create;
try
SM. loadfromfile (opendialog1.filename);
SM. position: = 0;
uncompressit (SM, DM);
DM. position: = 0;
DM. savetofile (savedialog1.filename);
finally
SM. free;
DM. free;
end;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.