Class: Kotoshu::Readers::ZipReader
- Inherits:
-
Object
- Object
- Kotoshu::Readers::ZipReader
- Defined in:
- lib/kotoshu/readers/file_reader.rb
Overview
Zip reader for reading files from zip archives.
This class reads files from within zip archives, such as OpenOffice/LibreOffice extensions (.odt, .oxt).
Constant Summary collapse
- UTF8_BOM =
BOM (byte-order mark) for UTF-8
"\xEF\xBB\xBF"
Instance Attribute Summary collapse
-
#encoding ⇒ String
readonly
The encoding.
-
#entry_path ⇒ String
readonly
The entry path within the zip.
-
#line_no ⇒ Integer
readonly
Current line number.
-
#zipfile ⇒ Zip::File
readonly
The zip file object.
Instance Method Summary collapse
-
#close ⇒ Object
Close the zip entry.
-
#each {|Integer, String| ... } ⇒ Enumerator
Iterate over lines.
-
#has_next? ⇒ Boolean
Check if there are more lines.
-
#initialize(zipfile, entry_path, encoding = 'UTF-8') ⇒ ZipReader
constructor
Create a new zip reader.
-
#next ⇒ Array<Integer, String>
Get next line.
-
#peek ⇒ Array<Integer, String>
Peek at next line without consuming it.
-
#reset ⇒ Object
Reset the reader to the beginning.
-
#reset_encoding(new_encoding) ⇒ Object
Reset encoding and reopen zip entry.
-
#to_a ⇒ Array<Array<Integer, String>>
Get all lines as an array.
Constructor Details
#initialize(zipfile, entry_path, encoding = 'UTF-8') ⇒ ZipReader
Create a new zip reader.
261 262 263 264 265 266 267 268 269 |
# File 'lib/kotoshu/readers/file_reader.rb', line 261 def initialize(zipfile, entry_path, encoding = 'UTF-8') @zipfile = zipfile @entry_path = entry_path @encoding = encoding @line_no = 0 @entry = nil @iterator = nil reset_io end |
Instance Attribute Details
#encoding ⇒ String (readonly)
Returns The encoding.
248 249 250 |
# File 'lib/kotoshu/readers/file_reader.rb', line 248 def encoding @encoding end |
#entry_path ⇒ String (readonly)
Returns The entry path within the zip.
245 246 247 |
# File 'lib/kotoshu/readers/file_reader.rb', line 245 def entry_path @entry_path end |
#line_no ⇒ Integer (readonly)
Returns Current line number.
251 252 253 |
# File 'lib/kotoshu/readers/file_reader.rb', line 251 def line_no @line_no end |
#zipfile ⇒ Zip::File (readonly)
Returns The zip file object.
242 243 244 |
# File 'lib/kotoshu/readers/file_reader.rb', line 242 def zipfile @zipfile end |
Instance Method Details
#close ⇒ Object
Close the zip entry.
329 330 331 332 |
# File 'lib/kotoshu/readers/file_reader.rb', line 329 def close @entry&.close @entry = nil end |
#each {|Integer, String| ... } ⇒ Enumerator
Iterate over lines.
285 286 287 288 289 |
# File 'lib/kotoshu/readers/file_reader.rb', line 285 def each(&) return enum_for(:each) unless block_given? @iterator.each(&) end |
#has_next? ⇒ Boolean
Check if there are more lines.
301 302 303 304 305 306 |
# File 'lib/kotoshu/readers/file_reader.rb', line 301 def has_next? peek true rescue StopIteration false end |
#next ⇒ Array<Integer, String>
Get next line.
318 319 320 |
# File 'lib/kotoshu/readers/file_reader.rb', line 318 def next @iterator.next end |
#peek ⇒ Array<Integer, String>
Peek at next line without consuming it.
311 312 313 |
# File 'lib/kotoshu/readers/file_reader.rb', line 311 def peek @iterator.peek end |
#reset ⇒ Object
Reset the reader to the beginning.
323 324 325 326 |
# File 'lib/kotoshu/readers/file_reader.rb', line 323 def reset @line_no = 0 reset_io end |
#reset_encoding(new_encoding) ⇒ Object
Reset encoding and reopen zip entry.
274 275 276 277 278 279 |
# File 'lib/kotoshu/readers/file_reader.rb', line 274 def reset_encoding(new_encoding) @encoding = new_encoding @line_no = 0 @entry&.close reset_io end |
#to_a ⇒ Array<Array<Integer, String>>
Get all lines as an array.
294 295 296 |
# File 'lib/kotoshu/readers/file_reader.rb', line 294 def to_a @iterator.to_a end |