DataFlex
DataFlex
Compressing and Decompressing Files Using Streaming (CompressFile / DecompressFile)
See more Compression Examples
This example demonstrates how to compress a file to a binary format and then restore it using the Chilkat.Compression class. The CompressFile method reads the source file, compresses it using the specified algorithm, and writes the result to a destination file. The DecompressFile method performs the reverse operation, restoring the original file from the compressed data.
Both operations are performed internally in streaming mode, allowing files of any size to be processed efficiently without loading the entire file into memory. The example also includes a simple verification step by comparing file sizes to confirm that the decompressed output matches the original input.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoCompress
String sInputFile
String sCompressedFile
String sDecompressedFile
Handle hoFac
Integer iOriginalSize
Integer iRestoredSize
String sTemp1
Move False To iSuccess
// This example assumes the Chilkat API has already been unlocked.
// See Global Unlock Sample for sample code.
Get Create (RefClass(cComChilkatCompression)) To hoCompress
If (Not(IsComObjectCreated(hoCompress))) Begin
Send CreateComObject of hoCompress
End
// Use the zlib algorithm (recommended for general use)
Set ComAlgorithm Of hoCompress To "zlib"
// ------------------------------------------------------------------
// Compress a file
// ------------------------------------------------------------------
Move "c:/temp/example.txt" To sInputFile
Move "c:/temp/example.txt.zlib" To sCompressedFile
Get ComCompressFile Of hoCompress sInputFile sCompressedFile To iSuccess
If (iSuccess = False) Begin
Showln "Compression failed:"
Get ComLastErrorText Of hoCompress To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "File compressed successfully:"
Showln " Input: " sInputFile
Showln " Compressed: " sCompressedFile
// ------------------------------------------------------------------
// Decompress the file back to its original form
// ------------------------------------------------------------------
Move "c:/temp/example_restored.txt" To sDecompressedFile
Get ComDecompressFile Of hoCompress sCompressedFile sDecompressedFile To iSuccess
If (iSuccess = False) Begin
Showln "Decompression failed:"
Get ComLastErrorText Of hoCompress To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "File decompressed successfully:"
Showln " Output: " sDecompressedFile
// ------------------------------------------------------------------
// Optional: Verify file sizes (basic sanity check)
// ------------------------------------------------------------------
Get Create (RefClass(cComCkFileAccess)) To hoFac
If (Not(IsComObjectCreated(hoFac))) Begin
Send CreateComObject of hoFac
End
Get ComFileSize Of hoFac sInputFile To iOriginalSize
Get ComFileSize Of hoFac sDecompressedFile To iRestoredSize
Showln "Original file size: " iOriginalSize
Showln "Restored file size: " iRestoredSize
If (iOriginalSize = iRestoredSize) Begin
Showln "Sizes match (basic verification successful)."
End
Else Begin
Showln "Warning: File sizes differ."
End
End_Procedure