Class: Bookbinder::FileSystem::ZipFile

Inherits:
Bookbinder::FileSystem show all
Defined in:
lib/bookbinder/file_system/zip_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ZipFile

Returns a new instance of ZipFile.



3
4
5
6
7
# File 'lib/bookbinder/file_system/zip_file.rb', line 3

def initialize(path)
  @zipfile_path = path
  @zipfile = nil
  instantiate_zipfile  if File.exists?(@zipfile_path)
end

Instance Method Details

#closeObject



76
77
78
# File 'lib/bookbinder/file_system/zip_file.rb', line 76

def close
  @zipfile.close  if @zipfile
end

#each(&blk) ⇒ Object



71
72
73
# File 'lib/bookbinder/file_system/zip_file.rb', line 71

def each(&blk)
  @zipfile.each { |entry| yield(entry.to_s) }
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/bookbinder/file_system/zip_file.rb', line 10

def exists?(path)
  @zipfile && @zipfile.find_entry(path) ? true : false
end

#get_file(path, mode = 'r', &blk) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bookbinder/file_system/zip_file.rb', line 34

def get_file(path, mode = 'r', &blk)
  read_before = mode[0] != 'w'
  write_after = mode[0] != 'r'
  if read_before
    must_exist(path)
    tmp_path = Dir::Tmpname.create(File.basename(path)) { |tmp_path|
      raise Errno::EEXIST  if File.exists?(tmp_path)
    }
    @zipfile.commit
    @zipfile.extract(path, tmp_path)
    File.open(tmp_path, mode) { |tmp_file|
      blk.call(tmp_file)
      if write_after
        set_file(path, tmp_file)
        @zipfile.commit
      end
    }
    File.unlink(tmp_path)
  else
    tmp_file = Tempfile.new(File.basename(path))
    blk.call(tmp_file)
    tmp_file.close
    set_file(path, tmp_file)  if write_after
    tmp_file.unlink
  end
  nil
end

#get_io(path, &blk) ⇒ Object



29
30
31
# File 'lib/bookbinder/file_system/zip_file.rb', line 29

def get_io(path, &blk)
  @zipfile.get_entry(path).get_input_stream(&blk)
end

#read(path) ⇒ Object



15
16
17
18
# File 'lib/bookbinder/file_system/zip_file.rb', line 15

def read(path)
  must_exist(path)
  @zipfile.read(path)
end

#set_file(path, file) ⇒ Object



63
64
65
66
67
68
# File 'lib/bookbinder/file_system/zip_file.rb', line 63

def set_file(path, file)
  instantiate_zipfile
  @zipfile.add(path, file.path) { true }
  @zipfile.commit
  nil
end

#write(path, data) ⇒ Object



21
22
23
24
25
26
# File 'lib/bookbinder/file_system/zip_file.rb', line 21

def write(path, data)
  return set_mimetype(data)  if path == 'mimetype'
  instantiate_zipfile
  @zipfile.get_output_stream(path) { |f| f.write(data) }
  nil
end