VB.NET
VB.NET
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 VB.NET Downloads
Dim success As Boolean = False
' This example assumes the Chilkat API has already been unlocked.
' See Global Unlock Sample for sample code.
Dim compress As New Chilkat.Compression
' Use the zlib algorithm (recommended for general use)
compress.Algorithm = "zlib"
' ------------------------------------------------------------------
' Compress a file
' ------------------------------------------------------------------
Dim inputFile As String = "c:/temp/example.txt"
Dim compressedFile As String = "c:/temp/example.txt.zlib"
success = compress.CompressFile(inputFile,compressedFile)
If (success = False) Then
Debug.WriteLine("Compression failed:")
Debug.WriteLine(compress.LastErrorText)
Exit Sub
End If
Debug.WriteLine("File compressed successfully:")
Debug.WriteLine(" Input: " & inputFile)
Debug.WriteLine(" Compressed: " & compressedFile)
' ------------------------------------------------------------------
' Decompress the file back to its original form
' ------------------------------------------------------------------
Dim decompressedFile As String = "c:/temp/example_restored.txt"
success = compress.DecompressFile(compressedFile,decompressedFile)
If (success = False) Then
Debug.WriteLine("Decompression failed:")
Debug.WriteLine(compress.LastErrorText)
Exit Sub
End If
Debug.WriteLine("File decompressed successfully:")
Debug.WriteLine(" Output: " & decompressedFile)
' ------------------------------------------------------------------
' Optional: Verify file sizes (basic sanity check)
' ------------------------------------------------------------------
Dim fac As New Chilkat.FileAccess
Dim originalSize As Integer = fac.FileSize(inputFile)
Dim restoredSize As Integer = fac.FileSize(decompressedFile)
Debug.WriteLine("Original file size: " & originalSize)
Debug.WriteLine("Restored file size: " & restoredSize)
If (originalSize = restoredSize) Then
Debug.WriteLine("Sizes match (basic verification successful).")
Else
Debug.WriteLine("Warning: File sizes differ.")
End If