ZLibCompressDecompress (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows the usage of TZCompressionStream and TZDecompressionStream. Supply the two filenames in the Edit controls of the main form and then press either the Compress or the Decompress button to do the actions. When the Compress button is pressed, the extension '.zip' is automatically added to the output file. When the Decompress button is pressed, '.zip' is automatically removed.

Code

void __fastcall TForm1::btnCompressClick(TObject *Sender)
{
   /* Create the Input, Output and Compressed streams. */
   TFileStream *input = new TFileStream(Edit1->Text, fmOpenRead);
   TFileStream *output = new TFileStream(Edit2->Text + ".zip", fmCreate);
   TZCompressionStream *zip = new TZCompressionStream(output, zcDefault);

   /* Compress data. */
   zip->CopyFrom(input, input->Size);

   /* Free the streams. */
   zip->Free();
   input->Free();
   output->Free();
}

void __fastcall TForm1::btnDecompressClick(TObject *Sender)
{
   /* Create the Input, Output, and Decompressed streams. */
   TFileStream *input = new TFileStream(Edit1->Text, fmOpenRead);
   TFileStream *output = new TFileStream(ChangeFileExt(Edit1->Text, ""), fmCreate);
   TZDecompressionStream *unzip = new TZDecompressionStream(input);

   /* Decompress data. */
   output->CopyFrom(unzip, 0);

   /* Free the streams. */
   unzip->Free();
   input->Free();
   output->Free();
}

Uses