Xbase++ Requires Chilkat v11.0.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oSftp
LOCAL cHostname
LOCAL nPort
LOCAL cHandle
LOCAL oDirListing
LOCAL cLocalFilePath
LOCAL cRemoteFilePath
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oSftp := CreateObject("Chilkat.SFtp")
// Connect to the SSH server.
cHostname := "sftp.example.com"
nPort := 22
nSuccess := oSftp:Connect(cHostname, nPort)
IF (nSuccess == 0)
? oSftp:LastErrorText
oSftp:destroy()
RETURN
ENDIF
nSuccess := oSftp:AuthenticatePw("myLogin", "myPassword")
IF (nSuccess == 0)
? oSftp:LastErrorText
oSftp:destroy()
RETURN
ENDIF
nSuccess := oSftp:InitializeSftp()
IF (nSuccess == 0)
? oSftp:LastErrorText
oSftp:destroy()
RETURN
ENDIF
// 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.
cHandle := oSftp:OpenDir("/+mode=binary")
IF (oSftp:LastMethodSuccess == 0)
? oSftp:LastErrorText
oSftp:destroy()
RETURN
ENDIF
// Download the "directory listing" (but it's not actually a directory listing, and we'll just discard it.)
oDirListing := CreateObject("Chilkat.SFtpDir")
nSuccess := oSftp:ReadDirListing(cHandle, oDirListing)
IF (nSuccess == 0)
? oSftp:LastErrorText
oSftp:destroy()
oDirListing:destroy()
RETURN
ENDIF
// Close the directory handle:
nSuccess := oSftp:CloseHandle(cHandle)
IF (nSuccess == 0)
? oSftp:LastErrorText
oSftp:destroy()
oDirListing:destroy()
RETURN
ENDIF
// Download the binary file:
cLocalFilePath := "c:/temp/test.zip"
cRemoteFilePath := "test.zip"
nSuccess := oSftp:DownloadFileByName(cRemoteFilePath, cLocalFilePath)
IF (nSuccess == 0)
? oSftp:LastErrorText
oSftp:destroy()
oDirListing:destroy()
RETURN
ENDIF
? "Success."
oSftp:destroy()
oDirListing:destroy()