React Native Requires Chilkat v11.0.0+
React Native
Compress Text Feed to Binary
See more Compression Examples
This example receives incoming text data in chunks, compresses as a stream, and accumulates the compressed binary data.Chilkat React Native Downloads
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 bdCompressed = new BinData();
const compress = new Compression();
compress.algorithm = 'deflate';
compress.charset = 'utf-8';
const sbUncompressedChunk = new StringBuilder();
compress.firstChunk = true;
compress.lastChunk = false;
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;
}
// Show the compressed data in hex format:
console.log('The hex encoded compressed text:');
console.log(bdCompressed.getEncoded('hex'));
// Now decompress in one call. It is important to set both FirstChunk and LastChunk = true
const bdDecompressed = new BinData();
compress.firstChunk = true;
compress.lastChunk = true;
try {
await compress.decompressBd2Async(bdCompressed, bdDecompressed);
} catch {
console.log(compress.lastErrorText);
return;
}
const originalText = bdDecompressed.getString('utf-8');
console.log(originalText);
}