Sample code for 30+ languages & platforms
AutoIt

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 AutoIt Downloads

AutoIt
Local $bSuccess = False

; This example assumes the Chilkat API has already been unlocked.
; See Global Unlock Sample for sample code.

$oCompress = ObjCreate("Chilkat.Compression")

; Use the zlib algorithm (recommended for general use)
$oCompress.Algorithm = "zlib"

; ------------------------------------------------------------------
; Compress a file
; ------------------------------------------------------------------

Local $sInputFile = "c:/temp/example.txt"
Local $sCompressedFile = "c:/temp/example.txt.zlib"

$bSuccess = $oCompress.CompressFile($sInputFile,$sCompressedFile)
If ($bSuccess = False) Then
    ConsoleWrite("Compression failed:" & @CRLF)
    ConsoleWrite($oCompress.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("File compressed successfully:" & @CRLF)
ConsoleWrite("  Input:      " & $sInputFile & @CRLF)
ConsoleWrite("  Compressed: " & $sCompressedFile & @CRLF)

; ------------------------------------------------------------------
; Decompress the file back to its original form
; ------------------------------------------------------------------

Local $sDecompressedFile = "c:/temp/example_restored.txt"

$bSuccess = $oCompress.DecompressFile($sCompressedFile,$sDecompressedFile)
If ($bSuccess = False) Then
    ConsoleWrite("Decompression failed:" & @CRLF)
    ConsoleWrite($oCompress.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("File decompressed successfully:" & @CRLF)
ConsoleWrite("  Output: " & $sDecompressedFile & @CRLF)

; ------------------------------------------------------------------
; Optional: Verify file sizes (basic sanity check)
; ------------------------------------------------------------------

$oFac = ObjCreate("Chilkat.FileAccess")

Local $iOriginalSize = $oFac.FileSize($sInputFile)
Local $iRestoredSize = $oFac.FileSize($sDecompressedFile)

ConsoleWrite("Original file size:   " & $iOriginalSize & @CRLF)
ConsoleWrite("Restored file size:   " & $iRestoredSize & @CRLF)

If ($iOriginalSize = $iRestoredSize) Then
    ConsoleWrite("Sizes match (basic verification successful)." & @CRLF)
Else
    ConsoleWrite("Warning: File sizes differ." & @CRLF)
EndIf