diff --git a/src/reader.ts b/src/reader.ts index 66be291..03ae22a 100644 --- a/src/reader.ts +++ b/src/reader.ts @@ -66,6 +66,10 @@ export class Reader { return this.buffer.indexOf(header, 0, "hex"); } + setOffset(offset: number): void { + this.offset = offset; + } + constructor(zip: Buffer) { this.offset = 0; this.buffer = zip; diff --git a/src/unzip.ts b/src/unzip.ts index d2a5619..2fa0c1d 100644 --- a/src/unzip.ts +++ b/src/unzip.ts @@ -3,21 +3,21 @@ import * as fs from "fs"; import { compressionMethod, formatModTime, formatModDate } from "./util"; const readLocalFileHeader = (reader: Reader) => { - // 0 | 4 : Local file header signature = 0x04034b50 (read as a little-endian number) - // 4 | 2 : Version needed to extract (minimum) - // 6 | 2 : General purpose bit flag - // 8 | 2 : Compression method - // 10 | 2 : File last modification time - // 12 | 2 : File last modification date - // 14 | 4 : CRC-32 of uncompressed data - // 18 | 4 : Compressed size - // 22 | 4 : Uncompressed size - // 26 | 2 : File name length (n) - // 28 | 2 : Extra field length (m) - // 30 | n : File name - // 30+n | m : Extra field + // 0 | 4 : Local file header signature = 0x04034b50 (read as a little-endian number) + // 4 | 2 : Version needed to extract (minimum) + // 6 | 2 : General purpose bit flag + // 8 | 2 : Compression method + // 10 | 2 : File last modification time + // 12 | 2 : File last modification date + // 14 | 4 : CRC-32 of uncompressed data + // 18 | 4 : Compressed size + // 22 | 4 : Uncompressed size + // 26 | 2 : File name length (n) + // 28 | 2 : Extra field length (m) + // 30 | n : File name + // 30+n | m : Extra field - reader.offset = reader.findHeader(LESignature.LocalFile); + reader.setOffset(reader.findHeader(LESignature.LocalFile)); let signature = reader.read4Bytes(); let extractVersion = reader.read2Bytes(); let bitFlag = reader.read2Bytes(); @@ -53,28 +53,28 @@ const readLocalFileHeader = (reader: Reader) => { }; const readCentralDirectory = (reader: Reader) => { - // 0 | 4 : Central directory file header signature = 0x02014b50 - // 4 | 2 : Version made by - // 6 | 2 : Version needed to extract (minimum) - // 8 | 2 : General purpose bit flag - // 10 | 2 : Compression method - // 12 | 2 : File last modification time - // 14 | 2 : File last modification date - // 16 | 4 : CRC-32 of uncompressed data - // 20 | 4 : Compressed size - // 24 | 4 : Uncompressed size - // 28 | 2 : File name length (n) - // 30 | 2 : Extra field length (m) - // 32 | 2 : File comment length (k) - // 34 | 2 : Disk number where file starts - // 36 | 2 : Internal file attributes - // 38 | 4 : External file attributes - // 42 | 4 : Relative offset of local file header. This is the number of bytes between the start of the first disk on which the file occurs, and the start of the local file header. This allows software reading the central directory to locate the position of the file inside the ZIP file. - // 46 | n : File name - // 46+n | m : Extra field - // 46+n+m | k : File comment + // 0 | 4 : Central directory file header signature = 0x02014b50 + // 4 | 2 : Version made by + // 6 | 2 : Version needed to extract (minimum) + // 8 | 2 : General purpose bit flag + // 10 | 2 : Compression method + // 12 | 2 : File last modification time + // 14 | 2 : File last modification date + // 16 | 4 : CRC-32 of uncompressed data + // 20 | 4 : Compressed size + // 24 | 4 : Uncompressed size + // 28 | 2 : File name length (n) + // 30 | 2 : Extra field length (m) + // 32 | 2 : File comment length (k) + // 34 | 2 : Disk number where file starts + // 36 | 2 : Internal file attributes + // 38 | 4 : External file attributes + // 42 | 4 : Relative offset of local file header. This is the number of bytes between the start of the first disk on which the file occurs, and the start of the local file header. This allows software reading the central directory to locate the position of the file inside the ZIP file. + // 46 | n : File name + // 46+n | m : Extra field + // 46+n+m | k : File comment - reader.offset = reader.findHeader(LESignature.CentralDirectory); + reader.setOffset(reader.findHeader(LESignature.CentralDirectory)); let signature = reader.read4Bytes(); let version = reader.read2Bytes(); let extractVersion = reader.read2Bytes(); @@ -122,17 +122,17 @@ const readCentralDirectory = (reader: Reader) => { }; const readEndCentralDirectory = (reader: Reader) => { - // 0 | 4 : End of central directory signature = 0x06054b50 - // 4 | 2 : Number of this disk - // 6 | 2 : Disk where central directory starts - // 8 | 2 : Number of central directory records on this disk - // 10 | 2 : Total number of central directory records - // 12 | 4 : Size of central directory (bytes) - // 16 | 4 : Offset of start of central directory, relative to start of archive - // 20 | 2 : Comment length (n) - // 22 | n : Comment + // 0 | 4 : End of central directory signature = 0x06054b50 + // 4 | 2 : Number of this disk + // 6 | 2 : Disk where central directory starts + // 8 | 2 : Number of central directory records on this disk + // 10 | 2 : Total number of central directory records + // 12 | 4 : Size of central directory (bytes) + // 16 | 4 : Offset of start of central directory, relative to start of archive + // 20 | 2 : Comment length (n) + // 22 | n : Comment - reader.offset = reader.findHeader(LESignature.EndCentralDirectory); + reader.setOffset(reader.findHeader(LESignature.EndCentralDirectory)); let signature = reader.read4Bytes(); let diskNumber = reader.read2Bytes(); let diskCentralStart = reader.read2Bytes();