Android™
Android™
List a Remote Directory on an SFTP Server
See more SFTP Examples
Demonstrates the Chilkat SFtp.ReadDirListing method, which reads all remaining entries from an open directory handle into an SFtpDir object. The first argument is the handle returned by OpenDir and the second is the SFtpDir that receives the listing. The example iterates the entries with NumFilesAndDirs and FileAt, reading each SFtpFile's Filename, IsDirectory, Size64, and LastModifiedTimeStr.
Background: Listing a directory is a three-part operation:
OpenDir gets a handle, ReadDirListing pulls every entry into an SFtpDir in one call, and the handle is then closed. Each entry is an SFtpFile carrying that item's name, type, size, timestamps, owner, and permission flags — a single stat covering what several separate GetFile* calls would return. The entries come back in the server's own order rather than sorted; use SFtpDir.Sort if you need a particular ordering. Note that a listing normally includes the . and .. entries, which most code skips.Chilkat Android™ Downloads
// 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 SFtp.ReadDirListing method, which reads all remaining entries from an open
// directory handle into an SFtpDir object. The 1st argument is the handle (from OpenDir) and
// the 2nd is the SFtpDir that receives the listing.
CkSFtp sftp = new CkSFtp();
// Connect, authenticate, and initialize the SFTP subsystem.
int port = 22;
success = sftp.Connect("sftp.example.com",port);
if (success == false) {
Log.i(TAG, sftp.lastErrorText());
return;
}
// 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.
String password = "mySshPassword";
success = sftp.AuthenticatePw("mySshLogin",password);
if (success == false) {
Log.i(TAG, sftp.lastErrorText());
return;
}
success = sftp.InitializeSftp();
if (success == false) {
Log.i(TAG, sftp.lastErrorText());
return;
}
// Open the remote directory to be listed. OpenDir returns a handle string.
String handle = sftp.openDir("subdir");
if (sftp.get_LastMethodSuccess() == false) {
Log.i(TAG, sftp.lastErrorText());
return;
}
// Read all directory entries into an SFtpDir object.
CkSFtpDir dirListing = new CkSFtpDir();
success = sftp.ReadDirListing(handle,dirListing);
if (success == false) {
Log.i(TAG, sftp.lastErrorText());
return;
}
// The listing has been read, so the directory handle can be closed now.
success = sftp.CloseHandle(handle);
if (success == false) {
Log.i(TAG, sftp.lastErrorText());
return;
}
// Iterate the entries. Entries are in server order (Chilkat does not sort the listing).
int n = dirListing.get_NumFilesAndDirs();
Log.i(TAG, "Directory contains " + String.valueOf(n) + " entries:");
CkSFtpFile fileObj = new CkSFtpFile();
int i;
for (i = 0; i <= n - 1; i++) {
// FileAt fills the SFtpFile object with the entry at the given index.
success = dirListing.FileAt(i,fileObj);
if (success == false) {
Log.i(TAG, dirListing.lastErrorText());
return;
}
if (fileObj.get_IsDirectory()) {
Log.i(TAG, " [DIR] " + fileObj.filename());
}
else {
// Size64 is a 64-bit size; LastModifiedTimeStr is the modification time as text.
Log.i(TAG, " " + fileObj.filename() + " (" + String.valueOf(fileObj.get_Size64()) + " bytes, modified " + fileObj.lastModifiedTimeStr() + ")");
}
}
sftp.Disconnect();
}
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."
}
}