Method: ZipTricks::ZipWriter#write_end_of_central_directory

Defined in:
lib/zip_tricks/zip_writer.rb

#write_end_of_central_directory(io:, start_of_central_directory_location:, central_directory_size:, num_files_in_archive:, comment: ZIP_TRICKS_COMMENT) ⇒ void

This method returns an undefined value.

Writes the "end of central directory record" (including the Zip6 salient bits if necessary)

Parameters:

  • io (#<<)

    the buffer to write the central directory to.

  • start_of_central_directory_location (Fixnum)

    byte offset of the start of central directory form the beginning of ZIP file

  • central_directory_size (Fixnum)

    the size of the central directory (only file headers) in bytes

  • num_files_in_archive (Fixnum)

    How many files the archive contains

  • comment (String) (defaults to: ZIP_TRICKS_COMMENT)

    the comment for the archive (defaults to ZIP_TRICKS_COMMENT)



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/zip_tricks/zip_writer.rb', line 253

def write_end_of_central_directory(io:, start_of_central_directory_location:, central_directory_size:, num_files_in_archive:, comment: ZIP_TRICKS_COMMENT)
  zip64_eocdr_offset = start_of_central_directory_location + central_directory_size

  zip64_required = central_directory_size > FOUR_BYTE_MAX_UINT ||
                   start_of_central_directory_location > FOUR_BYTE_MAX_UINT ||
                   zip64_eocdr_offset > FOUR_BYTE_MAX_UINT ||
                   num_files_in_archive > TWO_BYTE_MAX_UINT

  # Then, if zip64 is used
  if zip64_required
    # [zip64 end of central directory record]
    # zip64 end of central dir
    io << [0x06064b50].pack(C_UINT4)                           # signature                       4 bytes  (0x06064b50)
    io << [44].pack(C_UINT8)                                  # size of zip64 end of central
    # directory record                8 bytes
    # (this is ex. the 12 bytes of the signature and the size value itself).
    # Without the extensible data sector (which we are not using)
    # it is always 44 bytes.
    io << MADE_BY_SIGNATURE                                # version made by                 2 bytes
    io << [VERSION_NEEDED_TO_EXTRACT_ZIP64].pack(C_UINT2)      # version needed to extract       2 bytes
    io << [0].pack(C_UINT4)                                    # number of this disk             4 bytes
    io << [0].pack(C_UINT4)                                    # number of the disk with the
    # start of the central directory  4 bytes
    io << [num_files_in_archive].pack(C_UINT8)                # total number of entries in the
    # central directory on this disk  8 bytes
    io << [num_files_in_archive].pack(C_UINT8)                # total number of entries in the
    # central directory               8 bytes
    io << [central_directory_size].pack(C_UINT8)              # size of the central directory   8 bytes
    # offset of start of central
    # directory with respect to
    io << [start_of_central_directory_location].pack(C_UINT8) # the starting disk number        8 bytes
    # zip64 extensible data sector    (variable size), blank for us

    # [zip64 end of central directory locator]
    io << [0x07064b50].pack(C_UINT4)                           # zip64 end of central dir locator
    # signature                       4 bytes  (0x07064b50)
    io << [0].pack(C_UINT4)                                    # number of the disk with the
    # start of the zip64 end of
    # central directory               4 bytes
    io << [zip64_eocdr_offset].pack(C_UINT8)                  # relative offset of the zip64
    # end of central directory record 8 bytes
    # (note: "relative" is actually "from the start of the file")
    io << [1].pack(C_UINT4)                                    # total number of disks           4 bytes
  end

  # Then the end of central directory record:
  io << [0x06054b50].pack(C_UINT4)                            # end of central dir signature     4 bytes  (0x06054b50)
  io << [0].pack(C_UINT2)                                     # number of this disk              2 bytes
  io << [0].pack(C_UINT2)                                     # number of the disk with the
  # start of the central directory 2 bytes

  if zip64_required # the number of entries will be read from the zip64 part of the central directory
    io << [TWO_BYTE_MAX_UINT].pack(C_UINT2)                   # total number of entries in the
    # central directory on this disk   2 bytes
    io << [TWO_BYTE_MAX_UINT].pack(C_UINT2)                   # total number of entries in
  # the central directory            2 bytes
  else
    io << [num_files_in_archive].pack(C_UINT2)                # total number of entries in the
    # central directory on this disk   2 bytes
    io << [num_files_in_archive].pack(C_UINT2)                # total number of entries in
    # the central directory            2 bytes
  end

  if zip64_required
    io << [FOUR_BYTE_MAX_UINT].pack(C_UINT4)                  # size of the central directory    4 bytes
    io << [FOUR_BYTE_MAX_UINT].pack(C_UINT4)                  # offset of start of central
  # directory with respect to
  # the starting disk number        4 bytes
  else
    io << [central_directory_size].pack(C_UINT4)              # size of the central directory    4 bytes
    io << [start_of_central_directory_location].pack(C_UINT4) # offset of start of central
    # directory with respect to
    # the starting disk number        4 bytes
  end
  io << [comment.bytesize].pack(C_UINT2)                      # .ZIP file comment length        2 bytes
  io << comment                                           # .ZIP file comment       (variable size)
end