Java Requires Chilkat v11.0.0+
Chilkat2 Java
Compress String Feed to Base64
See more Compression Examples
This example receives incoming text data in chunks, compresses as a stream, and accumulates the compressed data in base64.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.BinData;
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 to have been previously unlocked.
// See Global Unlock Sample for sample code.
StringBuilder sbCompressedBase64 = new StringBuilder();
Compression compress = new Compression();
compress.setAlgorithm("deflate");
compress.setCharset("utf-8");
compress.setEncodingMode("base64");
compress.setFirstChunk(true);
compress.setLastChunk(false);
BinData bdCompressed = new BinData();
StringBuilder sbUncompressedChunk = new StringBuilder();
for (int i = 0; i <= 24; i++) {
if (i == 24) {
compress.setLastChunk(true);
}
sbUncompressedChunk.clear();
sbUncompressedChunk.appendInt(i);
sbUncompressedChunk.append(": This is a line of data to be compressed...\r\n");
compress.compressSb(sbUncompressedChunk, bdCompressed);
compress.setFirstChunk(false);
}
System.out.println("The base64 encoded compressed text:");
System.out.println(bdCompressed.getEncoded("base64"));
// Decompress in one call:
String originalText = compress.decompressStringENC(bdCompressed.getEncoded("base64"));
System.out.println(originalText);
}
}