Class: Zip::ZipCentralDirectory

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pik/contrib/zip/zip.rb

Direct Known Subclasses

ZipFile

Constant Summary collapse

END_OF_CENTRAL_DIRECTORY_SIGNATURE =
0x06054b50
MAX_END_OF_CENTRAL_DIRECTORY_STRUCTURE_SIZE =
65536 + 18
STATIC_EOCD_SIZE =
22

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#inject, #select_map

Constructor Details

#initialize(entries = ZipEntrySet.new, comment = "") ⇒ ZipCentralDirectory

:nodoc:



1170
1171
1172
1173
1174
# File 'lib/pik/contrib/zip/zip.rb', line 1170

def initialize(entries = ZipEntrySet.new, comment = "")  #:nodoc:
  super()
  @entrySet = entries.kind_of?(ZipEntrySet) ? entries : ZipEntrySet.new(entries)
  @comment = comment
end

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment.



1163
1164
1165
# File 'lib/pik/contrib/zip/zip.rb', line 1163

def comment
  @comment
end

Class Method Details

.read_from_stream(io) ⇒ Object

:nodoc:



1274
1275
1276
1277
1278
1279
1280
# File 'lib/pik/contrib/zip/zip.rb', line 1274

def ZipCentralDirectory.read_from_stream(io)  #:nodoc:
  cdir  = new
  cdir.read_from_stream(io)
  return cdir
rescue ZipError
  return nil
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



1282
1283
1284
1285
# File 'lib/pik/contrib/zip/zip.rb', line 1282

def == (other) #:nodoc:
  return false unless other.kind_of?(ZipCentralDirectory)
  @entrySet.entries.sort == other.entries.sort && comment == other.comment
end

#each(&proc) ⇒ Object

For iterating over the entries.



1264
1265
1266
# File 'lib/pik/contrib/zip/zip.rb', line 1264

def each(&proc)
  @entrySet.each(&proc)
end

#entriesObject

Returns an Enumerable containing the entries.



1166
1167
1168
# File 'lib/pik/contrib/zip/zip.rb', line 1166

def entries
  @entrySet.entries
end

#get_e_o_c_d(io) ⇒ Object

:nodoc:

Raises:



1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
# File 'lib/pik/contrib/zip/zip.rb', line 1232

def get_e_o_c_d(io)  #:nodoc:
  begin
	io.seek(-MAX_END_OF_CENTRAL_DIRECTORY_STRUCTURE_SIZE, IO::SEEK_END)
  rescue Errno::EINVAL
	io.seek(0, IO::SEEK_SET)
  rescue Errno::EFBIG # FreeBSD 4.9 raise Errno::EFBIG instead of Errno::EINVAL
	io.seek(0, IO::SEEK_SET)
  end
  
  # 'buf = io.read' substituted with lump of code to work around FreeBSD 4.5 issue
  retried = false
  buf = nil
  begin
    buf = io.read
  rescue Errno::EFBIG # FreeBSD 4.5 may raise Errno::EFBIG
    raise if (retried)
    retried = true
	
    io.seek(0, IO::SEEK_SET)
    retry
  end

  sigIndex = buf.rindex([END_OF_CENTRAL_DIRECTORY_SIGNATURE].pack('V'))
  raise ZipError, "Zip end of central directory signature not found" unless sigIndex
  buf=buf.slice!((sigIndex+4)...(buf.size))
  def buf.read(count)
	slice!(0, count)
  end
  return buf
end

#read_central_directory_entries(io) ⇒ Object

:nodoc:



1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
# File 'lib/pik/contrib/zip/zip.rb', line 1215

def read_central_directory_entries(io)  #:nodoc:
  begin
	io.seek(@cdirOffset, IO::SEEK_SET)
  rescue Errno::EINVAL
	raise ZipError, "Zip consistency problem while reading central directory entry"
  end
  @entrySet = ZipEntrySet.new
  @size.times {
	@entrySet << ZipEntry.read_c_dir_entry(io)
  }
end

#read_e_o_c_d(io) ⇒ Object

:nodoc:

Raises:



1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
# File 'lib/pik/contrib/zip/zip.rb', line 1202

def read_e_o_c_d(io) #:nodoc:
  buf = get_e_o_c_d(io)
  @numberOfThisDisk                     = ZipEntry::read_zip_short(buf)
  @numberOfDiskWithStartOfCDir          = ZipEntry::read_zip_short(buf)
  @totalNumberOfEntriesInCDirOnThisDisk = ZipEntry::read_zip_short(buf)
  @size                                 = ZipEntry::read_zip_short(buf)
  @sizeInBytes                          = ZipEntry::read_zip_long(buf)
  @cdirOffset                           = ZipEntry::read_zip_long(buf)
  commentLength                         = ZipEntry::read_zip_short(buf)
  @comment                              = buf.read(commentLength)
  raise ZipError, "Zip consistency problem while reading eocd structure" unless buf.size == 0
end

#read_from_stream(io) ⇒ Object

:nodoc:



1227
1228
1229
1230
# File 'lib/pik/contrib/zip/zip.rb', line 1227

def read_from_stream(io)  #:nodoc:
  read_e_o_c_d(io)
  read_central_directory_entries(io)
end

#sizeObject

Returns the number of entries in the central directory (and consequently in the zip archive).



1270
1271
1272
# File 'lib/pik/contrib/zip/zip.rb', line 1270

def size
  @entrySet.size
end

#write_to_stream(io) ⇒ Object

:nodoc:



1176
1177
1178
1179
1180
# File 'lib/pik/contrib/zip/zip.rb', line 1176

def write_to_stream(io)  #:nodoc:
  offset = io.tell
  @entrySet.each { |entry| entry.write_c_dir_entry(io) }
  write_e_o_c_d(io, offset)
end