Java
Java
Compressing StringBuilder Data Using CompressSb (Single Call and Chunked)
See more Compression Examples
This example demonstrates how to compress text stored in aStringBuilder using the CompressSb method in two ways:
- Single-call compression, where the entire input is compressed in one operation.
- Chunked compression, where the input is split into multiple parts and processed sequentially using the
FirstChunkandLastChunkproperties.
The example shows how compressed output is appended to a BinData object, and how the result can be verified by decompressing back to the original text. This is useful for both simple use cases and scenarios where data is processed incrementally (such as streaming or large inputs).
Key Points
CompressSbconverts text to bytes using theCharsetproperty (UTF-8 recommended).- Compressed data is appended to a
BinDataobject. - When both
FirstChunkandLastChunkaretrue, compression is done in a single call. - For chunked processing:
- First chunk →
FirstChunk = true,LastChunk = false - Middle chunks → both
false - Final chunk →
LastChunk = true
- First chunk →
- Chunked compression is useful when processing data incrementally.
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();
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) {
System.out.println(compress.lastErrorText());
return;
}
String compressedBase64 = bdCompressed.getEncoded("base64");
System.out.println("Single-call compressed (base64):");
System.out.println(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) {
System.out.println(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) {
System.out.println(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) {
System.out.println(compress.lastErrorText());
return;
}
String chunkedBase64 = bdChunkedOut.getEncoded("base64");
System.out.println("Chunked compressed (base64):");
System.out.println(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) {
System.out.println(compress.lastErrorText());
return;
}
System.out.println("Decompressed text:");
System.out.println(sbDecompressed.getAsString());
}
}