React Native Requires Chilkat v11.0.0+
React Native
Compress Large Binary File in Blocks
See more Compression Examples
Compresses a large binary file in blocks.Chilkat React Native Downloads
import { Compression, FileAccess } 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 facSrc = new FileAccess();
const facDest = new FileAccess();
// Open a large binary file for reading.
try {
facSrc.openForRead('qa_data/bmp/big.bmp');
} catch {
console.log(facSrc.lastErrorText);
return;
}
// If we compress in 64K chunks, find out how many blocks there will be.
const blockSize = 65536;
const numBlocks = facSrc.getNumBlocks(blockSize);
// Open an output file for the compressed data.
try {
facDest.openForWrite('c:/temp/qa_output/compressedBmp.dat');
} catch {
console.log(facDest.lastErrorText);
return;
}
const compress = new Compression();
compress.algorithm = 'deflate';
let fileBytes = new ArrayBuffer(0);
let compressedBytes = new ArrayBuffer(0);
// Assuming numBlocks > 1
compress.firstChunk = true;
compress.lastChunk = false;
let i = 0;
while (i < numBlocks) {
fileBytes = facSrc.readBlock(i, blockSize);
compressedBytes = await compress.compressBytesAsync(fileBytes);
facDest.fileWrite(compressedBytes);
i++;
compress.firstChunk = false;
if (i === (numBlocks - 1)) {
compress.lastChunk = true;
}
}
facSrc.fileClose();
facDest.fileClose();
console.log('Finished compressing file.');
}