Class: JScrambler::Archiver
- Inherits:
-
Object
- Object
- JScrambler::Archiver
- Defined in:
- lib/jscrambler/archiver.rb
Instance Attribute Summary collapse
-
#zipfile ⇒ Object
Returns the value of attribute zipfile.
Instance Method Summary collapse
-
#initialize(zipfile = Tempfile.new(%w(jscrambler .zip))) ⇒ Archiver
constructor
A new instance of Archiver.
- #unzip(to_path, options = {}) ⇒ Object
- #zip(paths) ⇒ Object
Constructor Details
#initialize(zipfile = Tempfile.new(%w(jscrambler .zip))) ⇒ Archiver
Returns a new instance of Archiver.
8 9 10 |
# File 'lib/jscrambler/archiver.rb', line 8 def initialize(zipfile=Tempfile.new(%w(jscrambler .zip))) @zipfile = zipfile end |
Instance Attribute Details
#zipfile ⇒ Object
Returns the value of attribute zipfile.
6 7 8 |
# File 'lib/jscrambler/archiver.rb', line 6 def zipfile @zipfile end |
Instance Method Details
#unzip(to_path, options = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/jscrambler/archiver.rb', line 29 def unzip(to_path, ={}) raise JScrambler::InvalidPath, 'When unzipping a file a destination path is required' unless File.directory?(to_path.to_s) [:overwrite] ||= true files = [] Zip::File.open(zipfile.path) do |zip_file| zip_file.each do |entry| to_file_path = File.join(to_path, entry.name) files << to_file_path File.delete(to_file_path) if File.exist?(to_file_path) && [:overwrite] entry.extract(to_file_path) end end files end |
#zip(paths) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/jscrambler/archiver.rb', line 12 def zip(paths) File.delete(zipfile.path) if File.exists?(zipfile.path) Zip::File.open(zipfile.path, Zip::File::CREATE) do |zip_handler| paths.each do |path| = Dir.glob(path) common_path = common_path( + [path]) .each do |file_path| internal_file_path = file_path.gsub(File.join(common_path, ''), '') zip_handler.add(internal_file_path, file_path) end end end zipfile end |