Skip to content

Commit

Permalink
Docs: Improve simple use example [improve]
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Apr 30, 2023
1 parent d9fbf04 commit 4107854
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,25 @@ npm install yauzl-promise

### Simple usage

Unzip all files from ZIP to a directory:

```js
const {pipeline} = require('stream/promises');
const fs = require('fs'),
{pipeline} = require('stream/promises');

const zipFile = await yauzl.open('/path/to/file.zip');
try {
for await (const entry of zipFile) {
if (entry.fileName.endsWith('/')) continue;
const readStream = await entry.openReadStream();
await pipeline(readStream, writeStream);
if (entry.fileName.endsWith('/')) {
await fs.promises.mkdir(`/path/to/output/${entry.fileName}`);
} else {
const readStream = await entry.openReadStream();
const writeStream = fs.createWriteStream(`/path/to/output/${entry.fileName}`);
await pipeline(readStream, writeStream);
}
}
} finally {
await zipfile.close();
await zipFile.close();
}
```

Expand Down

0 comments on commit 4107854

Please sign in to comment.