Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby - Listing files in a tar.gz archive

Tags:

ruby

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
like image 292
user2840647 Avatar asked Sep 12 '25 07:09

user2840647


1 Answers

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
like image 86
BMW Avatar answered Sep 14 '25 21:09

BMW