Sample code for 30+ languages & platforms
Unicode C

Co:Z SFTP Binary File Download (from z/OS IBM Mainframe)

See more SFTP Examples

Demonstrates how to download a binary file, such as a .zip, from a Co:Z SFTP server on a z/OS IBM Mainframe.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkSFtpW.h>
#include <C_CkSFtpDirW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSFtpW sftp;
    const wchar_t *hostname;
    int port;
    const wchar_t *handle;
    HCkSFtpDirW dirListing;
    const wchar_t *localFilePath;
    const wchar_t *remoteFilePath;

    success = FALSE;

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

    sftp = CkSFtpW_Create();

    // Connect to the SSH server.  
    hostname = L"sftp.example.com";
    port = 22;
    success = CkSFtpW_Connect(sftp,hostname,port);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
        CkSFtpW_Dispose(sftp);
        return;
    }

    success = CkSFtpW_AuthenticatePw(sftp,L"myLogin",L"myPassword");
    if (success == FALSE) {
        wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
        CkSFtpW_Dispose(sftp);
        return;
    }

    success = CkSFtpW_InitializeSftp(sftp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
        CkSFtpW_Dispose(sftp);
        return;
    }

    // To download a binary file from the Co:Z SFTP server, 
    // we must switch to binary mode in the following unconventional way.
    // We pretend to fetch a directory listing for "/+mode=binary"
    // This has the effect of putting the server in binary mode for transfers.
    handle = CkSFtpW_openDir(sftp,L"/+mode=binary");
    if (CkSFtpW_getLastMethodSuccess(sftp) == FALSE) {
        wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
        CkSFtpW_Dispose(sftp);
        return;
    }

    // Download the "directory listing" (but it's not actually a directory listing, and we'll just discard it.)

    dirListing = CkSFtpDirW_Create();
    success = CkSFtpW_ReadDirListing(sftp,handle,dirListing);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
        CkSFtpW_Dispose(sftp);
        CkSFtpDirW_Dispose(dirListing);
        return;
    }

    // Close the directory handle:
    success = CkSFtpW_CloseHandle(sftp,handle);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
        CkSFtpW_Dispose(sftp);
        CkSFtpDirW_Dispose(dirListing);
        return;
    }

    // Download the binary file:
    localFilePath = L"c:/temp/test.zip";
    remoteFilePath = L"test.zip";
    success = CkSFtpW_DownloadFileByName(sftp,remoteFilePath,localFilePath);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
        CkSFtpW_Dispose(sftp);
        CkSFtpDirW_Dispose(dirListing);
        return;
    }

    wprintf(L"Success.\n");


    CkSFtpW_Dispose(sftp);
    CkSFtpDirW_Dispose(dirListing);

    }