React Native Requires Chilkat v11.0.0+
React Native
Replace/Update a FIle in a .zip
See more Zip Examples
Demonstrates how to replace/update a file from a .zip. Note: This requires the entire .zip to be rewritten.Chilkat React Native Downloads
import { Zip, ZipEntry } from '@chilkat/react-native'
async function chilkatExample() {
// This requires the Chilkat Zip API to have been previously unlocked.
// See Unlock Chilkat Zip for sample code.
// First prepare a .zip and write it..
const zip = new Zip();
zip.newZip('qa_output/abc.zip');
// Add some files..
const charset = 'utf-8';
zip.addString('a.txt', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', charset);
zip.addString('b.txt', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', charset);
zip.addString('c.txt', 'cccccccccccccccccccccccccccccccccccc', charset);
// Write to qa_output/abc.zip
// This .zip contains three files: a.txt, b.txt, and c.txt
await zip.writeZipAndCloseAsync();
// -------------------------------------------------------------------
// Open abc.zip, replace the content of the "b.txt" entry with something else, and re-write.
const zip2 = new Zip();
await zip2.openZipAsync('qa_output/abc.zip');
const entry = new ZipEntry();
if (zip2.entryOf('b.txt', entry)) {
entry.replaceString('This is the new content. bbbbbbbbbbbbbbbbbbbbbb', 'utf-8');
}
// Write the modified .zip back to "abc.zip"
await zip2.writeZipAndCloseAsync();
console.log('success.');
}