Sample code for 30+ languages & platforms
Dart

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 Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // This requires the Chilkat Zip API to have been previously unlocked.
  // See Unlock Chilkat Zip for sample code.

  final zip = CkZip();

  // Open an unencrypted .zip
  try {
    zip.openZip('qa_data/zips/test.zip');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Unzip to a temp directory.
  final numFilesUnzipped = zip.unzip('qa_output/tmp');
  if (numFilesUnzipped < 0) {
    print(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 {
    zip.appendFiles('*.*', true);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Write the .zip and close it.
  try {
    zip.writeZipAndClose();
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Success.');
}