Sha256: a7419b6f97e116eed26e2395d37a82c259334b8490bbe45fe7aa884acf2ef1d8

Contents?: true

Size: 1.39 KB

Versions: 9

Compression:

Stored size: 1.39 KB

Contents

require 'epub/ocf/physical_container/archive_zip'

module EPUB
  class OCF
    class PhysicalContainer
      class ArchiveZip < self
        # @todo Write multiple files at once
        def write(path_name, content)
          ::Dir.mktmpdir do |dir|
            tmp_archive_path = ::File.join(dir, ::File.basename(@container_path) + '.tmp')
            ::File.open @container_path do |archive_in|
              ::File.open tmp_archive_path, 'w' do |archive_out|
                archive_out.binmode
                Archive::Zip.open archive_in, :r do |z_in|
                  Archive::Zip.open archive_out, :w do |z_out|
                    updated = false
                    z_in.each do |entry|
                      if entry.zip_path == path_name
                        entry.file_data = StringIO.new(content)
                        updated = true
                      end
                      z_out << entry
                    end
                    unless updated
                      entry = Archive::Zip::Entry::File.new(path_name)
                      entry.file_data = StringIO.new(content)
                      z_out << entry
                    end
                  end
                end
              end
            end
            ::File.chmod 0666 & ~::File.umask, tmp_archive_path
            ::File.rename tmp_archive_path, @container_path
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
epub-maker-0.1.2 lib/epub/maker/ocf/physical_container/archive_zip.rb
epub-maker-0.1.1 lib/epub/maker/ocf/physical_container/archive_zip.rb
epub-maker-0.1.0 lib/epub/maker/ocf/physical_container/archive_zip.rb
epub-maker-0.0.9 lib/epub/maker/ocf/physical_container/archive_zip.rb
epub-maker-0.0.8 lib/epub/maker/ocf/physical_container/archive_zip.rb
epub-maker-0.0.7 lib/epub/maker/ocf/physical_container/archive_zip.rb
epub-maker-0.0.6 lib/epub/maker/ocf/physical_container/archive_zip.rb
epub-maker-0.0.5 lib/epub/maker/ocf/physical_container/archive_zip.rb
epub-maker-0.0.4 lib/epub/maker/ocf/physical_container/archive_zip.rb