Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

set success 0

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

set sftp [new_CkSFtp]

# Connect to the SSH server.  
set hostname "sftp.example.com"
set port 22
set success [CkSFtp_Connect $sftp $hostname $port]
if {$success == 0} then {
    puts [CkSFtp_lastErrorText $sftp]
    delete_CkSFtp $sftp
    exit
}

set success [CkSFtp_AuthenticatePw $sftp "myLogin" "myPassword"]
if {$success == 0} then {
    puts [CkSFtp_lastErrorText $sftp]
    delete_CkSFtp $sftp
    exit
}

set success [CkSFtp_InitializeSftp $sftp]
if {$success == 0} then {
    puts [CkSFtp_lastErrorText $sftp]
    delete_CkSFtp $sftp
    exit
}

# 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.
set handle [CkSFtp_openDir $sftp "/+mode=binary"]
if {[CkSFtp_get_LastMethodSuccess $sftp] == 0} then {
    puts [CkSFtp_lastErrorText $sftp]
    delete_CkSFtp $sftp
    exit
}

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

set dirListing [new_CkSFtpDir]

set success [CkSFtp_ReadDirListing $sftp $handle $dirListing]
if {$success == 0} then {
    puts [CkSFtp_lastErrorText $sftp]
    delete_CkSFtp $sftp
    delete_CkSFtpDir $dirListing
    exit
}

# Close the directory handle:
set success [CkSFtp_CloseHandle $sftp $handle]
if {$success == 0} then {
    puts [CkSFtp_lastErrorText $sftp]
    delete_CkSFtp $sftp
    delete_CkSFtpDir $dirListing
    exit
}

# Download the binary file:
set localFilePath "c:/temp/test.zip"
set remoteFilePath "test.zip"
set success [CkSFtp_DownloadFileByName $sftp $remoteFilePath $localFilePath]
if {$success == 0} then {
    puts [CkSFtp_lastErrorText $sftp]
    delete_CkSFtp $sftp
    delete_CkSFtpDir $dirListing
    exit
}

puts "Success."

delete_CkSFtp $sftp
delete_CkSFtpDir $dirListing