Skip to content

cloudacy/zippen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zippen

zippen is a small and easy to use zip generator. It can be used to pack multiple files / buffers into one .zip file / buffer.

This package was built based on the pkware .ZIP File Format Specification.

usage

import

commonjs

const Zip = require('@cloudacy/zippen').Zip

es6 module

import {Zip} from '@cloudacy/zippen'

create zip object

const zip = new Zip()

add entries to the zip

arguments
  • path - string: The location of the file to be stored in the .zip file (may also contain directories: e.g. foo/bar.txt)
  • last modified date - Date: A date object, holding the last modified date of the file or directory
  • data - Buffer | undefined: If the entry is a file, this should be a buffer, holding the uncompressed data. If it is a directory, pass undefined here.
zip.addEntry('foo.txt', new Date(), Buffer.from('bar'))

return the resulting .zip file buffer

This will return the resulting .zip buffer.

zip.build()

store resulting .zip file

write will store the resulting .zip file at the given file path.

arguments
  • path - string | number | Buffer | URL: The location of the resulting .zip file.
zip.write('foo.zip')

example

const zip = new Zip()
zip.addEntry('foo.txt', new Date(), Buffer.from('bar'))
zip.write('foo.zip')

general zip structure

  • local file header 1
  • encryption header 1
  • file data 1
  • data descriptor 1
  • ...
  • local file header n
  • encryption header n
  • file data n
  • data descriptor n
  • archive decryption header
  • archive extra data record
  • central directory header 1
  • ...
  • central directory header n
  • zip64 end of central directory record
  • zip64 end of central directory locator
  • end of central directory record

example of a .zip file

The following example is a hexdump of a .zip file, holding one file, called abc.txt with the content abc\n. The file was generated by a macOS 10.14.2 system.

All parts of the .zip file were grouped to the pieces of a .zip file. The bytes were also grouped by their meaning, based on the specification.

local file header block

  • local file header signature: 50 4b 03 04
  • version needed to extract: 14 00
  • general purpose bit flag: 08 00
  • compression algorithm: 08 00
  • last mod file time (MSDOS format): 6b 81
  • last mod file date (MSDOS format): 8f 4d
  • crc-32: 00 00 00 00
  • compressed size: 00 00 00 00
  • uncompressed size: 00 00 00 00
  • file name length: 07 00
  • extra field length: 10 00
  • file name: 61 62 63 2e 74 78 74 - ASCII: abc.txt

compressed data block(s)

  • compressed data: 4b 4c 4a e6 02 00

data descriptor block(s)

  • data descriptor signature (unofficial): 50 4b 07 08
  • crc-32: 4e 81 88 47
  • compressed size: 06 00 00 00
  • uncompressed size: 04 00 00 00

central directory block(s)

  • central file header signature: 50 4b 01 02
  • version made by: 15 03
  • version needed to extract: 14 00
  • general purpose bit flag: 08 00
  • compression method: 08 00
  • last mod file time (MSDOS format): 6b 81
  • last mod file date (MSDOS format): 8f 4d
  • crc-32: 4e 81 88 47
  • compressed size: 06 00 00 00
  • uncompressed size: 04 00 00 00
  • file name length: 07 00
  • extra field length: 00 00
  • file comment length: 00 00
  • disk number start: 00 00
  • internal file attributes: 00 00
  • external file attributes: 00 40 a4 81
  • relative offset of local header: 00 00 00 00
  • file name: 61 62 63 2e 74 78 74 - ASCII: abc.txt

end of central directories block

  • end of central directories signature: 50 4b 05 06
  • disk number: 00 00
  • disk number: 00 00
  • number of entries: 01 00
  • number of entries on this disk: 01 00
  • size of central directory block(s): 41 00 00 00
  • offset of central directory block(s): 4b 00 00 00
  • comment length: 00 00