Sample code for 30+ languages & platforms
React Native Requires Chilkat v11.0.0+

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 React Native Downloads

React Native
import { BinData, Compression, StringBuilder } from '@chilkat/react-native'

async function chilkatExample() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const sbCompressedBase64 = new StringBuilder();

  const compress = new Compression();
  compress.algorithm = 'deflate';
  compress.charset = 'utf-8';
  compress.encodingMode = 'base64';

  compress.firstChunk = true;
  compress.lastChunk = false;

  const bdCompressed = new BinData();
  const sbUncompressedChunk = new StringBuilder();

  for (let i = 0; i <= 24; i++) {
    if (i === 24) {
      compress.lastChunk = true;
    }

    sbUncompressedChunk.clear();
    sbUncompressedChunk.appendInt(i);
    sbUncompressedChunk.append(': This is a line of data to be compressed...\r\n');

    await compress.compressSbAsync(sbUncompressedChunk, bdCompressed);

    compress.firstChunk = false;
  }

  console.log('The base64 encoded compressed text:');
  console.log(bdCompressed.getEncoded('base64'));

  // Decompress in one call:
  const originalText = await compress.decompressStringENCAsync(bdCompressed.getEncoded('base64'));
  console.log(originalText);
}