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.



4
5
6
7
8
9
# File 'lib/mingle_events/zip_directory.rb', line 4

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



42
43
44
45
46
# File 'lib/mingle_events/zip_directory.rb', line 42

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

#deleteObject



37
38
39
40
# File 'lib/mingle_events/zip_directory.rb', line 37

def delete
  close
  FileUtils.rm_rf(@root)
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/mingle_events/zip_directory.rb', line 32

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

#file(path, &block) ⇒ Object



25
26
27
28
29
30
# File 'lib/mingle_events/zip_directory.rb', line 25

def file(path, &block)
  measure("read file") do
    raise "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



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

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