Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Compress
string ls_InputFile
string ls_CompressedFile
string ls_DecompressedFile
oleobject loo_Fac
integer li_OriginalSize
integer li_RestoredSize

li_Success = 0

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

loo_Compress = create oleobject
li_rc = loo_Compress.ConnectToNewObject("Chilkat.Compression")
if li_rc < 0 then
    destroy loo_Compress
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Use the zlib algorithm (recommended for general use)
loo_Compress.Algorithm = "zlib"

// ------------------------------------------------------------------
// Compress a file
// ------------------------------------------------------------------

ls_InputFile = "c:/temp/example.txt"
ls_CompressedFile = "c:/temp/example.txt.zlib"

li_Success = loo_Compress.CompressFile(ls_InputFile,ls_CompressedFile)
if li_Success = 0 then
    Write-Debug "Compression failed:"
    Write-Debug loo_Compress.LastErrorText
    destroy loo_Compress
    return
end if

Write-Debug "File compressed successfully:"
Write-Debug "  Input:      " + ls_InputFile
Write-Debug "  Compressed: " + ls_CompressedFile

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

ls_DecompressedFile = "c:/temp/example_restored.txt"

li_Success = loo_Compress.DecompressFile(ls_CompressedFile,ls_DecompressedFile)
if li_Success = 0 then
    Write-Debug "Decompression failed:"
    Write-Debug loo_Compress.LastErrorText
    destroy loo_Compress
    return
end if

Write-Debug "File decompressed successfully:"
Write-Debug "  Output: " + ls_DecompressedFile

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

loo_Fac = create oleobject
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")

li_OriginalSize = loo_Fac.FileSize(ls_InputFile)
li_RestoredSize = loo_Fac.FileSize(ls_DecompressedFile)

Write-Debug "Original file size:   " + string(li_OriginalSize)
Write-Debug "Restored file size:   " + string(li_RestoredSize)

if li_OriginalSize = li_RestoredSize then
    Write-Debug "Sizes match (basic verification successful)."
else
    Write-Debug "Warning: File sizes differ."
end if



destroy loo_Compress
destroy loo_Fac