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
13
14
# File 'lib/bookbinder/file_system/zip_file.rb', line 10

def exists?(path)
  ascii_path = path
  ascii_path.force_encoding('ASCII-8BIT')
  @zipfile && @zipfile.find_entry(ascii_path) ? true : false
end

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



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 36

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 = Bookbinder::Scratch.file(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



31
32
33
# File 'lib/bookbinder/file_system/zip_file.rb', line 31

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

#read(path) ⇒ Object



17
18
19
20
# File 'lib/bookbinder/file_system/zip_file.rb', line 17

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



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

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