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.