Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.0.0+

Create a Zip Entirely in Memory

See more Zip Examples

Demonstrates how to create a .zip from in-memory byte data and strings, and to write the .zip to an in-memory image.

Chilkat Dart Downloads

Dart
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 crypt = CkCrypt2();
  final zip = CkZip();

  try {
    zip.newZip('test.zip');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Add the bytes 0x00 0x01 0x02 0x03 ... 0x0F as a file in the .zip
  final bd = CkBinData();
  bd.appendEncoded('000102030405060708090A0B0C0D0E0F', 'hex');
  zip.addBd('binaryData.dat', bd);

  // Add the string "Hello World!" to the .zip
  zip.addString('helloWorld.txt', 'Hello World!', 'utf-8');

  final zipFileInMemory = zip.writeToMemory();

  // We could save these files to a file, and it is a valid .zip
  final fac = CkFileAccess();
  try {
    fac.writeEntireFile('test.zip', zipFileInMemory);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Zip Created!');
}