adm-zip

repository·master·Indexed 24 days ago

https://github.com/cthackers/adm-zip

A pure JavaScript implementation of ZIP data compression and decompression for NodeJS, with support for Electron original-fs. Version 0.6.0 allows users to read, create, update, and delete files within ZIP archives both in memory and on disk.

Tokens
13K
Snippets
22
Records
64
Agent score
80%

What's inside adm-zip

  1. Understand Deflate (Method 8) block types

    master

    The Deflate algorithm stores compressed data in blocks. Each block starts with a header that defines its type. When parsing or implementing Deflate, identify the block type using bits 1-2 of the header:

    • 00 (0): Stored Block. Data is uncompressed and byte-aligned. The block contains a length word followed by its one's complement.
    • 01 (1): Fixed Huffman Codes. Uses a predefined set of Huffman codes for literals and distances.
    • 10 (2): Dynamic Huffman Codes. Uses custom Huffman codes defined within the block header.
    • 11 (3): Reserved. Encountering this should trigger an "Error in compressed data".

    Additionally, Bit 0 of the header is the Last Block bit, which is set to 1 if the current block is the final one in the stream.

  2. Understand ZIP file versioning and compatibility

    master

    ZIP files use two specific 2-byte fields to manage compatibility and feature requirements:

    1. version made by: Indicates the host system and ZIP specification version used to encode the file. The upper byte identifies the host system (e.g., 0 for MS-DOS/OS2, 3 for UNIX, 10 for Windows NTFS, 19 for OS X). The lower byte represents the ZIP specification version (major version is value / 10, minor version is value % 10).

    2. version needed to extract: The minimum ZIP specification version required to extract the file. This is determined by the highest-value feature applied to the file. For example, if a file uses AES encryption, this value must be at least 5.1 (represented as 51).

    Common feature version mappings include:

    • 1.0: Default
    • 2.0: Deflate compression or Folders
    • 4.5: ZIP64 format extensions
    • 5.1: AES encryption
    • 6.3: LZMA or PPMd+ compression
  3. Understand ZIP file structure and field constraints

    master

    The ZIP file format uses specific field lengths and special values to handle large files (ZIP64).

    Key Constraints:

    • Combined Lengths: The combined length of a directory record, file name, extra field, and file comment should generally not exceed 65,535 bytes.
    • Standard Input: If data is sourced from standard input, the file name length is set to zero, and external file attributes are set to zero.
    • File Names: Paths must use forward slashes (/) and must not contain drive letters, device letters, or leading slashes.
    • ZIP64 Transition: When a field value reaches its maximum capacity (e.g., 0xFFFFFFFF for 4-byte fields or 0xFFFF for 2-byte fields), the actual value is stored in a corresponding ZIP64 extended information extra field.
    • Encryption Masking: If Central Directory Encryption is used and general purpose bit flag 13 is set, the file name and uncompressed size in the Local Header may be masked (set to zero or a unique hexadecimal value) to hide metadata.
  4. Understand Manifest Files in ZIP archives

    master

    Applications may use Manifest Files to store additional information required by the application process that cannot be stored using standard ZIP storage records.

    Key characteristics:

    • Location: Typically placed within the same ZIP file as the data it describes. By convention, it is often the first file in the archive and may include a defined directory path.
    • Format: Can be any file type required by the application (e.g., META-INF/MANIFEST.MF in .JAR files).
    • Compression: Manifest files may be compressed or encrypted.

    Note: Manifest file specifications are outside the scope of the standard ZIP specification.

  5. Understand WavPack compression (Method 97) in ZIP files

    master

    WavPack compression (Method 97) uses the open-source WavPack utility.

    Implementation Details:

    • Header Indication: The compression method field must be set to 97 in both the local header and the central directory header.
    • Data Placement: WavPack data begins immediately after the end of the local header data.
    • Digital Sample Data Best Practice: When storing digital sample data, compress all bytes of the sample data, including unused bits up to the byte boundary. For example, if a 2-byte sample uses only 12 bits, provide the full 16-bit sample size to the WavPack routines to prevent unused bits from being zeroed out during extraction.
  6. Understand the ZIP file format specification

    master

    The .ZIP file format is a cross-platform, interoperable container used to aggregate, compress, and encrypt files. While many applications use the .ZIP extension, the format is also used by .JAR, .WAR, .DOCX, .XLSX, and other formats.

    Key characteristics of the ZIP format include:

    • Compression: Files can be compressed using various algorithms (e.g., Deflate, method 8) or simply 'stored' (copied uncompressed).
    • Encryption: Supports passwords and public/private keys for individual files or metadata encryption.
    • Integrity: Data integrity MUST be provided for each file using CRC32. Digital signatures may be used for additional integrity.
    • Structure: A ZIP file MUST contain exactly one "end of central directory record". Each file within the archive MUST have a "local file header" and a corresponding "central directory header".
    • Extensibility: Supports "extra data fields" for platform or application-specific needs.
  7. Understand LZMA compression (Method 14) in ZIP files

    master

    LZMA compression in a ZIP file consists of an LZMA Properties Header followed by the LZMA Compressed Data.

    LZMA Properties Header Structure:

    • LZMA Version Information (2 bytes): First byte is the major version, second byte is the minor version of the LZMA SDK used.
    • LZMA Properties Size (2 bytes): Defines the size of the following property data.
    • LZMA Property Data (variable): Required values for decompression (e.g., dictionary size).

    Key Implementation Details:

    • EOS Marker: LZMA data may include an end-of-stream (EOS) marker. If present, general purpose bit 1 must be set in the ZIP header. If bit 1 is not set, the EOS marker is assumed to be absent.
  8. Understand the ZIP file structure and record sequence

    master

    A .ZIP archive is composed of a series of repeating file segments followed by a central directory. The standard sequence for a file entry is:

    1. Local File Header: Contains metadata about the file.
    2. Encryption Header (Optional): Present if the file is encrypted.
    3. File Data: The actual compressed or stored content.
    4. Data Descriptor (Optional): Used for streaming; follows the file data.

    After all file entries, the archive contains:

    • Archive Decryption Header (Optional): Used for Central Directory Encryption.
    • Archive Extra Data Record (Optional).
    • Central Directory Headers: One for each file in the archive.
    • Zip64 End of Central Directory Record (if applicable).
    • Zip64 End of Central Directory Locator (if applicable).
    • End of Central Directory Record.
  9. Understand ZIP Strong Encryption Algorithms

    master

    The ZIP specification supports several strong encryption algorithms. When working with encrypted archives, you may encounter different AlgId values representing these algorithms:

    • AES: 128 bit (0x660E), 192 bit (0x660F), and 256 bit (0x6610)
    • 3DES: 168 bit (0x6603) and 112 bit (0x6609)
    • DES: (0x6601)
    • RC2: Version-dependent (0x6602 for < 5.2, 0x6702 for >= 5.2)
    • Blowfish: (0x6720)
    • Twofish: (0x6721)
    • RC4: (0x6801)
  10. Understand ZIP Local Header Masking

    master

    When the Central Directory is encrypted, the Local Header (the header preceding the actual file data) may be 'masked' to hide file metadata. Masking replaces true content with false information to prevent leaking details about the archive contents.

    Important Considerations:

    • Streaming: Masked Local Headers are not suitable for streaming access.
    • Data Recovery: Masking reduces the effectiveness of data recovery for damaged archives.
    • Confidentiality: Do not store confidential data in 'Extra Data' fields within the Local Header, as these may be subject to masking.
    • Compatibility: The file name field in the Local Header should never be left blank. For masked files, the name is typically a sequentially incremented Base 16 value.

    Masked Fields Mapping:

    Field NameMask Value
    compression method0
    last mod file time0
    last mod file date0
    crc-320
    compressed size0
    uncompressed size0
    file nameBase 16 value (1 - 0xFFFFFFFFFFFFFFFF)

    Note: As of Version 6.2, Compression Method and Compressed Size fields are not yet masked. Fields with values 0xFFFF or 0xFFFFFFFF for ZIP64 should not be masked.

  11. Understand Split and Spanned ZIP file naming conventions

    master

    ZIP files can be segmented into multiple parts using two different methods: Spanning and Splitting.

    Spanned ZIP Files

    Used typically for removable media (like floppy disks). All segments share the same filename. The sequence is determined by the DOS volume label in the form PKBACK#xxx, where xxx is the decimal segment number (e.g., 001 to nnn).

    Split ZIP Files

    Used for local file systems to avoid name collisions. Segments are named using a specific sequence:

    • Segment 1: filename.z01
    • Segment n-1: filename.z(n-1)
    • Final Segment: filename.zip (The .ZIP extension is used on the last segment to allow quick reading of the central directory).

    Split PKSFX (Self-extracting) Files

    If a split archive is a PKSFX self-extracting file, the first segment must be named filename.exe and must be large enough to contain the entire executable program.

  12. Handle UTF-8 encoding in ZIP headers

    master

    When working with ZIP files that require Unicode support, the UnicodeName is the UTF-8 version of the File Name field.

    To indicate that both the File Name and Comment fields are UTF-8, use the General Purpose Bit Flag, bit 11 (Language encoding flag (EFS)).

    Important constraints:

    • No UTF-8 Byte Order Mark (BOM) should be used.
    • If bit 11 is used, the Unicode Path and Unicode Comment extra fields are not needed and should not be created.
    • For backward compatibility, bit 11 should only be used if the native character set of the paths and comments being zipped is already UTF-8.
    • The same storage method (either bit 11 or extra fields) must be used consistently in both the Local and Central Directory Header for a file.