Work with files / Archives

# gem install rubyzip
require 'zip'
require 'fileutils'

zip_file = "tmp/data.zip"
path_to = "tmp/data"

# prepare the archive to be unpacked
FileUtils.mkdir_p(File.dirname(zip_file))
Zip::File.open(zip_file, create: truedo |zip|
    zip.get_output_stream("file.txt") { |f| 
        f.write("Hello, world!\n")
    }
end

Zip::File.open(zip_file) do |zip_file|
    zip_file.each do |file|
        f_path = File.join(path_to, file.name)
        FileUtils.mkdir_p(File.dirname(f_path))
        zip_file.extract(file, f_path) unless File.exist?(f_path)
    end
end

puts Dir["#{path_to}/*"]