Sample code for 30+ languages & platforms
Android™

Zip and Keep Open

See more Zip Examples

Zip a directory tree by calling WriteZip instead of WriteZipAndClose. When WriteZip is called, the created zip is automatically opened and the zip object contains references to the compressed zip entries (mapped entries) contained within the .zip.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkZip zip = new CkZip();

    String zipPath = "c:/temp/myFiles.zip";

    // Initialize the zip object, which also sets the FileName property to the path of the zip to be created.
    zip.NewZip(zipPath);

    // Append references to files to be zipped.
    boolean recurse = true;
    success = zip.AppendFiles("c:/temp/files_to_zip/*",recurse);
    if (success == false) {
        Log.i(TAG, zip.lastErrorText());
        return;
        }

    // Write the .zip, but don't close the zip file.
    // The zip file we just created is automatically opened and the zip object
    // now contains entries that are contained within the zip.  (They are memory-mapped entries)
    success = zip.WriteZip();
    if (success == false) {
        Log.i(TAG, zip.lastErrorText());
        return;
        }

    Log.i(TAG, "Successfully created " + zipPath);

    // Let's look at what's in the .zip we just created..

    int numEntries = zip.get_NumEntries();

    CkZipEntry entry = new CkZipEntry();

    int i = 0;
    while (i < numEntries) {
        zip.EntryAt(i,entry);
        if (entry.get_IsDirectory()) {
            Log.i(TAG, String.valueOf(i) + ": " + entry.fileName() + " (directory)");
            }
        else {
            Log.i(TAG, String.valueOf(i) + ": " + entry.fileName());
            }

        i = i + 1;
        }

    // Close the zip file when finished.
    zip.CloseZip();

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}