React Native implementation of Zlib compression library. It's written in TypeScript and has direct bindings to a Zlib C++ library. The primary function of this implementation is to provide fast and efficient compression and decompression capabilities.
To use this library in your React Native project, run the following commands:
Start by installing Nitro Modules. You can find more information here.
bun i react-native-nitro-modules
cd ios && pod install
Now install react-native-nitro-zlib
bun install react-native-nitro-zlib
import { zlib } from "react-native-nitro-zlib";
// Compression
const data = new TextEncoder().encode("Hello, World!");
const compressed = zlib.deflate(data.buffer);
console.log("Compressed data:", compressed);
// Decompression
const decompressed = zlib.inflate(compressed);
console.log("Decompressed data:", new TextDecoder().decode(decompressed));
// Using streams
const deflateStream = zlib.createDeflateStream();
deflateStream.onData((chunk) => console.log("Received chunk:", chunk));
deflateStream.onEnd(() => console.log("Stream ended"));
deflateStream.write(data.buffer);
deflateStream.end();
interface Zlib {
readonly version: string;
inflate(data: ArrayBuffer, flush?: FlushMode): ArrayBuffer;
inflateRaw(data: ArrayBuffer, flush?: FlushMode): ArrayBuffer;
compress(data: ArrayBuffer, level?: CompressionLevel): ArrayBuffer;
deflate(data: ArrayBuffer, level?: CompressionLevel, flush?: FlushMode): ArrayBuffer;
deflateRaw(data: ArrayBuffer, level?: CompressionLevel, flush?: FlushMode): ArrayBuffer;
gzip(data: ArrayBuffer, level?: CompressionLevel): ArrayBuffer;
gunzip(data: ArrayBuffer): ArrayBuffer;
createDeflateStream(level?: CompressionLevel, strategy?: number): ZlibStream;
createInflateStream(): ZlibStream;
}
interface ZlibStream {
write(chunk: ArrayBuffer): boolean;
end(): void;
flush(kind?: number): void;
onData(callback: (chunk: ArrayBuffer) => void): void;
onEnd(callback: () => void): void;
onError(callback: (error: Error) => void): void;
params(level: number, strategy: number): void;
reset(): void;
getMemorySize(): number;
}
type CompressionLevel = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
type FlushMode =
| 'NO_FLUSH'
| 'PARTIAL_FLUSH'
| 'SYNC_FLUSH'
| 'FULL_FLUSH'
| 'FINISH'
| 'BLOCK'
| 'TREES';
Decompresses the given data.
Decompresses raw deflate data.
Compresses the given data.
Compresses the given data using the deflate algorithm.
Compresses the given data using the raw deflate algorithm.
Compresses the given data using the gzip format.
Decompresses gzip data.
Creates a new deflate stream.
Creates a new inflate stream.
Writes a chunk of data to the stream.
Ends the stream.
Flushes the stream.
Sets a callback to be called when data is available.
Sets a callback to be called when the stream ends.
Sets a callback to be called when an error occurs.
Updates the compression parameters.
Resets the stream.
Returns the current memory usage of the stream.
- mrousavy/nitro Nitro Modules
- zlib Zlib compression library
- margelo/react-native-worklets-core Helpful for how JSI works