DEV Community

Mateo Guzmán
Mateo Guzmán

Posted on

LZ4 – C++ React Native bindings for an extremely fast compression algorithm

I've been dipping my toes into JSI and C++ lately and, as a result, I got to build a small package called react-native-lz4. It’s a library for fast file compression in React Native using the LZ4 algorithm written in C.

It is still experimental as I'm still polishing the error handling and extending its API but it can already be used (with caution!)

Package: https://github.com/mateoguzmana/react-native-lz4
You can learn more about LZ4 on its website: https://lz4.org/

The package supports both old and new architecture, and currently exposes two main functions to compress and decompress any type of file.

Basic example:

import { compressFile, decompressFile } from 'react-native-lz4';

function onProgress(processedSize: number, totalSize: number) {
  // e.g. { processedSize: 50, totalSize: 100, progress: '50%' }
  console.log({
    processedSize,
    totalSize,
    progress: `${Math.round((processedSize / totalSize) * 100)}%`,
  });
}

const compressionResult = await compressFile(
  'path/to/file',
  'path/to/output',
  onProgress
);
const decompressionResult = await decompressFile(
  'path/to/file',
  'path/to/output',
  onProgress
);

console.log(compressionResult);
// { success: true, message: 'File compressed successfully', originalSize: 100, finalSize: 50 }

console.log(decompressionResult);
// { success: true, message: 'File decompressed successfully', originalSize: 50, finalSize: 100 }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)