Class: MingleEvents::ZipDirectory

Inherits:
Object
  • Object
show all
Defined in:
lib/mingle_events/zip_directory.rb

Defined Under Namespace

Classes: ReusableEntryStream

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ ZipDirectory

Returns a new instance of ZipDirectory.



6
7
8
9
10
11
# File 'lib/mingle_events/zip_directory.rb', line 6

def initialize(name)
  FileUtils.mkdir_p(File.dirname(name))
  @root = name
  @entry_map = nil
  @readio = nil
end

Instance Method Details

#closeObject Also known as: reload



44
45
46
47
48
# File 'lib/mingle_events/zip_directory.rb', line 44

def close
  @readio.close if @readio
  @readio = nil
  @entry_map = nil
end

#deleteObject



39
40
41
42
# File 'lib/mingle_events/zip_directory.rb', line 39

def delete
  close
  FileUtils.rm_rf(@root)
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/mingle_events/zip_directory.rb', line 34

def exists?(path)
  return unless File.exist?(@root)
  entry_map.include?(path)
end

#file(path, &block) ⇒ Object



27
28
29
30
31
32
# File 'lib/mingle_events/zip_directory.rb', line 27

def file(path, &block)
  measure("read file") do
    raise FileNotExist, "File at '#{path}' in archive '#{@root}' dose not exisits" unless exists?(path)
    entry_map[path].open { |entry_stream| yield(entry_stream) }
  end
end

#write_file(path, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mingle_events/zip_directory.rb', line 13

def write_file(path, &block)
  measure('write_file') do
    exists = File.exist?(@root) && File.lstat(@root).size > 1024
    writeio = File.open(@root, exists ? 'r+b' : 'wb')
    # for a existing tar, seek to -1024 from end to skip 1024 '\0' in tar format
    writeio.seek(-1024, IO::SEEK_END) if exists

    Archive::Tar::Minitar::Output.open(writeio) do |output|
      stat = {:mode => 0100644, :mtime => Time.now}
      output.tar.add_file(path, stat) { |io, opts| yield(io) }
    end
  end
end