Skip to content

Instantly share code, notes, and snippets.

@davispuh davispuh/files.rb
Created Mar 23, 2016

Embed
What would you like to do?
Show Tar archive files
#!/bin/ruby
require 'faraday'
url = 'http://domain/somefile.tar'
conn = Faraday.new
header_response = conn.head(url)
total_size = header_response.env[:response_headers]["content-length"].to_i
puts "Size: #{total_size}"
block = 512
startb = 0
endb = block * 4
loop do
break if startb > total_size
endb = total_size if endb > total_size
conn.headers = {'Range' => "bytes=#{startb}-#{endb}"}
response = conn.get(url)
data = response.body
0.upto(data.length / block - 1) do |i|
startb += block
file_start = startb
filename = data[i * block, 100].to_s
file_size = data[i * block + 124, 12].to_i(8)
startb += (file_size / block.to_f).ceil * block
endb = startb + block
puts "#{filename} : #{file_start}-#{endb} (#{file_size})"
if file_size.zero? and i.zero?
endb += block * 2
elsif not file_size.zero?
break
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.