Skip to content

Instantly share code, notes, and snippets.

@dharshan
Last active December 13, 2022 13:30
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 dharshan/262a93f8306712a38dd99575ed8fd094 to your computer and use it in GitHub Desktop.
Save dharshan/262a93f8306712a38dd99575ed8fd094 to your computer and use it in GitHub Desktop.
Ruby zip a folder - rubyzip
require 'zip'
input_directory = '' # directory to be zipped
zipfile_name = '' # zip-file name
# zip a folder with only files (NO SUB FOLDERS)
Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
Dir[File.join(input_directory, '*')].each do |file|
zipfile.add(file.sub(input_directory, ''), file)
end
end
# zip a folder with files and subfolders
Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
Dir["#{input_directory}/**/**"].each do |file|
zipfile.add(file.sub(input_directory + '/', ''), file)
end
end
@romanbaitaliuk
Copy link

Hi,
This script creates empty zip file in my case? Is there any updates on this?

@dharshan
Copy link
Author

did you set your directory path to input_directory ?

@romanbaitaliuk
Copy link

Found the issue, thanks:)

@sbonifazzi
Copy link

Found the issue, thanks:)

Hi @romanbaitaliuk , I have the same issue, please can you let me know what was the issue?
Many thank for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment