Skip to content

Instantly share code, notes, and snippets.

@pablasso
Created October 24, 2012 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablasso/3949372 to your computer and use it in GitHub Desktop.
Save pablasso/3949372 to your computer and use it in GitHub Desktop.
Net::FTP and UTF-8
# encoding: UTF-8
Encoding.default_internal = Encoding::UTF_8
Encoding.default_external = Encoding::UTF_8
require 'net/ftp'
require 'debugger'
require 'fileutils'
module Net
class FTP
def storbinary(cmd, file, blocksize, rest_offset = nil, &block) # :yield: data
if rest_offset
file.seek(rest_offset, IO::SEEK_SET)
end
synchronize do
with_binary(true) do
@sock.set_encoding('utf-8')
conn = transfercmd(cmd)
loop do
buf = file.read(blocksize)
break if buf == nil
conn.write(buf)
yield(buf) if block
end
conn.close
voidresp
end
end
rescue Errno::EPIPE
# EPIPE, in this case, means that the data connection was unexpectedly
# terminated. Rather than just raising EPIPE to the caller, check the
# response on the control connection. If getresp doesn't raise a more
# appropriate exception, re-raise the original exception.
getresp
raise
end
def putline line
if @debug_mode
print "put: ", sanitize(line), "\n"
end
line = line + CRLF
@sock.write(line)
end
private :putline
end
end
filename = '/tmp/somefile_©_.sample'
FileUtils.touch filename
ftp = Net::FTP.new 'host'
ftp.login 'username', 'password'
ftp.debug_mode = true
ftp.put filename
ftp.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment