Reading from tar.gz file without saving the unpacked version
I have a tar.gz file saved to disk and I want to leave it there, but I need to open one file in the archive, read it, and save some information somewhere.
File structure:
base_folder
file_i_need.txt
other_folder
other_file
code (it's not much - I've tried 10mio in different ways and this is what's left)
def self.open_file(file)
uncompressed_file = Gem::Package::TarReader.new(Zlib::GzipReader.open(file))
uncompressed_file.rewind
end
When I run it in the console, I get
<Gem::Package::TarReader:0x007fbaac178090>
and I can run commands for entries. I just didn't figure out how to open the entry and read it without saving it without decompressing it to disk. I basically want a line from a text file.
Any help is appreciated. I might just be missing something ...
source to share
TarReader
Enumerable
returning Entry
.
However, to get the text content from a file by its name, you can
uncompressed = Gem::Package::TarReader.new(Zlib::GzipReader.open(file))
text = uncompressed.detect do |f|
f.fullname == 'base_folder/file_i_need.txt'
end.read
#ā Hello, Iām content of the text file, located inside gzipped tar
Hope it helps.
source to share