Skip to content

Instantly share code, notes, and snippets.

@iwan
Last active April 6, 2016 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iwan/d37123d9037e7cb9035ccb77e446833f to your computer and use it in GitHub Desktop.
Save iwan/d37123d9037e7cb9035ccb77e446833f to your computer and use it in GitHub Desktop.
Compress all *.json files of a directory using tar and gzip
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