Sample code for 30+ languages & platforms
Android™

Enable FTP MODE Z Compression

See more FTP Examples

Demonstrates the Chilkat Ftp2.SetModeZ method, which enables MODE Z compression for subsequent data connections. It takes no arguments. Call it after connecting and only when the HasModeZ property is true.

Background: MODE Z compresses data on the fly (using zlib) as it crosses the wire, which can meaningfully speed up transfers of compressible content — text, HTML, logs — over a slow link. It is a server extension, so check HasModeZ first and expect it to be unavailable on many servers. There is little benefit for already-compressed data such as images or archives, where the CPU cost of compression buys almost nothing.

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;

    //  Demonstrates the Ftp2.SetModeZ method, which enables MODE Z compression for subsequent data
    //  connections.  It takes no arguments.  Call it after connecting, and only when the HasModeZ
    //  property indicates the server supports it.

    CkFtp2 ftp = new CkFtp2();

    ftp.put_Hostname("ftp.example.com");
    ftp.put_Username("myFtpLogin");

    //  Normally you would not hard-code the password in source.  You should instead obtain it
    //  from an interactive prompt, environment variable, or a secrets vault.
    ftp.put_Password("myPassword");

    success = ftp.Connect();
    if (success == false) {
        Log.i(TAG, ftp.lastErrorText());
        return;
        }

    success = ftp.ChangeRemoteDir("public_html");
    if (success == false) {
        Log.i(TAG, ftp.lastErrorText());
        return;
        }

    //  Only enable compression if the server advertises MODE Z support.
    if (ftp.get_HasModeZ()) {
        success = ftp.SetModeZ();
        if (success == false) {
            Log.i(TAG, ftp.lastErrorText());
            return;
            }

        Log.i(TAG, "MODE Z compression enabled.");
        }
    else {
        Log.i(TAG, "The server does not support MODE Z.");
        }

    //  Subsequent uploads, downloads, and listings are transferred compressed.

    success = ftp.Disconnect();
    if (success == false) {
        Log.i(TAG, ftp.lastErrorText());
        return;
        }


  }

  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."
  }
}