# yauzl
[![Build Status](https://travis-ci.org/thejoshwolfe/yauzl.svg?branch=master)](https://travis-ci.org/thejoshwolfe/yauzl)
[![Coverage Status](https://img.shields.io/coveralls/thejoshwolfe/yauzl.svg)](https://coveralls.io/r/thejoshwolfe/yauzl)
yet another unzip library for node. For zipping, see
[yazl](https://github.com/thejoshwolfe/yazl).
Design principles:
* Follow the spec.
Don't scan for local file headers.
Read the central directory for file metadata.
(see [No Streaming Unzip API](#no-streaming-unzip-api)).
* Don't block the JavaScript thread.
Use and provide async APIs.
* Keep memory usage under control.
Don't attempt to buffer entire files in RAM at once.
* Never crash (if used properly).
Don't let malformed zip files bring down client applications who are trying to catch errors.
* Catch unsafe file names.
See `validateFileName()`.
## Usage
```js
var yauzl = require("yauzl");
yauzl.open("path/to/file.zip", {lazyEntries: true}, function(err, zipfile) {
if (err) throw err;
zipfile.readEntry();
zipfile.on("entry", function(entry) {
if (/\/$/.test(entry.fileName)) {
// Directory file names end with '/'.
// Note that entires for directories themselves are optional.
// An entry's fileName implicitly requires its parent directories to exist.
} else {
// file entry
zipfile.openReadStream(entry, function(err, readStream) {
if (err) throw err;
readStream.on("end", function() {
zipfile.readEntry();
});
readStream.pipe(somewhere);
});
}
});
});
```
See also `examples/` for more usage examples.
## API
The default for every optional `callback` parameter is:
```js
function defaultCallback(err) {
if (err) throw err;
}
```
### open(path, [options], [callback])
Calls `fs.open(path, "r")` and reads the `fd` effectively the same as `fromFd()` would.
`options` may be omitted or `null`. The defaults are `{autoClose: true, lazyEntries: false, decodeStrings: true, validateEntrySizes: true}`.
`autoClose` is effectively equivalent to:
```js
zipfile.once("end", function() {
zipfile.close();
});
```
`lazyEntries` indicates that entries should be read only when `readEntry()` is called.
If `lazyEntries` is `false`, `entry` events will be emitted as fast as possible to allow `pipe()`ing
file data from all entries in parallel.
This is not recommended, as it can lead to out of control memory usage for zip files with many entries.
See [issue #22](https://github.com/thejoshwolfe/yauzl/issues/22).
If `lazyEntries` is `true`, an `entry` or `end` event will be emitted in response to each call to `readEntry()`.
This allows processing of one entry at a time, and will keep memory usage under control for zip files with many entries.
`decodeStrings` is the default and causes yauzl to decode strings with `CP437` or `UTF-8` as required by the spec.
The exact effects of turning this option off are:
* `zipfile.comment`, `entry.fileName`, and `entry.fileComment` will be `Buffer` objects instead of `String`s.
* Any Info-ZIP Unicode Path Extra Field will be ignored. See `extraFields`.
* Automatic file name validation will not be performed. See `validateFileName()`.
`validateEntrySizes` is the default and ensures that an entry's reported uncompressed size matches its actual uncompressed size.
This check happens as early as possible, which is either before emitting each `"entry"` event (for entries with no compression),
or during the `readStream` piping after calling `openReadStream()`.
See `openReadStream()` for more information on defending against zip bomb attacks.
The `callback` is given the arguments `(err, zipfile)`.
An `err` is provided if the End of Central Directory Record cannot be found, or if its metadata appears malformed.
This kind of error usually indicates that this is not a zip file.
Otherwise, `zipfile` is an instance of `ZipFile`.
### fromFd(fd, [options], [callback])
Reads from the fd, which is presumed to be an open .zip file.
Note that random access is required by the zip file specification,
so the fd cannot be an open socket or any other fd that does not support random access.
`options` may be omitted or `null`. The defaults are `{autoClose: false, lazyEntries: false, decodeStrings: true, validateEntrySizes: true}`.
See `open()` for the meaning of the options and callback.
### fromBuffer(buffer, [options], [callback])
Like `fromFd()`, but reads from a RAM buffer instead of an open file.
`buffer` is a `Buffer`.
If a `ZipFile` is acquired from this method,
it will never emit the `close` event,
and calling `close()` is not necessary.
`options` may be omitted or `null`. The defaults are `{lazyEntries: false, decodeStrings: true, validateEntrySizes: true}`.
See `open()` for the meaning of the options and callback.
The `autoClose` option is ignored for this method.
### fromRandomAccessReader(reader, totalSize, [options], [callback])
This method of reading a zip file allows clients to implement their own back-end file system.
For example, a client might translate read calls into network requests.
The `reader` parameter must be of a type that is a subclass of
[RandomAccessReader](#class-randomaccessreader) that implements the required methods.
The `totalSize` is a Number and indicates the total file size of the zip file.
`options` may be omitted or `null`. The defaults are `{autoClose: true, lazyEntries: false, decodeStrings: true, validateEntrySizes: true}`.
See `open()` for the meaning of the options and callback.
### dosDateTimeToDate(date, time)
Converts MS-DOS `date` and `time` data into a JavaScript `Date` object.
Each parameter is a `Number` treated as an unsigned 16-bit integer.
Note that this format does not support timezones,
so the returned object will use the local timezone.
### validateFileName(fileName)
Returns `null` or a `String` error message depending on the validity of `fileName`.
If `fileName` starts with `"/"` or `/[A-Za-z]:\//` or if it contains `".."` path segments or `"\\"`,
this function returns an error message appropriate for use like this:
```js
var errorMessage = yauzl.validateFileName(fileName);
if (errorMessage != null) throw new Error(errorMessage);
```
This function is automatically run for each entry, as long as `decodeStrings` is `true`.
See `open()` and `Event: "entry"` for more information.
### Class: ZipFile
The constructor for the class is not part of the public API.
Use `open()`, `fromFd()`, `fromBuffer()`, or `fromRandomAccessReader()` instead.
#### Event: "entry"
Callback gets `(entry)`, which is an `Entry`.
See `open()` and `readEntry()` for when this event is emitted.
If `decodeStrings` is `true`, entries emitted via this event have already passed file name validation.
See `validateFileName()` and `open()` for more information.
If `validateEntrySizes` is `true` and this entry's `compressionMethod` is `0` (stored without compression),
this entry has already passed entry size validation.
See `open()` for more information.
#### Event: "end"
Emitted after the last `entry` event has been emitted.
See `open()` and `readEntry()` for more info on when this event is emitted.
#### Event: "close"
Emitted after the fd is actually closed.
This is after calling `close()` (or after the `end` event when `autoClose` is `true`),
and after all stream pipelines created from `openReadStream()` have finished reading data from the fd.
If this `ZipFile` was acquired from `fromRandomAccessReader()`,
the "fd" in the previous paragraph refers to the `RandomAccessReader` implemented by the client.
If this `ZipFile` was acquired from `fromBuffer()`, this event is never emitted.
#### Event: "error"
Emitted in the case of errors with reading the zip file.
(Note that other errors can be emitted from the streams created from `openReadStream()` as well.)
After this event has been emitted, no further `entry`, `end`, or `error` events will be emitted,
but the `close` event may still be
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
vscode c/c++ 扩展 (515个子文件)
msvsmon.exe.config 2KB
OpenDebugAD7.exe.config 745B
vsdbg.exe.config 662B
CppDebug.dll 2.7MB
vsdebugeng.dll 2.05MB
vsdebugeng.impl.dll 1.85MB
vsdebugeng.impl.dll 1.62MB
vsdebugeng.dll 1.48MB
msdia140.dll 1.38MB
Microsoft.VisualStudio.Debugger.Engine.dll 1.3MB
Microsoft.VisualStudio.Debugger.Engine.dll 1.3MB
msvcdis140.dll 1.22MB
msdia140.dll 1.11MB
msvcdis140.dll 975KB
ucrtbase.dll 863KB
ucrtbase.dll 863KB
msvcp140.dll 626KB
VSDebugUI.dll 589KB
vsdbg.dll 491KB
Newtonsoft.Json.dll 458KB
vsdebugeng.manimpl.dll 442KB
vsdebugeng.manimpl.dll 442KB
msvcp140.dll 430KB
DebuggerProxy.dll 427KB
Microsoft.MIDebugEngine.dll 325KB
msdbgui.dll 296KB
Microsoft.VisualStudio.Debugger.Metadata.dll 255KB
Microsoft.VisualStudio.Debugger.Metadata.dll 255KB
CppDebug.Resources.dll 231KB
Microsoft.MICore.dll 222KB
Microsoft.VisualStudio.Debugger.InteropA.dll 132KB
symsrv.dll 128KB
cppdebug.remote.dll 111KB
SymbolLocator.dll 100KB
cppdebug.remote.dll 91KB
vcruntime140.dll 86KB
Microsoft.MICore.XmlSerializers.dll 84KB
vcruntime140.dll 82KB
symbollocator.dll 79KB
vsdebugeng.impl.resources.dll 68KB
api-ms-win-crt-private-l1-1-0.dll 63KB
vsdebugeng.impl.resources.dll 61KB
api-ms-win-crt-private-l1-1-0.dll 61KB
Microsoft.DebugEngineHost.dll 38KB
Microsoft.VisualStudio.Debugger.Interop.10.0.dll 37KB
Microsoft.VisualStudio.Debugger.Interop.11.0.dll 37KB
vsdbg.resources.dll 31KB
vsbaseservices.dll 30KB
Microsoft.VisualStudio.Debugger.Interop.12.0.dll 23KB
api-ms-win-crt-math-l1-1-0.dll 22KB
symbollocator.resources.dll 21KB
api-ms-win-crt-math-l1-1-0.dll 20KB
api-ms-win-crt-multibyte-l1-1-0.dll 19KB
api-ms-win-crt-multibyte-l1-1-0.dll 19KB
api-ms-win-crt-stdio-l1-1-0.dll 17KB
api-ms-win-crt-string-l1-1-0.dll 17KB
api-ms-win-crt-stdio-l1-1-0.dll 17KB
api-ms-win-crt-string-l1-1-0.dll 17KB
api-ms-win-crt-runtime-l1-1-0.dll 16KB
api-ms-win-crt-runtime-l1-1-0.dll 16KB
api-ms-win-crt-convert-l1-1-0.dll 15KB
api-ms-win-crt-convert-l1-1-0.dll 15KB
api-ms-win-core-file-l1-1-0.dll 15KB
api-ms-win-core-file-l1-1-0.dll 15KB
api-ms-win-crt-time-l1-1-0.dll 14KB
api-ms-win-crt-time-l1-1-0.dll 14KB
api-ms-win-core-localization-l1-2-0.dll 14KB
api-ms-win-core-localization-l1-2-0.dll 14KB
api-ms-win-core-synch-l1-1-0.dll 13KB
api-ms-win-crt-filesystem-l1-1-0.dll 13KB
api-ms-win-core-synch-l1-1-0.dll 13KB
api-ms-win-core-processthreads-l1-1-0.dll 13KB
api-ms-win-core-processthreads-l1-1-0.dll 13KB
api-ms-win-crt-filesystem-l1-1-0.dll 13KB
api-ms-win-core-processenvironment-l1-1-0.dll 12KB
api-ms-win-core-sysinfo-l1-1-0.dll 12KB
api-ms-win-core-processenvironment-l1-1-0.dll 12KB
api-ms-win-crt-conio-l1-1-0.dll 12KB
api-ms-win-crt-heap-l1-1-0.dll 12KB
api-ms-win-crt-process-l1-1-0.dll 12KB
api-ms-win-crt-process-l1-1-0.dll 12KB
api-ms-win-crt-heap-l1-1-0.dll 12KB
api-ms-win-core-sysinfo-l1-1-0.dll 12KB
api-ms-win-crt-conio-l1-1-0.dll 12KB
api-ms-win-core-libraryloader-l1-1-0.dll 12KB
api-ms-win-crt-environment-l1-1-0.dll 12KB
api-ms-win-crt-utility-l1-1-0.dll 12KB
api-ms-win-core-memory-l1-1-0.dll 12KB
api-ms-win-core-synch-l1-2-0.dll 12KB
api-ms-win-core-processthreads-l1-1-1.dll 12KB
api-ms-win-core-interlocked-l1-1-0.dll 12KB
api-ms-win-core-console-l1-1-0.dll 12KB
api-ms-win-core-console-l1-1-0.dll 12KB
api-ms-win-core-libraryloader-l1-1-0.dll 12KB
api-ms-win-core-memory-l1-1-0.dll 12KB
api-ms-win-core-processthreads-l1-1-1.dll 12KB
api-ms-win-crt-locale-l1-1-0.dll 12KB
api-ms-win-core-heap-l1-1-0.dll 12KB
api-ms-win-core-heap-l1-1-0.dll 12KB
api-ms-win-crt-environment-l1-1-0.dll 12KB
共 515 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
斑驳儿
- 粉丝: 0
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功