import JSZip from "jszip";
const createArchive = async() => {
// Some code
const zip = new JSZip();
const data = getExampleData();
zip.file("example.json", data);
const zipContent = await zip.generateAsync(
{ type: "base64" },
(metadata) => {
setCreationProgress(metadata.percent); // Set a progress value for ui
console.log("Value:", metadata.percent);
}
);// When this function is executed the ui freezes
// Some code
}
I create an zip archive in this code. It actually works, but when the function "zip.generateAsync()" is executed, the app freezes until the function completes. This can take a long time depending on the size of the files. The interface does not update, and nothing can be clicked. So, the problem is that the user cannot see the progress of the archive creation or cancel it because the interface is unresponsive.
(Also, keep in mind that I’m using Expo).
I believe one solution could be to move the task from the main thread to the background using the ‘expo-task-manager’ and ‘expo-background-fetch’ libraries, but I haven’t been able to make it work yet. Alternatively, I could use another library for creating the archive, but I don’t know which one, as nothing else seems to work with Expo.
2
Answers
You can use the react-native-zip-stream library to extract a specific file from a ZIP archive without needing to unzip the entire archive. This library allows you to stream individual files in various formats like base64, arraybuffer, or string.
Here’s an example of how to extract a specific file from a ZIP archive:
In this example, you can choose the format (base64, arraybuffer, or string) depending on how you want the file data to be processed in your app. This method extracts only the specified file, which is ideal for large ZIP archives where you don’t want to extract everything.
You can find more details on the library here: react-native-zip-stream GitHub.
react-native-zip-stream It seems that the expo project is not supported.