Lua Requires Chilkat v11.0.0+
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local sftp = chilkat.SFtp.new()
-- Connect to the SSH server.
local hostname = "sftp.example.com"
local port = 22
local success = sftp:connect(hostname, port)
if not success then
print(sftp.last_error_text)
return
end
success = sftp:authenticate_pw("myLogin", "myPassword")
if not success then
print(sftp.last_error_text)
return
end
success = sftp:initialize_sftp()
if not success then
print(sftp.last_error_text)
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.
local handle = sftp:open_dir("/+mode=binary")
if not sftp.last_method_success then
print(sftp.last_error_text)
return
end
-- Download the "directory listing" (but it's not actually a directory listing, and we'll just discard it.)
local dirListing = chilkat.SFtpDir.new()
success = sftp:read_dir_listing(handle, dirListing)
if not success then
print(sftp.last_error_text)
return
end
-- Close the directory handle:
success = sftp:close_handle(handle)
if not success then
print(sftp.last_error_text)
return
end
-- Download the binary file:
local localFilePath = "c:/temp/test.zip"
local remoteFilePath = "test.zip"
success = sftp:download_file_by_name(remoteFilePath, localFilePath)
if not success then
print(sftp.last_error_text)
return
end
print("Success.")