Class: CFBundle::Storage::Zip

Inherits:
Base
  • Object
show all
Defined in:
lib/cfbundle/storage/zip.rb

Overview

A bundle storage that reads from a ZIP archive.

Instance Method Summary collapse

Constructor Details

#initialize(zip, path, skip_close: false) ⇒ Zip



12
13
14
15
16
# File 'lib/cfbundle/storage/zip.rb', line 12

def initialize(zip, path, skip_close: false)
  @zip = zip
  @root = path
  @skip_close = skip_close
end

Instance Method Details

#closevoid

This method returns an undefined value.

Invoked when the storage is no longer needed.

This method closes the underlying Zip file unless the storage was initialized with skip_close: true.



58
59
60
# File 'lib/cfbundle/storage/zip.rb', line 58

def close
  @zip.close unless @skip_close
end

#directory?(path) ⇒ Object

Returns whether a given directory exists within the storage.



30
31
32
33
# File 'lib/cfbundle/storage/zip.rb', line 30

def directory?(path)
  entry = find(path)
  !entry.nil? && entry.directory?
end

#exist?(path) ⇒ Object

Returns whether a given path exists within the storage.



19
20
21
# File 'lib/cfbundle/storage/zip.rb', line 19

def exist?(path)
  find(path) != nil
end

#file?(path) ⇒ Object

Returns whether a given file exists within the storage.



24
25
26
27
# File 'lib/cfbundle/storage/zip.rb', line 24

def file?(path)
  entry = find(path)
  !entry.nil? && entry.file?
end

#foreach(path) ⇒ Enumerator

Returns an enumerator that enumerates the files contained in a directory.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cfbundle/storage/zip.rb', line 41

def foreach(path)
  Enumerator.new do |y|
    directory = find! path
    base = @zip.entries.sort.each
    loop do
      entry = base.next
      next unless entry.parent_as_string == directory.name
      y << PathUtils.join(path, File.basename(entry.name))
    end
  end
end

#open(path) {|file| ... } ⇒ Object, IO

Opens a file for reading in the storage.

Yield Parameters:

  • file (IO)

    The opened file. It is automatically closed when the block terminates.



36
37
38
# File 'lib/cfbundle/storage/zip.rb', line 36

def open(path, &block)
  find!(path).get_input_stream(&block)
end