Module: Zip

Defined in:
lib/apps/zip.rb

Class Method Summary collapse

Class Method Details

.export(zip_file, destination) ⇒ Object

exports a zip file to a destination directory zip_file full path to a zip file to be exported destination directory where the zip file contents are to be placed



12
13
14
15
16
17
# File 'lib/apps/zip.rb', line 12

def self.export(zip_file, destination)
  raise "#{zip_file} does not exist." unless ::File.exist?(zip_file)

  unzip(zip_file, destination) unless Dir.exist?(destination)
  sleep(0.5) # I guess we need to give the OS some time to get things in order?
end

.publish(source_dir, destination, source_filelist = FileList.new("**/*")) ⇒ Object

publish a directory to a file path source_dir is the directory with the files to be published destination is the zip file path source_glob is a string or array of glob directives to specify files in source_dir to be publish source_glob defaults to ‘*/’ to publish all files in the source_dir



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/apps/zip.rb', line 24

def self.publish(source_dir, destination, source_filelist = FileList.new("**/*"))
  Dir.mktmpdir do |dir| # Build zip file locally
    tmp_file_name = "#{dir}/#{::File.basename(destination)}"

    zip(source_dir, source_filelist, tmp_file_name)

    destination_dir = ::File.dirname(destination)
    FileUtils.mkpath(destination_dir) unless Dir.exist?(destination_dir)

    FileUtils.cp(tmp_file_name, destination)
  end
end

.unzip(zip_file, destination) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/apps/zip.rb', line 48

def self.unzip(zip_file, destination)
  FileUtils.mkpath(destination) unless Dir.exist?(destination)
  Zip::File.open(zip_file) do |files|
    files.each do |entry|
      destination_file = "#{destination}/#{entry.name}"

      directory = ::File.dirname(destination_file)
      FileUtils.mkpath(directory) unless Dir.exist?(directory)

      entry.extract(destination_file)
    end
  end
end

.zip(base_directory, files_to_archive, zip_file) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/apps/zip.rb', line 37

def self.zip(base_directory, files_to_archive, zip_file)
  FileUtils.mkpath(::File.dirname(zip_file)) unless Dir.exist?(::File.dirname(zip_file))
  io = Zip::File.open(zip_file, Zip::File::CREATE)

  files_to_archive.each do |file|
    io.get_output_stream(file) { |f| f.puts(::File.open("#{base_directory}/#{file}", "rb").read) }
  end

  io.close
end