Class: ZipTricks::FileReader::ZipEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/zip_tricks/file_reader.rb

Overview

Represents a file within the ZIP archive being read. This is different from the Entry object used in Streamer for ZIP writing, since during writing more data can be kept in memory for immediate use.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commentString

Returns the file comment.

Returns:

  • (String)

    the file comment



133
134
135
# File 'lib/zip_tricks/file_reader.rb', line 133

def comment
  @comment
end

#compressed_sizeFixnum

Returns size of compressed file data in the ZIP.

Returns:

  • (Fixnum)

    size of compressed file data in the ZIP



111
112
113
# File 'lib/zip_tricks/file_reader.rb', line 111

def compressed_size
  @compressed_size
end

#crc32Fixnum

Returns the CRC32 checksum of this file.

Returns:

  • (Fixnum)

    the CRC32 checksum of this file



108
109
110
# File 'lib/zip_tricks/file_reader.rb', line 108

def crc32
  @crc32
end

#disk_number_startFixnum

Returns disk number where this file starts.

Returns:

  • (Fixnum)

    disk number where this file starts



120
121
122
# File 'lib/zip_tricks/file_reader.rb', line 120

def disk_number_start
  @disk_number_start
end

#dos_dateFixnum

Returns the bit-packed DOS date.

Returns:

  • (Fixnum)

    the bit-packed DOS date



105
106
107
# File 'lib/zip_tricks/file_reader.rb', line 105

def dos_date
  @dos_date
end

#dos_timeFixnum

Returns the bit-packed DOS time.

Returns:

  • (Fixnum)

    the bit-packed DOS time



102
103
104
# File 'lib/zip_tricks/file_reader.rb', line 102

def dos_time
  @dos_time
end

#external_attrsFixnum

Returns external attributes of the file.

Returns:

  • (Fixnum)

    external attributes of the file



126
127
128
# File 'lib/zip_tricks/file_reader.rb', line 126

def external_attrs
  @external_attrs
end

#filenameString

Returns the filename.

Returns:

  • (String)

    the filename



117
118
119
# File 'lib/zip_tricks/file_reader.rb', line 117

def filename
  @filename
end

#gp_flagsFixnum

Returns bit-packed general purpose flags.

Returns:

  • (Fixnum)

    bit-packed general purpose flags



96
97
98
# File 'lib/zip_tricks/file_reader.rb', line 96

def gp_flags
  @gp_flags
end

#internal_attrsFixnum

Returns internal attributes of the file.

Returns:

  • (Fixnum)

    internal attributes of the file



123
124
125
# File 'lib/zip_tricks/file_reader.rb', line 123

def internal_attrs
  @internal_attrs
end

#local_file_header_offsetFixnum

Returns at what offset the local file header starts in your original IO object.

Returns:

  • (Fixnum)

    at what offset the local file header starts in your original IO object



130
131
132
# File 'lib/zip_tricks/file_reader.rb', line 130

def local_file_header_offset
  @local_file_header_offset
end

#made_byFixnum

Returns bit-packed version signature of the program that made the archive.

Returns:

  • (Fixnum)

    bit-packed version signature of the program that made the archive



90
91
92
# File 'lib/zip_tricks/file_reader.rb', line 90

def made_by
  @made_by
end

#storage_modeFixnum

Returns Storage mode (0 for stored, 8 for deflate).

Returns:

  • (Fixnum)

    Storage mode (0 for stored, 8 for deflate)



99
100
101
# File 'lib/zip_tricks/file_reader.rb', line 99

def storage_mode
  @storage_mode
end

#uncompressed_sizeFixnum

Returns size of the file once uncompressed.

Returns:

  • (Fixnum)

    size of the file once uncompressed



114
115
116
# File 'lib/zip_tricks/file_reader.rb', line 114

def uncompressed_size
  @uncompressed_size
end

#version_needed_to_extractFixnum

Returns ZIP version support needed to extract this file.

Returns:

  • (Fixnum)

    ZIP version support needed to extract this file



93
94
95
# File 'lib/zip_tricks/file_reader.rb', line 93

def version_needed_to_extract
  @version_needed_to_extract
end

Instance Method Details

#compressed_data_offsetFixnum

Returns at what offset you should start reading for the compressed data in your original IO object.

Returns:

  • (Fixnum)

    at what offset you should start reading for the compressed data in your original IO object



156
157
158
# File 'lib/zip_tricks/file_reader.rb', line 156

def compressed_data_offset
  @compressed_data_offset || raise(LocalHeaderPending)
end

#compressed_data_offset=(offset) ⇒ Object

Sets the offset at which the compressed data for this file starts in the ZIP. By default, the value will be set by the Reader for you. If you use delayed reading, you need to set it by using the get_compressed_data_offset on the Reader:

entry.compressed_data_offset = reader.get_compressed_data_offset(io: file,
       local_file_header_offset: entry.local_header_offset)


178
179
180
# File 'lib/zip_tricks/file_reader.rb', line 178

def compressed_data_offset=(offset)
  @compressed_data_offset = offset.to_i
end

#extractor_from(from_io) ⇒ #extract(n_bytes), #eof?

Returns a reader for the actual compressed data of the entry.

reader = entry.extractor_from(source_file) outfile << reader.extract(512 * 1024) until reader.eof?

Returns:

  • (#extract(n_bytes), #eof?)

    the reader for the data



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/zip_tricks/file_reader.rb', line 141

def extractor_from(from_io)
  from_io.seek(compressed_data_offset, IO::SEEK_SET)
  case storage_mode
  when 8
    InflatingReader.new(from_io, compressed_size)
  when 0
    StoredReader.new(from_io, compressed_size)
  else
    raise UnsupportedFeature, 'Unsupported storage mode for reading - %<storage_mode>d' %
                              {storage_mode: storage_mode}
  end
end

#known_offset?Boolean

Tells whether the compressed data offset is already known for this entry

Returns:

  • (Boolean)


162
163
164
# File 'lib/zip_tricks/file_reader.rb', line 162

def known_offset?
  !@compressed_data_offset.nil?
end

#uses_data_descriptor?Boolean

Tells whether the entry uses a data descriptor (this is defined by bit 3 in the GP flags).

Returns:

  • (Boolean)


168
169
170
# File 'lib/zip_tricks/file_reader.rb', line 168

def uses_data_descriptor?
  (gp_flags & 0x0008) == 0x0008
end