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

Decompress Large Binary File in Blocks

See more Compression Examples

Decompresses a large binary file in blocks.

Chilkat React Native Downloads

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

async function chilkatExample() {
  // This example requires 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 previously compressed file for decompressing.
  // See Compress Large File in Blocks

  try {
    facSrc.openForRead('c:/temp/qa_output/compressedBmp.dat');
  } catch {
    console.log(facSrc.lastErrorText);
    return;
  }

  // If we compress in 32K chunks, find out how many blocks there will be.
  const blockSize = 32768;
  const numBlocks = facSrc.getNumBlocks(blockSize);

  // Open an output file for the decompressed data.
  try {
    facDest.openForWrite('c:/temp/qa_output/decompressed.bmp');
  } catch {
    console.log(facDest.lastErrorText);
    return;
  }

  const compress = new Compression();
  compress.algorithm = 'deflate';

  let decompressedBytes = new ArrayBuffer(0);
  let compressedBytes = new ArrayBuffer(0);

  // Assuming numBlocks > 1
  compress.firstChunk = true;
  compress.lastChunk = false;

  let i = 0;
  while (i < numBlocks) {
    compressedBytes = facSrc.readBlock(i, blockSize);

    decompressedBytes = await compress.decompressBytesAsync(compressedBytes);
    facDest.fileWrite(decompressedBytes);

    i++;

    compress.firstChunk = false;
    if (i === (numBlocks - 1)) {
      compress.lastChunk = true;
    }
  }

  facSrc.fileClose();
  facDest.fileClose();

  console.log('Finished decompressing file.');
}