Module: CFoundry::Zip

Defined in:
lib/cfoundry/zip.rb

Overview

Generic Zpi API. Uses rubyzip underneath, but may be changed in the future to use system zip command if necessary.

Constant Summary collapse

PACK_EXCLUSION_GLOBS =

Directory entries to exclude from packing.

['..', '.', '*~', '#*#', '*.log']

Class Method Summary collapse

Class Method Details

.entry_lines(file) ⇒ Object

Get the entries in the zip file. Returns an array of the entire contents, recursively (not just top-level).



14
15
16
17
18
19
20
# File 'lib/cfoundry/zip.rb', line 14

def entry_lines(file)
  entries = []
  ::Zip::ZipFile.foreach(file) do |zentry|
    entries << zentry
  end
  entries
end

.files_to_pack(dir) ⇒ Object

Determine what files in dir to pack.



33
34
35
36
37
38
39
40
# File 'lib/cfoundry/zip.rb', line 33

def files_to_pack(dir)
  Dir.glob("#{dir}/**/*", File::FNM_DOTMATCH).select do |f|
    File.exists?(f) &&
      PACK_EXCLUSION_GLOBS.none? do |e|
        File.fnmatch(e, File.basename(f))
      end
  end
end

.pack(dir, zipfile) ⇒ Object

Package directory dir as file zipfile.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cfoundry/zip.rb', line 43

def pack(dir, zipfile)
  files = files_to_pack(dir)
  return false if files.empty?

  ::Zip::ZipFile.open(zipfile, true) do |zf|
    files.each do |f|
      zf.add(f.sub("#{dir}/",''), f)
    end
  end

  true
end

.unpack(file, dest) ⇒ Object

Unpack a zip file to directory dest.



23
24
25
26
27
28
29
30
# File 'lib/cfoundry/zip.rb', line 23

def unpack(file, dest)
  ::Zip::ZipFile.foreach(file) do |zentry|
    epath = "#{dest}/#{zentry}"
    dirname = File.dirname(epath)
    FileUtils.mkdir_p(dirname) unless File.exists?(dirname)
    zentry.extract(epath) unless File.exists?(epath)
  end
end