Skip to content

Instantly share code, notes, and snippets.

@Centaur
Forked from petems/Gemfile
Last active August 29, 2015 14:28
Show Gist options
  • Save Centaur/caa2db9bc3c73994282c to your computer and use it in GitHub Desktop.
Save Centaur/caa2db9bc3c73994282c to your computer and use it in GitHub Desktop.
An example http download with Progress Bar output in the command line with Ruby and the native `net/http` library...
#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