PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkFileAccess.pb"
IncludeFile "CkCompression.pb"
Procedure ChilkatExample()
success.i = 0
; This example assumes the Chilkat API has already been unlocked.
; See Global Unlock Sample for sample code.
compress.i = CkCompression::ckCreate()
If compress.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Use the zlib algorithm (recommended for general use)
CkCompression::setCkAlgorithm(compress, "zlib")
; ------------------------------------------------------------------
; Compress a file
; ------------------------------------------------------------------
inputFile.s = "c:/temp/example.txt"
compressedFile.s = "c:/temp/example.txt.zlib"
success = CkCompression::ckCompressFile(compress,inputFile,compressedFile)
If success = 0
Debug "Compression failed:"
Debug CkCompression::ckLastErrorText(compress)
CkCompression::ckDispose(compress)
ProcedureReturn
EndIf
Debug "File compressed successfully:"
Debug " Input: " + inputFile
Debug " Compressed: " + compressedFile
; ------------------------------------------------------------------
; Decompress the file back to its original form
; ------------------------------------------------------------------
decompressedFile.s = "c:/temp/example_restored.txt"
success = CkCompression::ckDecompressFile(compress,compressedFile,decompressedFile)
If success = 0
Debug "Decompression failed:"
Debug CkCompression::ckLastErrorText(compress)
CkCompression::ckDispose(compress)
ProcedureReturn
EndIf
Debug "File decompressed successfully:"
Debug " Output: " + decompressedFile
; ------------------------------------------------------------------
; Optional: Verify file sizes (basic sanity check)
; ------------------------------------------------------------------
fac.i = CkFileAccess::ckCreate()
If fac.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
originalSize.i = CkFileAccess::ckFileSize(fac,inputFile)
restoredSize.i = CkFileAccess::ckFileSize(fac,decompressedFile)
Debug "Original file size: " + Str(originalSize)
Debug "Restored file size: " + Str(restoredSize)
If originalSize = restoredSize
Debug "Sizes match (basic verification successful)."
Else
Debug "Warning: File sizes differ."
EndIf
CkCompression::ckDispose(compress)
CkFileAccess::ckDispose(fac)
ProcedureReturn
EndProcedure