Skip to content

Instantly share code, notes, and snippets.

@g13013
Forked from petems/Gemfile
Last active August 29, 2015 14:21
Show Gist options
  • Save g13013/1c8f4020ec16bf850ef1 to your computer and use it in GitHub Desktop.
Save g13013/1c8f4020ec16bf850ef1 to your computer and use it in GitHub Desktop.
#from http://stackoverflow.com/questions/2301009/get-file-size-before-downloading-counting-how-much-already-downloaded-httpru
require 'net/http'
require 'uri'
require 'progressbar'
url = "http://ipv4.download.thinkbroadband.com/5MB.zip"
url_base = url.split('/')[2]
url_path = '/'+url.split('/')[3..-1].join('/')
@counter = 0
Net::HTTP.start(url_base) do |http|
response = http.request_head(URI.escape(url_path))
ProgressBar
pbar = ProgressBar.new("file name:", response['content-length'].to_i)
File.open("test.file", 'w') {|f|
http.get(URI.escape(url_path)) do |str|
f.write str
@counter += str.length
pbar.set(@counter)
end
}
pbar.finish
end
puts "Done."
source "https://rubygems.org"
gem "progressbar"
$ bundle exec ruby download_progress.rb
file name:: 100% |oooooooooooooooooooooooooooooooooooooooooooo| Time: 0:00:00
Done.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment