Class: Bookbinder::FileSystem::ZipFile
- Inherits:
-
Bookbinder::FileSystem
- Object
- Bookbinder::FileSystem
- Bookbinder::FileSystem::ZipFile
- Defined in:
- lib/bookbinder/file_system/zip_file.rb
Instance Method Summary collapse
- #close ⇒ Object
- #each(&blk) ⇒ Object
- #exists?(path) ⇒ Boolean
- #get_file(path, mode = 'r', &blk) ⇒ Object
- #get_io(path, &blk) ⇒ Object
-
#initialize(path) ⇒ ZipFile
constructor
A new instance of ZipFile.
- #read(path) ⇒ Object
- #set_file(path, file) ⇒ Object
- #write(path, data) ⇒ Object
Constructor Details
Instance Method Details
#close ⇒ Object
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
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 |