# 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.
A zip file entry throws an error if its file name starts with `"/"` or `/[A-Za-z]:\//`
or if it contains `".."` path segments or `"\\"` (per the spec).
## Usage
```js
var yauzl = require("yauzl");
var fs = require("fs");
var path = require("path");
var mkdirp = require("mkdirp"); // or similar
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 '/'
mkdirp(entry.fileName, function(err) {
if (err) throw err;
zipfile.readEntry();
});
} else {
// file entry
zipfile.openReadStream(entry, function(err, readStream) {
if (err) throw err;
// ensure parent directory exists
mkdirp(path.dirname(entry.fileName), function(err) {
if (err) throw err;
readStream.pipe(fs.createWriteStream(entry.fileName));
readStream.on("end", function() {
zipfile.readEntry();
});
});
});
}
});
});
```
## 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 gives the `fd`, `options`, and `callback` to `fromFd()` below.
`options` may be omitted or `null`. The defaults are `{autoClose: true, lazyEntries: false}`.
`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.
### 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.
The `callback` is given the arguments `(err, zipfile)`.
An `err` is provided if the End of Central Directory Record Signature cannot be found in the file,
which indicates that the fd is not a zip file.
`zipfile` is an instance of `ZipFile`.
`options` may be omitted or `null`. The defaults are `{autoClose: false, lazyEntries: false}`.
See `open()` for the meaning of the options.
### fromBuffer(buffer, [options], [callback])
Like `fromFd()`, but reads from a RAM buffer instead of an open file.
`buffer` is a `Buffer`.
`callback` is effectively passed directly to `fromFd()`.
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}`.
See `open()` for the meaning of the options.
The `autoClose` option is ignored for this method.
### fromRandomAccessReader(reader, totalSize, [options], [callback])
This method of creating 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}`.
See `open()` for the meaning of the options.
### 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.
### 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.
#### 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 emitted.
#### readEntry()
Causes this `ZipFile` to emit an `entry` or `end` event (or an `error` event).
This method must only be called when this `ZipFile` was created with the `lazyEntries` option set to `true` (see `open()`).
When this `ZipFile` was created with the `lazyEntries` option set to `true`,
`entry` and `end` events are only ever emitted in response to this method call.
The event that is emitted in response to this method will not be emitted until after this method has returned,
so it is safe to call this method before attaching event listeners.
After calling this method, calling this method again before the response event has been emitted will cause undefined behavior.
Calling this method after the `end` event has been emitted will cause undefined behavior.
Calling this method after calling `close()` will cause undefined behavior.
#### openReadStream(entry, callback)
`entry` must be an `Entry` object from this `ZipFile`.
`callback` gets `(err, readStream)`, where `readStream` is a `Readable Stream`.
If the entry is compressed (with a supported compression method),
the read stream provides the decompressed data.
If this zipfile is already closed (see `close()`), the `callback` will receive an `err`.
It's possible for the `readStream` it to emit errors for several reasons.
For example, if zlib cannot decompress the data, the zlib error will be emitted from the `readStream`.
Two more error cases are if the decompressed data has too many or too few actual bytes
compared to the reported byte count from the entry's `uncompressedSize` field.
yauzl notices this false information and emits an error from the `rea
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
VSCODE C/C++ 插件离线包 (347个子文件)
Microsoft.VSCode.CPP.Extension.darwin 8.28MB
Newtonsoft.Json.dll 474KB
Microsoft.MIDebugEngine.dll 305KB
Microsoft.MICore.dll 180KB
Microsoft.VisualStudio.Debugger.InteropA.dll 132KB
Microsoft.MICore.XmlSerializers.dll 71KB
Microsoft.VisualStudio.Debugger.Interop.11.0.dll 37KB
Microsoft.VisualStudio.Debugger.Interop.10.0.dll 37KB
Microsoft.DebugEngineHost.dll 37KB
Microsoft.VisualStudio.Debugger.Interop.12.0.dll 23KB
.eslintrc 5KB
.eslintrc 5KB
Microsoft.VSCode.CPP.Extension.exe 5.67MB
OpenDebugAD7.exe 169KB
.gitattributes 71B
AppInsights-node.js.iml 425B
debugSession.js 79KB
debugProtocol.js 59KB
main.js 49KB
protocol.js 38KB
semver.js 32KB
semver.js 32KB
index.js 27KB
index.js 23KB
index.js 23KB
main.js 17KB
protocol.js 15KB
C_Cpp_ConfigurationProperties.js 13KB
registry.js 12KB
test.js 12KB
packageManager.js 11KB
Performance.js 11KB
tmp.js 10KB
test.js 9KB
Contracts.js 9KB
test.js 9KB
protocolConverter.js 9KB
Sender.js 8KB
codeConverter.js 8KB
test.js 8KB
test.js 8KB
messageReader.js 7KB
Client.js 7KB
parse.js 7KB
index.js 7KB
nativeAttach.js 6KB
dir-sync-test.js 6KB
dir-test.js 6KB
events.js 6KB
applicationinsights.js 6KB
Exceptions.js 6KB
electronForkStart.js 6KB
file-test.js 6KB
https-proxy-agent.js 6KB
index.js 5KB
file-sync-test.js 5KB
ltr.js 5KB
ltr.js 5KB
telemetryReporter.js 5KB
extension.js 5KB
gtr.js 5KB
gtr.js 5KB
node.js 5KB
node.js 5KB
messages.js 5KB
common.js 4KB
RequestDataHelper.js 4KB
Util.js 4KB
messageWriter.js 4KB
base.js 4KB
debug.js 4KB
debug.js 4KB
index.js 4KB
browser.js 4KB
browser.js 4KB
main.js 3KB
electron.js 3KB
http-proxy-agent.js 3KB
Requests.js 3KB
Channel.js 3KB
handles.js 3KB
C_Cpp_Feedback.js 3KB
C_Cpp.js 3KB
cancellation.js 3KB
messages.js 3KB
debugProxy.js 3KB
index.js 3KB
agent.js 3KB
agent.js 3KB
Context.js 2KB
test.js 2KB
async.js 2KB
index.js 2KB
index.js 2KB
platform.js 2KB
index.js 2KB
index.js 2KB
crc.test.js 2KB
name-test.js 2KB
linuxDistribution.js 2KB
共 347 条
- 1
- 2
- 3
- 4
资源评论
- LGD_LR2018-09-13还没用不知道能用不
- 确实"用户昵称已存在"2019-01-15但还是需要联网才能正常使用
- cheny52102016-11-03对1.6版本 vscode 无效
xqdoo00o
- 粉丝: 1
- 资源: 32
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功