user2840647
user2840647

Reputation: 1314

Ruby - Listing files in a tar.gz archive

I need to list the files in a .tar.gz archive, without uncompressing the archive. I looked into the zlib library, but I could not find a way to list the files, only show the contents of the file.

How can I list only the files in the archive?

This is the code I have:

require 'zlib'

Zlib::GzipReader.open("archive.tar.gz") do |entry|
  entry.each { |e| # what's the method? }
end

Upvotes: 4

Views: 639

Answers (1)

BMW
BMW

Reputation: 45353

require 'rubygems/package'
require 'zlib'
tar_extract = Gem::Package::TarReader.new(Zlib::GzipReader.open('archive.tar.gz'))
tar_extract.rewind # The extract has to be rewinded after every iteration
tar_extract.each do |entry|
   if entry.file?
       puts entry.full_name
   end
end
tar_extract.close

Upvotes: 5

Related Questions