Skip to content

klarna/react-native-zlib

Repository files navigation

@klarna/react-native-zlib

Getting started

$ npm install @klarna/react-native-zlib --save

Mostly automatic installation

$ react-native link @klarna/react-native-zlib

Manual installation

iOS

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-native-zlib and add RNReactNativeZlib.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libRNReactNativeZlib.a to your project's Build PhasesLink Binary With Libraries
  4. Run your project (Cmd+R)<

Android

  1. Open up android/app/src/main/java/[...]/MainActivity.java
  • Add import com.klarna.reactnative.zlib.RNReactNativeZlibPackage; to the imports at the top of the file
  • Add new RNReactNativeZlibPackage() to the list returned by the getPackages() method
  1. Append the following lines to android/settings.gradle:
    include ':react-native-zlib'
    project(':react-native-zlib').projectDir = new File(rootProject.projectDir, 	'../node_modules/react-native-zlib/android')
    
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
      compile project(':react-native-zlib')
    

Android Helper

Open settings.gradle of your root project and add into it:

 def configureRNModule(prj) {
     def dir = new File(rootProject.projectDir, "../node_modules/${prj.name}/android")
     def dirSrc = new File(rootProject.projectDir, "../node_modules/${prj.name}/src/android")
     def dirKlarna = new File(rootProject.projectDir, "../node_modules/@klarna/${prj.name}/android")

     if (dirKlarna.exists()) {
         prj.projectDir = dirKlarna
     } else if (dirSrc.exists()) {
         prj.projectDir = dirSrc
     } else if (dir.exists()) {
         prj.projectDir = dir
     } else {
         throw new Exception("project directory not found! project: ${prj.name}")
     }
}

after that you can replace line:

project(':react-native-zlib').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-zlib/android')

on shorter version:

configureRNModule(project(':react-native-zlib'))

Usage

import RNReactNativeZlib from '@klarna/react-native-zlib';
import base64 from "react-native-base64";
import { Buffer } from "buffer";

/* Working with byte arrays */
const testArray = [0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6];
RNReactNativeZlib.deflate(testArray).then(compressed => {
  RNReactNativeZlib.inflate(compressed).then(decompressed => {
    // decompressed
  })
});

/* Working with base64 strings */
const testString = `{ experiment: 'something inside'}`;
const encoded1 = base64.encode(testString);
const encoded2 = Buffer.from(testString).toString("base64");

RNReactNativeZlib.deflateBase64(encoded1).then(compressed => {
  // base64 --> byte[] --> compressed byte[] --> base64
  
  RNReactNativeZlib.inflateBase64(compressed).then(decompressed => {
    const result1 = base64.decode(buffer);
    const result2 = Buffer.from(buffer, 'base64').toString('utf8')

    // decompressed
  })
});

Supported API

References on Alternatives