使用Node JS从缓冲区读取上下字节

时间:2015-07-16 04:21:33

标签: javascript node.js byte buffer

我在Node JS中搞砸并构建一个zip文件阅读器。我在zip规范中遇到了一个问题,我不太清楚如何继续。

我有一个缓冲区,其中包含两个包含version made by数据的字节。有一个高字节,它告诉zip文件是由哪个系统制作的,并且有一个低字节,用于指示使用哪个版本的zip规范来创建文件。

如何从缓冲区读取高位字节和低位字节?现在我的代码正在做:

var madeBy = buffer.readUInt16LE(4); // 4 is the offset of `version made by`

这个无符号整数最终成为一个我觉得无用的数字。

另外,我不知道上/下字节的含义,所以如果你能向我解释这也会有所帮助。 :)

作为参考,这里有一个指向Node JS缓冲区文档的链接,以及zip规范所说的内容:

https://nodejs.org/api/buffer.html

4.4.2 version made by (2 bytes)

4.4.2.1 The upper byte indicates the compatibility of the file
attribute information.  If the external file attributes 
are compatible with MS-DOS and can be read by PKZIP for 
DOS version 2.04g then this value will be zero.  If these 
attributes are not compatible, then this value will 
identify the host system on which the attributes are 
compatible.  Software can use this information to determine
the line record format for text files etc.  

4.4.2.2 The current mappings are:

 0 - MS-DOS and OS/2 (FAT / VFAT / FAT32 file systems)
 1 - Amiga                     2 - OpenVMS
 3 - UNIX                      4 - VM/CMS
 5 - Atari ST                  6 - OS/2 H.P.F.S.
 7 - Macintosh                 8 - Z-System
 9 - CP/M                     10 - Windows NTFS
11 - MVS (OS/390 - Z/OS)      12 - VSE
13 - Acorn Risc               14 - VFAT
15 - alternate MVS            16 - BeOS
17 - Tandem                   18 - OS/400
19 - OS X (Darwin)            20 thru 255 - unused

4.4.2.3 The lower byte indicates the ZIP specification version 
(the version of this document) supported by the software 
used to encode the file.  The value/10 indicates the major 
version number, and the value mod 10 is the minor version 
number.  

1 个答案:

答案 0 :(得分:0)

对于zip file类型,文件头描述如下:

A.  Local file header:

local file header signature     4 bytes  
version needed to extract       2 bytes
general purpose bit flag        2 bytes
compression method              2 bytes
last mod file time              2 bytes
last mod file date              2 bytes
crc-32                          4 bytes
compressed size                 4 bytes
uncompressed size               4 bytes
filename length                 2 bytes
extra field length              2 bytes

由于值存储为little-endian,因此必须按相反的顺序读取它们:

var buffer = fs.readFileSync('/input.zip'),
 versionHeader = buffer.slice(4,6) //taking 5th & 6th byte according to spec.
 host = versionHeader [1] 
 majorVersion = versionHeader[0]/10
 minorVersion = versionHeader[0]%10

用于zip文件,前6个字节如下:

50 4b 03 04 14 00

打印host=0,例如MS-DOS和version=2.0例如主要版本2和次要版本0。

解释上下字节:

var versionHeader = buffer.readUInt16LE(4) // outputs 20

由于它是16位,十进制20可以用十六进制写为0x0014。高字节为0x00(十进制= 0),低字节为0x14(十进制为20)。

相关问题