Created
October 24, 2012 22:32
-
-
Save pablasso/3949372 to your computer and use it in GitHub Desktop.
Net::FTP and UTF-8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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