Java
Java
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 Java Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean success = false;
// This example assumes the Chilkat API has already been unlocked.
// See Global Unlock Sample for sample code.
CkCompression compress = new CkCompression();
// Use the zlib algorithm (recommended for general use)
compress.put_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) {
System.out.println("Compression failed:");
System.out.println(compress.lastErrorText());
return;
}
System.out.println("File compressed successfully:");
System.out.println(" Input: " + inputFile);
System.out.println(" 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) {
System.out.println("Decompression failed:");
System.out.println(compress.lastErrorText());
return;
}
System.out.println("File decompressed successfully:");
System.out.println(" Output: " + decompressedFile);
// ------------------------------------------------------------------
// Optional: Verify file sizes (basic sanity check)
// ------------------------------------------------------------------
CkFileAccess fac = new CkFileAccess();
int originalSize = fac.FileSize(inputFile);
int restoredSize = fac.FileSize(decompressedFile);
System.out.println("Original file size: " + originalSize);
System.out.println("Restored file size: " + restoredSize);
if (originalSize == restoredSize) {
System.out.println("Sizes match (basic verification successful).");
}
else {
System.out.println("Warning: File sizes differ.");
}
}
}