Sample code for 30+ languages & platforms
Java

Compressing StringBuilder Data Using CompressSb (Single Call and Chunked)

See more Compression Examples

This example demonstrates how to compress text stored in a StringBuilder using the CompressSb method in two ways:

  1. Single-call compression, where the entire input is compressed in one operation.
  2. Chunked compression, where the input is split into multiple parts and processed sequentially using the FirstChunk and LastChunk properties.

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

  • CompressSb converts text to bytes using the Charset property (UTF-8 recommended).
  • Compressed data is appended to a BinData object.
  • When both FirstChunk and LastChunk are true, compression is done in a single call.
  • For chunked processing:
    • First chunk → FirstChunk = true, LastChunk = false
    • Middle chunks → both false
    • Final chunk → LastChunk = true
  • Chunked compression is useful when processing data incrementally.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Compression;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API has already been unlocked.
        // See Global Unlock Sample for sample code.

        Compression compress = new Compression();
        compress.setAlgorithm("zlib");

        // ================================================================
        // 1) Single-call compression (entire data in one call)
        // ================================================================

        StringBuilder sb = new StringBuilder();
        sb.append("The quick brown fox jumps over the lazy dog. ");
        sb.append("This is a simple example using CompressSb.");

        BinData bdCompressed = new BinData();

        // When both FirstChunk and LastChunk are true (the defaults),
        // the entire compression happens in a single call.
        compress.setFirstChunk(true);
        compress.setLastChunk(true);

        try {
            compress.compressSb(sb, bdCompressed);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        String compressedBase64 = bdCompressed.getEncoded("base64");

        System.out.println("Single-call compressed (base64):");
        System.out.println(compressedBase64);

        // ================================================================
        // 2) Chunked compression using FirstChunk / LastChunk
        // ================================================================

        BinData bdChunkedOut = new BinData();

        // First chunk
        compress.setFirstChunk(true);
        compress.setLastChunk(false);

        StringBuilder sbPart = new StringBuilder();
        sbPart.append("The quick brown fox ");

        try {
            compress.compressSb(sbPart, bdChunkedOut);

            // Middle chunk
            compress.setFirstChunk(false);
            compress.setLastChunk(false);

            sbPart.clear();
            sbPart.append("jumps over the lazy dog. ");

            compress.compressSb(sbPart, bdChunkedOut);

            // Final chunk
            compress.setFirstChunk(false);
            compress.setLastChunk(true);

            sbPart.clear();
            sbPart.append("This is a chunked CompressSb example.");

            compress.compressSb(sbPart, bdChunkedOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        String chunkedBase64 = bdChunkedOut.getEncoded("base64");

        System.out.println("Chunked compressed (base64):");
        System.out.println(chunkedBase64);

        // ================================================================
        // 3) Decompress to verify correctness
        // ================================================================

        StringBuilder sbDecompressed = new StringBuilder();

        // Decompress in a single call (entire data already assembled)
        compress.setFirstChunk(true);
        compress.setLastChunk(true);

        try {
            compress.decompressSb(bdChunkedOut, sbDecompressed);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Decompressed text:");
        System.out.println(sbDecompressed.getAsString());
    }
}