Use of stream in Delphi (7) compression and decompression (tcompressionstream, tdecompressionstream)

Source: Internet
Author: User
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; procedure button1click (Sender: tobject); Procedure button2click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} uses zlib; {compressed stream tcompressionstream and decompressed stream tdecompressionstream from zlib unit} // compressed procedure tform1.button1click (Sender: tobject); var Cs: tcompressionstream; {defining compressed stream} FS, MS: tmemorystream; {FS is the stream to be compressed; MS is the stream that receives the compressed file} num: integer; {Original file size} begin {Step 1: call the file to be compressed and get the size} FS: = tmemorystream. create; FS. loadfromfile ('C: \ temp \ test.txt '); {the file must exist.} num: = FS. size; {Step 2: Create the received stream and write the original file size first} MS: = tmemorystream. create; Ms. write (Num, sizeof (Num); {Step 3: compression} Cs: = tcompressionstream. create (CLmax, MS); {parameter 1 is the compression ratio; parameter 2 is the receiving stream} FS. savetostream (CS); {input data to be compressed} CS. free; {compression stream free before compression is completed, so free} {Step 4: Save} ms in advance. savetofile ('C: \ temp \ test.zip x'); {Step 5: Release} ms. free; FS. free; {compression ratio parameter: clnone no compression clfastest fast cldefault default CLmax Max ratio} end; // extract procedure tform1.button2click (Sender: tobject); var DS: tdecompressionstream; {decompress stream} FS, Ms: tmemorystream; {FS is the stream to be decompressed; The MS is the stream that receives the decompressed data} num: integer; {size before File compression} begin {Step 1: files to be decompressed} FS: = tmemorystream. create; FS. loadfromfile ('C: \ temp \ test.zip x'); {the file generated by the previous compression method must be used} {Step 2: Read the file size before compression} FS. position: = 0; FS. readbuffer (Num, sizeof (Num); {Step 3: Prepare the stream to be received and set the desired size} MS: = tmemorystream. create; Ms. setsize (Num); {Step 4: Decompress} DS: = tdecompressionstream. create (FS); {the parameter is the stream to be decompressed} {Step 5: Read the extracted data and store it to the prepared stream} Ds. read (Ms. memory ^, num); {Step 6: Save} ms. savetofile ('C: \ temp \ test2.txt '); DS. free; Ms. free; FS. 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.