![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(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 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.
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 |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.