Class: JScrambler::Archiver

Inherits:
Object
  • Object
show all
Defined in:
lib/jscrambler/archiver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#zipfileObject

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, options={})
  raise JScrambler::InvalidPath, 'When unzipping a file a destination path is required' unless File.directory?(to_path.to_s)

  options[: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) && options[: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|
      expanded_glob = Dir.glob(path)
      common_path = common_path(expanded_glob + [path])
      expanded_glob.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