Dart Requires Chilkat v11.0.0+
Dart
Creates an AES Encrypted Zip with One File Unencrypted
See more Zip Examples
Demonstrates how to create an AES encrypted zip, but also containing one file that is not encrypted. The way we do it is to first create an AES encrypted zip in the usual way, and then we append an unecrypted file to it.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
final zip = CkZip();
try {
zip.newZip('qa_output/aes_with_one_unencrypted.zip');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Set properties to indicate that the Zip should be
// AES encrypted.
// A value of 4 indicates WinZip compatible AES encryption.
zip.encryption = 4;
// Key length can be 128, 192, or 256 bits.
zip.encryptKeyLength = 128;
// Set the password for AES encryption:
zip.encryptPassword = 'myPassword';
// Exclude the file helloWorld.txt
// This file will be added unencrypted to the .zip
final saExclusions = CkStringArray();
saExclusions.append('helloWorld.txt');
zip.setExclusions(saExclusions);
zip.verboseLogging = true;
// Add a directory tree to be zipped.
final recurse = true;
// Append from a directory relative to our current working directory.
zip.appendFromDir = 'qa_data/filesToZip';
try {
zip.appendFiles('*', recurse);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print(zip.lastErrorText);
// Writes qa_output/aes_with_one_unencrypted.zip
try {
zip.writeZipAndClose();
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// ----------------------------------------------
// At this point, we have an encrypted .zip with all
// files except for helloWorld.txt.
// We'll add helloWorld.txt (unencrypted) to the .zip we just created.
// The NewZip method only initializes the Zip object -- it does
// not create or write a .zip file.
try {
zip.newZip('notUsed.zip');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
saExclusions.clear();
zip.setExclusions(saExclusions);
// No encryption.
zip.encryption = 0;
zip.appendFromDir = 'qa_data/filesToZip';
// Add a reference to a file. This is the file that will
// be added to a already-existing .zip.
final saveExtraPath = false;
try {
zip.addFile('helloWorld.txt', saveExtraPath);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Appends the contents of the zip object to the preExisting.zip
// zip archive. preExisting.zip is opened, and the files
// referenced by this zip object are streamed in, compressed,
// and appended to the end of the archive.
try {
zip.quickAppend('qa_output/aes_with_one_unencrypted.zip');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('Success!');
}