Dart
Dart
Compress and Encrypt a Large File (Low and Constant Memory Footprint)
See more Compression Examples
Demonstrates how to compress and encrypt a large file such that the memory footprint remains low and constant.Note: This example requires Chilkat v9.5.0.99 or greater.
Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
final compress = CkCompression();
compress.algorithm = 'deflate';
// Set encryption params.
// The possible values are the same as for the corresponding properties in the Chilkat Crypt2 class/object.
// The encoded IV and Key must be specified as hex.
final json = CkJsonObject();
json.updateString('cryptAlgorithm', 'aes');
json.updateString('cipherMode', 'cbc');
json.updateInt('keyLength', 128);
json.updateInt('paddingScheme', 0);
json.updateString('encodedIV', '000102030405060708090A0B0C0D0E0F');
json.updateString('encodedKey', '000102030405060708090A0B0C0D0E0F');
// Do file-to-file compression+encryption in a single call.
final inPath = 'qa_data/largeFile.dat';
final outPath = 'c:/temp/qa_output/compressed_encrypted.dat';
try {
compress.compressEncryptFile(json, inPath, outPath);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// We can do file-to-file decrypt/decompress like this:
final inPath2 = outPath;
final outPath2 = 'c:/temp/qa_output/restored.dat';
try {
compress.decryptDecompressFile(json, inPath2, outPath2);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Note: The above decrypt + decompress is the equivalent of doing the same in these two steps:
final crypt = CkCrypt2();
crypt.cryptAlgorithm = 'aes';
crypt.cipherMode = 'cbc';
crypt.keyLength = 128;
crypt.paddingScheme = 0;
crypt.setEncodedIV('000102030405060708090A0B0C0D0E0F', 'hex');
crypt.setEncodedKey('000102030405060708090A0B0C0D0E0F', 'hex');
final decryptedPath = 'c:/temp/qa_output/decrypted.dat';
try {
crypt.ckDecryptFile(inPath2, decryptedPath);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final outPath3 = 'c:/temp/qa_output/restored_in_two_steps.dat';
try {
compress.decompressFile(decryptedPath, outPath3);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('Success.');
}