React Native
React Native
Encrypt Already Existing Zip
See more Zip Examples
To encrypt an already existing non-encrypted .zip, the application must open the .zip, set the encryption related properties, and then re-write.Chilkat React Native Downloads
import { Zip } 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.
const zip = new Zip();
// Open an unencrypted .zip
try {
await zip.openZipAsync('qa_data/zips/test.zip');
} catch {
console.log(zip.lastErrorText);
return;
}
// Unzip to a temp directory.
const numFilesUnzipped = await zip.unzipAsync('qa_output/tmp');
if (numFilesUnzipped < 0) {
console.log(zip.lastErrorText);
return;
}
// Clear the zip object.
zip.newZip('qa_output/aesTest.zip');
// Indicate that 128-bit AES encryption is to be used when writing the .zip
zip.encryption = 4;
zip.encryptKeyLength = 128;
// Set the password.
zip.encryptPassword = 'secret';
// Append the files.
zip.appendFromDir = 'qa_output/tmp';
try {
await zip.appendFilesAsync('*.*', true);
} catch {
console.log(zip.lastErrorText);
return;
}
// Write the .zip and close it.
try {
await zip.writeZipAndCloseAsync();
} catch {
console.log(zip.lastErrorText);
return;
}
console.log('Success.');
}