Last active
April 6, 2016 07:40
-
-
Save iwan/d37123d9037e7cb9035ccb77e446833f to your computer and use it in GitHub Desktop.
Compress all *.json files of a directory using tar and gzip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems/package' | |
def tar_gz_squeeze(dirname, filename: nil) | |
filename = filename || dirname | |
File.open("#{filename}.tar.gz", "wb") do |file| | |
Zlib::GzipWriter.wrap(file) do |gz| | |
Gem::Package::TarWriter.new(gz) do |tar| | |
Dir.glob("#{dirname}/*.json").each do |f| | |
puts "adding #{File.basename(f)}..." | |
content = File.read(f, mode: 'rb') | |
tar.add_file_simple(File.basename(f), 0444, content.size) do |io| | |
io.write(content) | |
end | |
end | |
end | |
end | |
end | |
end | |
tar_gz_squeeze("files") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment