Sha256: bab988a8f5ad23bf24c93e4854abd15d5ba5440fc9641ea4f1475c8491b27ba1

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

require 'archive/zip'

module EPUB
  class OCF
    class PhysicalContainer
      class ArchiveZip < self
        def initialize(container_path)
          super
          @entries = {}
          @last_iterated_entry_index = 0
        end

        def open
          Archive::Zip.open @container_path do |archive|
            @archive = archive
            begin
              yield self
            ensure
              @archive = nil
            end
          end
        end

        def read(path_name)
          target_index = @entries[path_name]
          if @archive
            @archive.each.with_index do |entry, index|
              if target_index
                if target_index == index
                  return entry.file_data.read
                else
                  next
                end
              end
              next if index < @last_iterated_entry_index
              # We can force encoding UTF-8 becase EPUB spec allows only UTF-8 filenames
              entry_path = entry.zip_path.force_encoding('UTF-8')
              @entries[entry_path] = index
              @last_iterated_entry_index = index
              if entry_path == path_name
                return entry.file_data.read
              end
            end
          else
            open {|container| container.read(path_name)}
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
epub-parser-0.2.4 lib/epub/ocf/physical_container/archive_zip.rb
epub-parser-0.2.3 lib/epub/ocf/physical_container/archive_zip.rb
epub-parser-0.2.2 lib/epub/ocf/physical_container/archive_zip.rb
epub-parser-0.2.1 lib/epub/ocf/physical_container/archive_zip.rb