Android™
Android™
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 Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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) {
Log.i(TAG, "Compression failed:");
Log.i(TAG, compress.lastErrorText());
return;
}
Log.i(TAG, "File compressed successfully:");
Log.i(TAG, " Input: " + inputFile);
Log.i(TAG, " 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) {
Log.i(TAG, "Decompression failed:");
Log.i(TAG, compress.lastErrorText());
return;
}
Log.i(TAG, "File decompressed successfully:");
Log.i(TAG, " Output: " + decompressedFile);
// ------------------------------------------------------------------
// Optional: Verify file sizes (basic sanity check)
// ------------------------------------------------------------------
CkFileAccess fac = new CkFileAccess();
int originalSize = fac.FileSize(inputFile);
int restoredSize = fac.FileSize(decompressedFile);
Log.i(TAG, "Original file size: " + String.valueOf(originalSize));
Log.i(TAG, "Restored file size: " + String.valueOf(restoredSize));
if (originalSize == restoredSize) {
Log.i(TAG, "Sizes match (basic verification successful).");
}
else {
Log.i(TAG, "Warning: File sizes differ.");
}
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}