Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unzip (zip, tar, tag.gz) files with ruby

I want to unzip a lot of zip files. Is there a module or script that checks which format the zip file is and decompresses it? This should work on Linux, I don't care about other OSs.

like image 257
gustavgans Avatar asked May 13 '09 09:05

gustavgans


People also ask

How do I unzip a tar gz file?

Simply right-click the item you want to compress, mouseover compress, and choose tar. gz. You can also right-click a tar. gz file, mouseover extract, and select an option to unpack the archive.

Can 7zip unzip tar gz?

gz File by 7-Zip. Go to your . tar. gz file location, right-click on it, and choose Extract files option.

How do I unzip a TXT gz file in Linux?

You can unzip GZ files in Linux by adding the -d flag to the Gzip/Gunzip command. All the same flags we used above can be applied. The GZ file will be removed by default after we uncompressed it unless we use the -k flag. Below we will unzip the GZ files we compressed in the same directory.


1 Answers

To extract files from a .tar.gz file you can use the following methods from packages distributed with Ruby:

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

Each entry of type Gem::Package::TarReader::Entry points to a file or directory within the .tar.gz file.

Similar code can be used (replace Reader with Writer) to write files to a .tar.gz file.

like image 177
Florian Feldhaus Avatar answered Sep 23 '22 21:09

Florian Feldhaus