Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSftp
    String sHostname
    Integer iPort
    String sHandle
    Variant vDirListing
    Handle hoDirListing
    String sLocalFilePath
    String sRemoteFilePath
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatSFtp)) To hoSftp
    If (Not(IsComObjectCreated(hoSftp))) Begin
        Send CreateComObject of hoSftp
    End

    // Connect to the SSH server.  
    Move "sftp.example.com" To sHostname
    Move 22 To iPort
    Get ComConnect Of hoSftp sHostname iPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComAuthenticatePw Of hoSftp "myLogin" "myPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComInitializeSftp Of hoSftp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // 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.
    Get ComOpenDir Of hoSftp "/+mode=binary" To sHandle
    Get ComLastMethodSuccess Of hoSftp To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

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

    Get Create (RefClass(cComChilkatSFtpDir)) To hoDirListing
    If (Not(IsComObjectCreated(hoDirListing))) Begin
        Send CreateComObject of hoDirListing
    End
    Get pvComObject of hoDirListing to vDirListing
    Get ComReadDirListing Of hoSftp sHandle vDirListing To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Close the directory handle:
    Get ComCloseHandle Of hoSftp sHandle To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Download the binary file:
    Move "c:/temp/test.zip" To sLocalFilePath
    Move "test.zip" To sRemoteFilePath
    Get ComDownloadFileByName Of hoSftp sRemoteFilePath sLocalFilePath To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Success."


End_Procedure