![]() |
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™) Compressing StringBuilder Data Using CompressSb (Single Call and Chunked)See more Compression ExamplesThis example demonstrates how to compress text stored in aStringBuilder using the CompressSb method in two ways:
The example shows how compressed output is appended to a Key Points
// 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"); // ================================================================ // 1) Single-call compression (entire data in one call) // ================================================================ CkStringBuilder sb = new CkStringBuilder(); sb.Append("The quick brown fox jumps over the lazy dog. "); sb.Append("This is a simple example using CompressSb."); CkBinData bdCompressed = new CkBinData(); // When both FirstChunk and LastChunk are true (the defaults), // the entire compression happens in a single call. compress.put_FirstChunk(true); compress.put_LastChunk(true); success = compress.CompressSb(sb,bdCompressed); if (success == false) { Log.i(TAG, compress.lastErrorText()); return; } String compressedBase64 = bdCompressed.getEncoded("base64"); Log.i(TAG, "Single-call compressed (base64):"); Log.i(TAG, compressedBase64); // ================================================================ // 2) Chunked compression using FirstChunk / LastChunk // ================================================================ CkBinData bdChunkedOut = new CkBinData(); // First chunk compress.put_FirstChunk(true); compress.put_LastChunk(false); CkStringBuilder sbPart = new CkStringBuilder(); sbPart.Append("The quick brown fox "); success = compress.CompressSb(sbPart,bdChunkedOut); if (success == false) { Log.i(TAG, compress.lastErrorText()); return; } // Middle chunk compress.put_FirstChunk(false); compress.put_LastChunk(false); sbPart.Clear(); sbPart.Append("jumps over the lazy dog. "); success = compress.CompressSb(sbPart,bdChunkedOut); if (success == false) { Log.i(TAG, compress.lastErrorText()); return; } // Final chunk compress.put_FirstChunk(false); compress.put_LastChunk(true); sbPart.Clear(); sbPart.Append("This is a chunked CompressSb example."); success = compress.CompressSb(sbPart,bdChunkedOut); if (success == false) { Log.i(TAG, compress.lastErrorText()); return; } String chunkedBase64 = bdChunkedOut.getEncoded("base64"); Log.i(TAG, "Chunked compressed (base64):"); Log.i(TAG, chunkedBase64); // ================================================================ // 3) Decompress to verify correctness // ================================================================ CkStringBuilder sbDecompressed = new CkStringBuilder(); // Decompress in a single call (entire data already assembled) compress.put_FirstChunk(true); compress.put_LastChunk(true); success = compress.DecompressSb(bdChunkedOut,sbDecompressed); if (success == false) { Log.i(TAG, compress.lastErrorText()); return; } Log.i(TAG, "Decompressed text:"); Log.i(TAG, sbDecompressed.getAsString()); } 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.