Dart
Dart
Add Text Files to a ZIP Using AddString
See more Zip Examples
This example demonstrates how to use the AddString method to add multiple text files directly from string variables into a ZIP archive.
Each string is converted to bytes using the specified character encoding and stored as a separate file entry within the ZIP archive.
This method is useful for dynamically generating small text files such as configuration files, reports, JSON documents, XML, or log files entirely in memory.
Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
final zip = CkZip();
try {
zip.newZip('stringEntries.zip');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Add a README text file.
final readmeText = 'This ZIP archive was created using AddString.';
try {
zip.addString('docs/readme.txt', readmeText, 'utf-8');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Add a JSON configuration file.
final jsonText = '{ "server": "example.com", "port": 443 }';
try {
zip.addString('config/settings.json', jsonText, 'utf-8');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Add a small XML document.
final xmlText = '<root><status>OK</status></root>';
try {
zip.addString('xml/status.xml', xmlText, 'utf-8');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Write the ZIP archive to disk and close it.
try {
zip.writeZipAndClose();
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('ZIP archive created successfully.');
}