Class: Liri::Common::Compressor::Zip

Inherits:
Object
  • Object
show all
Defined in:
lib/common/compressor/zip.rb

Instance Method Summary collapse

Constructor Details

#initialize(input_dir, output_file, ignored_folders) ⇒ Zip

Inicializa la carpeta a comprimir y la ubicación en donde se guardará el archivo comprimido



11
12
13
14
15
# File 'lib/common/compressor/zip.rb', line 11

def initialize(input_dir, output_file, ignored_folders)
  @input_dir = input_dir
  @output_file = output_file
  @ignored_folders = ignored_folders.split(",")
end

Instance Method Details

#compressObject

Comprime el directorio de entrada @input_dir en un archivo con extensión zip.



18
19
20
21
22
23
24
25
26
# File 'lib/common/compressor/zip.rb', line 18

def compress
  clear_output_file
  entries = Dir.entries(@input_dir) - (%w[. ..] + @ignored_folders)

  ::Zip::File.open(@output_file, ::Zip::File::CREATE) do |zipfile|
    write_entries(entries, '', zipfile)
  end
  true
end

#decompress(zip_dir, dir_destination) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/common/compressor/zip.rb', line 28

def decompress(zip_dir, dir_destination)
  FileUtils.mkdir_p(dir_destination)
  ::Zip::File.open(File.expand_path(zip_dir)) do |zip_file|
    zip_file.each do |f|
      fpath = File.join(dir_destination, f.name)
      FileUtils.mkdir_p(File.dirname(fpath))
      zip_file.extract(f, fpath) unless File.exist?(fpath)
    end
  end
end