![]() |
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
(Android™) Chunked Compression Using CompressBd2 with FirstChunk and LastChunkSee more Compression ExamplesThis example demonstrates how to compress data in multiple segments using theCompressBd2 method together with the FirstChunk and LastChunk properties. Instead of compressing all data in a single call, the input is divided into smaller chunks and processed sequentially, which is useful when handling streaming data or large inputs.
The example shows how to correctly mark the first, middle, and final chunks so that the compression stream is properly initialized and finalized. The compressed output is accumulated in a separate This approach is ideal for scenarios where data is received incrementally, such as reading from a network stream, processing large files in parts, or handling real-time data feeds.
// 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(); compress.put_Algorithm("zlib"); // This will accumulate the compressed output. CkBinData bdOut = new CkBinData(); // ------------------------------------------------------------------ // Simulate input arriving in chunks. // ------------------------------------------------------------------ String part1 = "The quick brown fox "; String part2 = "jumps over the lazy dog. "; String part3 = "This text is split into chunks."; // ------------------------------------------------------------------ // Compress the first chunk // ------------------------------------------------------------------ compress.put_FirstChunk(true); compress.put_LastChunk(false); CkBinData bdIn = new CkBinData(); bdIn.AppendString(part1,"utf-8"); success = compress.CompressBd2(bdIn,bdOut); if (success == false) { Log.i(TAG, compress.lastErrorText()); return; } // ------------------------------------------------------------------ // Compress a middle chunk // ------------------------------------------------------------------ compress.put_FirstChunk(false); compress.put_LastChunk(false); bdIn.Clear(); bdIn.AppendString(part2,"utf-8"); success = compress.CompressBd2(bdIn,bdOut); if (success == false) { Log.i(TAG, compress.lastErrorText()); return; } // ------------------------------------------------------------------ // Compress the final chunk // ------------------------------------------------------------------ compress.put_FirstChunk(false); compress.put_LastChunk(true); bdIn.Clear(); bdIn.AppendString(part3,"utf-8"); success = compress.CompressBd2(bdIn,bdOut); if (success == false) { Log.i(TAG, compress.lastErrorText()); return; } // Get the final compressed result as base64 for display String compressedBase64 = bdOut.getEncoded("base64"); Log.i(TAG, "Compressed data (base64):"); Log.i(TAG, compressedBase64); // ------------------------------------------------------------------ // Decompress to verify correctness // ------------------------------------------------------------------ CkBinData bdDecompressed = new CkBinData(); compress.put_FirstChunk(true); compress.put_LastChunk(true); success = compress.DecompressBd2(bdOut,bdDecompressed); if (success == false) { Log.i(TAG, compress.lastErrorText()); return; } // Convert decompressed bytes back to a string String resultText = bdDecompressed.getString("utf-8"); Log.i(TAG, "Decompressed text:"); Log.i(TAG, resultText); } 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." } } |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.