Created
March 20, 2023 16:47
-
-
Save sephraim/cce902f67e64b6d48dcb08e3f7fe357f to your computer and use it in GitHub Desktop.
[Create a zip file]
This file contains hidden or 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
# 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