Class: DooDah::ZipOutputStream

Inherits:
Object
  • Object
show all
Includes:
CentralDirectoryHeader
Defined in:
lib/doo_dah/zip_output_stream.rb

Defined Under Namespace

Classes: EntryOpen

Constant Summary

Constants included from ZipEntryHeader

DooDah::ZipEntryHeader::CENTRAL_ENTRY_HEADER_SIGNATURE, DooDah::ZipEntryHeader::DEFLATED, DooDah::ZipEntryHeader::END_CENTRAL_DIRECTORY_SIGNATURE, DooDah::ZipEntryHeader::GP_FLAGS_CRC_UNKNOWN, DooDah::ZipEntryHeader::GP_FLAGS_UTF8, DooDah::ZipEntryHeader::LOCAL_ENTRY_FOOTER_SIGNATURE, DooDah::ZipEntryHeader::LOCAL_ENTRY_HEADER_SIGNATURE, DooDah::ZipEntryHeader::LOCAL_ENTRY_STATIC_HEADER_LENGTH, DooDah::ZipEntryHeader::LOCAL_ENTRY_TRAILING_DESCRIPTOR_LENGTH, DooDah::ZipEntryHeader::STORED, DooDah::ZipEntryHeader::VERSION_NEEDED_TO_EXTRACT

Instance Method Summary collapse

Methods included from CentralDirectoryHeader

central_header_size, end_of_central_directory_size, version_made_by_size, #write_central_header, #write_end_of_central_directory, #write_version_made_by

Methods included from ZipEntryHeader

common_header_size, name_size, #write_common_header, #write_infozip_utf8_name, #write_name

Methods included from ZipHeader

signature_size, #write_signature

Constructor Details

#initialize(output_stream) ⇒ ZipOutputStream

Returns a new instance of ZipOutputStream.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/doo_dah/zip_output_stream.rb', line 11

def initialize(output_stream)
  @output_stream = output_stream
  @total_bytes_written = 0
  @entries = []
  return unless block_given?
  begin
    yield self
  ensure
    close
  end
end

Instance Method Details

#closeObject



49
50
51
52
53
54
# File 'lib/doo_dah/zip_output_stream.rb', line 49

def close
  end_current_entry
  @central_directory_offset = current_offset
  @entries.each { |entry| entry.write_central_header }
  write_end_of_central_directory
end

#create_entry(name, size = 0, crc = 0) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
# File 'lib/doo_dah/zip_output_stream.rb', line 23

def create_entry(name, size=0, crc=0)
  raise EntryOpen if entry_open?
  begin
    yield start_entry(name, size, crc)
  ensure
    end_current_entry
  end
end

#current_offsetObject



56
57
58
# File 'lib/doo_dah/zip_output_stream.rb', line 56

def current_offset
  @total_bytes_written
end

#end_current_entryObject



40
41
42
43
# File 'lib/doo_dah/zip_output_stream.rb', line 40

def end_current_entry
  return unless current_entry
  current_entry.close
end

#entry_countObject



45
46
47
# File 'lib/doo_dah/zip_output_stream.rb', line 45

def entry_count
  @entries.size
end

#start_entry(name, size = 0, crc = 0) ⇒ Object

TODO: take block instead of using a start/end method pair?



33
34
35
36
37
38
# File 'lib/doo_dah/zip_output_stream.rb', line 33

def start_entry(name, size=0, crc=0)
  end_current_entry
  new_entry = ZipEntry.new(self, name, size, crc)
  @entries << new_entry
  new_entry
end

#write(data) ⇒ Object



60
61
62
63
64
# File 'lib/doo_dah/zip_output_stream.rb', line 60

def write(data)
  bytes_written = @output_stream.write(data)
  @total_bytes_written += bytes_written
  bytes_written
end