React Native
React Native
Compress Bytes to Base64 (or any other encoding)
See more Compression Examples
Compresses bytes to base64 or any other encoding. Also decompress to return the original.Chilkat React Native Downloads
import { BinData, Compression } 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.
// First create some binary data to compress.
const binData = new BinData();
for (let i = 1; i <= 16; i++) {
binData.appendEncoded('000102030405060708090A0B0C0D0E0F', 'hex');
}
const compress = new Compression();
compress.algorithm = 'deflate';
compress.encodingMode = 'base64';
let uncompressedBytes = new ArrayBuffer(0);
uncompressedBytes = binData.getBinary();
// Compress and return the compressed bytes in base64 format.
const compressedBase64 = await compress.compressBytesENCAsync(uncompressedBytes);
console.log(`compressed and base64 encoded: ${compressedBase64}`);
// Compress and return in hex format:
compress.encodingMode = 'hex';
const compressedHex = await compress.compressBytesENCAsync(uncompressedBytes);
console.log(`compressed and hex encoded: ${compressedHex}`);
// Now decompress..
const binData2 = new BinData();
// Decompress the base64..
compress.encodingMode = 'base64';
uncompressedBytes = await compress.decompressBytesENCAsync(compressedBase64);
binData2.appendBinary(uncompressedBytes);
// Show the uncompressed bytes in hex format:
console.log(binData2.getEncoded('hex'));
console.log('--');
// Decompress the hex..
compress.encodingMode = 'hex';
uncompressedBytes = await compress.decompressBytesENCAsync(compressedHex);
binData2.clear();
binData2.appendBinary(uncompressedBytes);
// Show the uncompressed bytes in hex format:
console.log(binData2.getEncoded('hex'));
console.log('--');
}