![]() |
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
(C#) 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.
bool success = false; // This example assumes the Chilkat API has already been unlocked. // See Global Unlock Sample for sample code. Chilkat.Compression compress = new Chilkat.Compression(); // Use the zlib algorithm (recommended for general use) compress.Algorithm = "zlib"; // ------------------------------------------------------------------ // Compress a file // ------------------------------------------------------------------ string inputFile = "c:/temp/example.txt"; string compressedFile = "c:/temp/example.txt.zlib"; success = compress.CompressFile(inputFile,compressedFile); if (success == false) { Debug.WriteLine("Compression failed:"); Debug.WriteLine(compress.LastErrorText); return; } Debug.WriteLine("File compressed successfully:"); Debug.WriteLine(" Input: " + inputFile); Debug.WriteLine(" Compressed: " + compressedFile); // ------------------------------------------------------------------ // Decompress the file back to its original form // ------------------------------------------------------------------ string decompressedFile = "c:/temp/example_restored.txt"; success = compress.DecompressFile(compressedFile,decompressedFile); if (success == false) { Debug.WriteLine("Decompression failed:"); Debug.WriteLine(compress.LastErrorText); return; } Debug.WriteLine("File decompressed successfully:"); Debug.WriteLine(" Output: " + decompressedFile); // ------------------------------------------------------------------ // Optional: Verify file sizes (basic sanity check) // ------------------------------------------------------------------ Chilkat.FileAccess fac = new Chilkat.FileAccess(); int originalSize = fac.FileSize(inputFile); int restoredSize = fac.FileSize(decompressedFile); Debug.WriteLine("Original file size: " + Convert.ToString(originalSize)); Debug.WriteLine("Restored file size: " + Convert.ToString(restoredSize)); if (originalSize == restoredSize) { Debug.WriteLine("Sizes match (basic verification successful)."); } else { Debug.WriteLine("Warning: File sizes differ."); } |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.