home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / JSAGE / ZSUS / LBR / ZIP-APP.TXT < prev    next >
Text File  |  2000-06-30  |  15KB  |  455 lines

  1.  
  2. Disclaimer                                             February 10, 1989
  3. ----------                                             -----------------
  4.  
  5. Although PKWARE will attempt to supply current and accurate  information
  6. relating  to its file formats, algorithms, and the subject programs, the
  7. possibility of error can not be eliminated.  PKWARE therefore  expressly
  8. disclaims  any warranty that the information contained in the associated
  9. materials relating to the subject programs  and/or  the  format  of  the
  10. files  created or accessed by the subject programs and/or the algorithms
  11. used by the subject programs, or any other matter, is  current,  correct
  12. or  accurate as delivered.  Any risk of damage due to any possible inac-
  13. curate information is assumed by the user of the information.   Further-
  14. more,  the  information relating to the subject programs and/or the file
  15. formats created or accessed by the subject  programs  and/or  the  algo-
  16. rithms used by the subject programs is subject to change without notice.
  17.  
  18.  
  19. General Format of a ZIP file
  20. ----------------------------
  21.  
  22.   Files  stored  in  arbitrary  order.  Large zipfiles can span multiple
  23.   diskette media.
  24.  
  25.   Overall zipfile format:
  26.  
  27.     [local file header+file data] . . .
  28.     [central directory] end of central directory record
  29.  
  30.  
  31.   A.  Local file header:
  32.  
  33.         local file header signature     4 bytes  (0x04034b50)
  34.         version needed to extract       2 bytes
  35.         general purpose bit flag        2 bytes
  36.         compression method              2 bytes
  37.         last mod file time              2 bytes
  38.         last mod file date              2 bytes
  39.         crc-32                          4 bytes
  40.         compressed size                 4 bytes
  41.         uncompressed size               4 bytes
  42.         filename length                 2 bytes
  43.         extra field length              2 bytes
  44.  
  45.         filename (variable size)
  46.         extra field (variable size)
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.   B.  Central directory structure:
  54.  
  55.       [file header] . . .  end of central dir record
  56.  
  57.       File header:
  58.  
  59.         central file header signature   4 bytes  (0x02014b50)
  60.         version made by                 2 bytes
  61.         version needed to extract       2 bytes
  62.         general purpose bit flag        2 bytes
  63.         compression method              2 bytes
  64.         last mod file time              2 bytes
  65.         last mod file date              2 bytes
  66.         crc-32                          4 bytes
  67.         compressed size                 4 bytes
  68.         uncompressed size               4 bytes
  69.         filename length                 2 bytes
  70.         extra field length              2 bytes
  71.         file comment length             2 bytes
  72.         disk number start               2 bytes
  73.         internal file attributes        2 bytes
  74.         external file attributes        4 bytes
  75.         relative offset of local header 4 bytes
  76.  
  77.         filename (variable size)
  78.         extra field (variable size)
  79.         file comment (variable size)
  80.  
  81.       End of central dir record:
  82.  
  83.         end of central dir signature    4 bytes  (0x06054b50)
  84.         number of this disk             2 bytes
  85.         number of the disk with the
  86.         start of the central directory  2 bytes
  87.         total number of entries in
  88.         the central dir on this disk    2 bytes
  89.         total number of entries in
  90.         the central dir                 2 bytes
  91.         size of the central directory   4 bytes
  92.         offset of start of central
  93.         directory with respect to
  94.         the starting disk number        4 bytes
  95.         zipfile comment length          2 bytes
  96.         zipfile comment (variable size)
  97.  
  98.  
  99.  
  100.  
  101.   C.  Explanation of fields:
  102.  
  103.       version made by
  104.  
  105.           The upper byte indicates the host system (OS)  for
  106.           the  file.   Software  can use this information to
  107.           determine the line record format  for  text  files
  108.           etc.  The cur- rent mappings are:
  109.  
  110.           0 - IBM (MS-DOS)      1 - Amiga       2 - VMS
  111.           3 - *nix              4 thru 255 - unused
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.           The lower byte indicates the version number of the
  120.           software  used  to  encode the file.  The value/10
  121.           indicates the major version number, and the  value
  122.           mod 10 is the minor version number.
  123.  
  124.       version needed to extract
  125.  
  126.           The minimum software version needed to extract the
  127.           file, mapped as above.
  128.  
  129.       general purpose bit flag:
  130.  
  131.           The lowest bit, if set, indicates that the file is
  132.           encrypted.   The upper three bits are reserved and
  133.           used internally by the  software  when  processing
  134.           the  zipfile.   The  remaining  bits are unused in
  135.           version 1.0.
  136.  
  137. compression method:
  138.  
  139.           (see  accompanying  documentation  for   algorithm
  140.           descriptions)
  141.  
  142.           0 - The file is stored (no compression)
  143.           1 - The file is Shrunk
  144.           2 - The file is Reduced with compression factor 1
  145.           3 - The file is Reduced with compression factor 2
  146.           4 - The file is Reduced with compression factor 3
  147.           5 - The file is Reduced with compression factor 4
  148.  
  149.       date and time fields:
  150.  
  151.           The  date  and time are encoded in standard MS-DOS
  152.           format.
  153.  
  154.       CRC-32:
  155.  
  156.           The CRC-32 algorithm was generously contributed by
  157.           David Schwaderer and can be found in his excellent
  158.           book "C Programmers Guide to NetBIOS" published by
  159.           Howard W. Sams & Co.  Inc.  The 'magic number' for
  160.           the CRC is 0xdebb20e3.  The  proper  CRC  pre  and
  161.           post  conditioning  is  used, meaning that the CRC
  162.           register  is  pre-conditioned  with  all  ones  (a
  163.           starting  value  of  0xffffffff)  and the value is
  164.           post-conditioned by taking the one's complement of
  165.           the CRC residual.
  166.  
  167. compressed size: uncompressed size:
  168.  
  169.           The size of the file compressed and  uncompressed,
  170.           respectively.
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.      filename length:
  179.       extra field length:
  180.       file comment length:
  181.  
  182.           The length  of  the  filename,  extra  field,  and
  183.           comment  fields respectively.  The combined length
  184.           of any directory record  and  these  three  fields
  185.           should not generally exceed 65,535 bytes.
  186.  
  187.       disk number start:
  188.  
  189.           The number of the disk on which this file begins.
  190.  
  191.       internal file attributes:
  192.  
  193.           The  lowest  bit  of this field indicates, if set,
  194.           that the file is apparently an ASCII or text file.
  195.           If not set,  that  the  file  apparently  contains
  196.           binary  data.  The  remaining  bits  are unused in
  197.           version 1.0.
  198.  
  199.       external file attributes:
  200.  
  201.           The  mapping  of  the external attributes is host-
  202.           system dependent (see  'version  made  by').   For
  203.           MS-DOS, the low order byte is the MS-DOS directory
  204.           attribute byte.
  205.  
  206.       relative offset of local header:
  207.  
  208.           This  is  the  offset  from the start of the first
  209.           disk on which this  file  appears,  to  where  the
  210.           local header should be found.
  211.  
  212.       filename:
  213.  
  214.           The name of the file, with optional relative path.
  215.           The  path  stored  should  not  contain a drive or
  216.           device letter, or a leading  slash.   All  slashes
  217.           should  be forward slashes '/' as opposed to back-
  218.           wards slashes '\' for compatibility with Amiga and
  219.           Unix file systems etc.
  220.  
  221.       extra field:
  222.  
  223.           This  is  for future expansion.  If additional in-
  224.           formation needs to be stored  in  the  future,  it
  225.           should  be  stored  here.  Earlier versions of the
  226.           software can then safely skip this file, and  find
  227.           the  next  file  or  header.  This field will be 0
  228.           length in version 1.0.
  229.  
  230.       file comment:
  231.  
  232.           The comment for this file.
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.       number of this disk:
  240.  
  241.           The number of this disk,  which  contains  central
  242.           directory end record.
  243.  
  244.       number of the disk with the start of the central directory:
  245.  
  246.           The  number  of  the  disk  on  which  the central
  247.           directory starts.
  248.  
  249.       total number of entries in the central dir on this disk:
  250.  
  251.           The number of central directory  entries  on  this
  252.           disk.
  253.  
  254.       total number of entries in the central dir:
  255.  
  256.           The total number of files in the zipfile.
  257.  
  258.  
  259.       size of the central directory:
  260.  
  261.           The size (in bytes) of the entire central directory.
  262.  
  263.       offset of start of central directory with respect to the
  264.       starting disk number:
  265.  
  266.           Offset of the start of the central direcory on the
  267.           disk on which the central directory starts.
  268.  
  269.       zipfile comment length:
  270.  
  271.           The length of the comment for this zipfile.
  272.  
  273.       zipfile comment:
  274.  
  275.           The comment for this zipfile.
  276.  
  277.  
  278.   D.  General notes:
  279.  
  280.       1)  All fields  unless otherwise noted are unsigned and stored
  281.           in Intel low-byte:high-byte, low-word:high-word order.
  282.  
  283.       2)  String fields are not null terminated, since the length is
  284.           given explicitly.
  285.  
  286.       3)  Local headers should not span disk boundries.  Also,  even
  287.           though  the  central directory can span disk boundries, no
  288.           single record in the central  directory  should  be  split
  289.           across disks.
  290.  
  291.       4)  The  entries  in the central directory may not necessarily
  292.           be in the same order that files appear in the zipfile.
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299. UnShrinking
  300. -----------
  301.  
  302. Shrinking  is a Dynamic Ziv-Lempel-Welch compression algorithm with par-
  303. tial clearing.  The initial code size is 9 bits, and  the  maximum  code
  304. size is 13 bits. Shrinking differs from conventional Dynamic Ziv-Lempel-
  305. Welch implementations in several respects:
  306.  
  307. 1)  The  code size is controlled by the compressor, and is not auto-
  308.     matically increased when codes larger than the current code size
  309.     are created (but not necessarily used).  When  the  decompressor
  310.     encounters  the  code  sequence  256 (decimal) followed by 1, it
  311.     should increase the code size read from the input stream to  the
  312.     next  bit  size.   No blocking of the codes is performed, so the
  313.     next code at the increased size should be read  from  the  input
  314.     stream  immediately after where the previous code at the smaller
  315.     bit size was read.  Again, the decompressor should not  increase
  316.     the code size used until the sequence 256,1 is encountered.
  317.  
  318. 2)  When the table becomes full, total clearing  is  not  performed.
  319.     Rather, when the compresser emits the code sequence 256,2 (deci-
  320.     mal),  the  decompressor  should  clear  all leaf nodes from the
  321.     Ziv-Lempel tree, and continue to use the current code size.  The
  322.     nodes that are cleared from the Ziv-Lempel tree are then re-used
  323.     with the lowest code value re-used first, and the  highest  code
  324.     value  re-used last.  The compressor can emit the sequence 256,2
  325.     at any time.
  326.  
  327.  
  328.  
  329. Expanding
  330. ---------
  331.  
  332. The Reducing algorithm is actually a combination of two  distinct  algo-
  333. rithms.  The first algorithm compresses repeated byte sequences, and the
  334. second  algorithm  takes  the compressed stream from the first algorithm
  335. and applies a probabilistic compression method.
  336.  
  337. The probabilistic compression stores an array of 'follower  sets'  S(j),
  338. for  j=0  to  255, corresponding to each possible ASCII character.  Each
  339. set contains between 0 and 32 characters, to be denoted as  S(j)[0],...,
  340. S(j)[m],  where  m<32.  The sets are stored at the beginning of the data
  341. area for a Reduced file, in reverse order, with S(255) first,  and  S(0)
  342. last.
  343.  
  344. The  sets  are encoded as { N(j), S(j)[0],...,S(j)[N(j)-1] }, where N(j)
  345. is the size of set S(j).  N(j) can be 0, in which case the follower  set
  346. for  S(j)  is  empty.  Each N(j) value is encoded in 6 bits, followed by
  347. N(j) eight bit character values corresponding to S(j)[0] to S(j)[N(j)-1]
  348. respectively.  If N(j) is 0, then no values for S(j) are stored, and the
  349. value for N(j-1) immediately follows.
  350.  
  351. Immediately after the follower sets, is the compressed data stream.  The
  352. compressed data stream can be interpreted for the  probabilistic  decom-
  353. pression as follows:
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360. let Last-Character <- 0.
  361. loop until done
  362.     if the follower set S(Last-Character) is empty then
  363.         read 8 bits from the input stream, and copy this
  364.         value to the output stream.
  365.     otherwise if the follower set S(Last-Character) is non-empty then
  366.         read 1 bit from the input stream.
  367.         if this bit is not zero then
  368.             read 8 bits from the input stream, and copy this
  369.             value to the output stream.
  370.         otherwise if this bit is zero then
  371.             read B(N(Last-Character)) bits from the input
  372.             stream, and assign this value to I.
  373.             Copy the value of S(Last-Character)[I] to the
  374.             output stream.
  375.  
  376.     assign the last value placed on the output stream to
  377.     Last-Character.
  378. end loop
  379.  
  380.  
  381. B(N(j))  is defined as the minimal number of bits required to encode the
  382. value N(j)-1.
  383.  
  384.  
  385. The decompressed stream from above can then be expanded to re-create the
  386. original file as follows:
  387.  
  388.  
  389. let State <- 0.
  390.  
  391. loop until done
  392.     read 8 bits from the input stream into C.
  393.     case State of
  394.         0:  if C is not equal to DLE (144 decimal) then
  395.                 copy C to the output stream.
  396.             otherwise if C is equal to DLE then
  397.                 let State <- 1.
  398.  
  399.         1:  if C is non-zero then
  400.                 let V <- C.
  401.                 let Len <- L(V)
  402.                 let State <- F(Len).
  403.             otherwise if C is zero then
  404.                 copy the value 144 (decimal) to the output stream.
  405.                 let State <- 0
  406.  
  407.         2:  let Len <- Len + C
  408.             let State <- 3.
  409.  
  410.         3:  move backwards D(V,C) bytes in the output stream
  411.             (if this position is before the start of the output
  412.             stream, then assume that all the data before the
  413.             start of the output stream is filled with zeros).
  414.             copy Len+3 bytes from this position to the output stream.
  415.             let State <- 0.
  416.     end case
  417. end loop
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424. The functions F,L, and D are dependent on the 'compression
  425. factor' (see FORMAT.DOC), 1 through 4, and are defined as follows:
  426.  
  427. For compression factor 1:
  428.     L(X) equals the lower 7 bits of X.
  429.     F(X) equals 2 if X equals 127 otherwise F(X) equals 3.
  430.     D(X,Y) equals the (upper 1 bit of X) * 256 + Y + 1.
  431. For compression factor 2:
  432.     L(X) equals the lower 6 bits of X.
  433.     F(X) equals 2 if X equals 63 otherwise F(X) equals 3.
  434.     D(X,Y) equals the (upper 2 bits of X) * 256 + Y + 1.
  435. For compression factor 3:
  436.     L(X) equals the lower 5 bits of X.
  437.     F(X) equals 2 if X equals 31 otherwise F(X) equals 3.
  438.     D(X,Y) equals the (upper 3 bits of X) * 256 + Y + 1.
  439. For compression factor 4:
  440.     L(X) equals the lower 4 bits of X.
  441.     F(X) equals 2 if X equals 15 otherwise F(X) equals 3.
  442.     D(X,Y) equals the (upper 4 bits of X) * 256 + Y + 1.
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454. -
  455.