Skip to content

Instantly share code, notes, and snippets.

@sephraim
Created March 20, 2023 16:47
Show Gist options
  • Save sephraim/cce902f67e64b6d48dcb08e3f7fe357f to your computer and use it in GitHub Desktop.
Save sephraim/cce902f67e64b6d48dcb08e3f7fe357f to your computer and use it in GitHub Desktop.
[Create a zip file]
# Zip file
#
# @private
#
# @return [String] path to file
def zip_file
zfilename ||= 'example.zip'
zfile ||= Tempfile.new(zfilename) # you can also create a non-temp file so that it's not in the tmp directory
Zip::File.open(zfile.path, Zip::File::CREATE) do |zip|
['path/to/file1', 'path/to/file2'].each do |filepath|
next unless File.exist?(filepath)
zip.add(File.basename(filepath), filepath)
end
end
zip_file.close # this will close the file but won't delete it; use .close! to delete
zip_file.path
end
# Zip file data
#
# @private
#
# @return [String]
def zip_data
@zip_data ||= File.read(zip_file.path)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment