A React Native module for working with ZIP archives. This module allows you to list the contents of ZIP files, stream files from ZIP archives, create new ZIP files, and unzip files with progress tracking.
To install the module, follow these steps:
-
Add the dependency to your project:
npm install react-native-zip-stream
-
For iOS, install the required CocoaPods dependencies:
cd ios pod install
react-native-zip-stream
can also be used in Expo projects with some additional configuration. If you're using Expo and want to access the app's local document directory (e.g., to read and write ZIP files), follow these steps:
-
Install the package:
expo install react-native-zip-stream
-
Add the plugin configuration to your
app.json
:{ "expo": { "plugins": ["./node_modules/react-native-zip-stream/plugin"] } }
-
Run
expo prebuild
to generate the necessary native code for your Expo project.
This setup ensures that the app has the correct permissions to access local files and manipulate ZIP files on both Android and iOS.
Lists the contents of a ZIP file. This function returns an array of file names contained within the ZIP archive.
Streams a specific file from a ZIP archive. You can retrieve the file data in one of three formats: base64
, arraybuffer
, or string
.
Extracts all the contents of a ZIP file to a specified destination directory.
Creates a new ZIP file from the contents of a specified directory.
Lists the contents of a ZIP file.
-
zipFilePath
:string
- The full path to the ZIP file.
-
Promise<string[]>
- A promise that resolves to an array of file names inside the ZIP file.
Streams a specific file from the ZIP archive.
-
zipFilePath
:string
- The full path to the ZIP file. -
entryName
:string
- The name of the file within the ZIP archive to extract. -
type
:string
(optional, default:base64
) - The format in which to return the file data. Can bebase64
,arraybuffer
, orstring
.
-
Promise<string | ArrayBuffer | Uint8Array>
- A promise that resolves to the file content in the specified format.
Extracts all the contents of a ZIP file to a specified destination directory.
-
zipFilePath
:string
- The full path to the ZIP file. -
destinationPath
:string
- The path where the contents of the ZIP file should be extracted.
-
Promise<boolean>
- A promise that resolves totrue
if the operation is successful.
Creates a new ZIP file from the contents of a specified directory.
-
destinationPath
:string
- The full path where the ZIP file should be created. -
sourcePath
:string
- The path to the directory or file that should be zipped.
-
Promise<boolean>
- A promise that resolves totrue
if the ZIP file is created successfully.
import { listZipContents } from 'react-native-zip-stream';
const zipFilePath = '/path/to/your/zipfile.zip';
const exampleListZipContents = async () => {
try {
const fileNames = await listZipContents(zipFilePath);
console.log('Files in ZIP:', fileNames);
} catch (error) {
console.error('Error listing ZIP contents:', error);
}
};
import { streamFileFromZip } from 'react-native-zip-stream';
const zipFilePath = '/path/to/your/zipfile.zip';
const entryName = 'fileInsideZip.txt';
const exampleStreamFile = async () => {
try {
const base64Data = await streamFileFromZip(
zipFilePath,
entryName,
'base64'
);
console.log('Base64 Data:', base64Data);
const arrayBufferData = await streamFileFromZip(
zipFilePath,
entryName,
'arraybuffer'
);
console.log('ArrayBuffer Data:', new Uint8Array(arrayBufferData));
const stringData = await streamFileFromZip(
zipFilePath,
entryName,
'string'
);
console.log('String Data:', stringData);
} catch (error) {
console.error('Error streaming file:', error);
}
};
import { unzipFile } from 'react-native-zip-stream';
const zipFilePath = '/path/to/your/zipfile.zip';
const destinationPath = '/path/to/extract/';
const exampleUnzipFile = async () => {
try {
const success = await unzipFile(zipFilePath, destinationPath);
console.log('Unzip successful:', success);
} catch (error) {
console.error('Error unzipping file:', error);
}
};
import { createZipFile } from 'react-native-zip-stream';
const sourcePath = '/path/to/source/folder';
const destinationPath = '/path/to/output.zip';
const exampleCreateZipFile = async () => {
try {
const success = await createZipFile(destinationPath, sourcePath);
console.log('Zip creation successful:', success);
} catch (error) {
console.error('Error creating zip file:', error);
}
};