Skip to content

Commit 7f83183

Browse files
authoredFeb 7, 2021
Add custom source option for Open (#223)
* Add Open.custom to provide unzipping from a custom source * Fix readme code block * Tweak readme for Open.custom * Update Open.custom example with Google Cloud Storage This better explains the use-case for using a custom source.
1 parent fddad0a commit 7f83183

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
 

‎README.md

+35
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,41 @@ async function main() {
317317
main();
318318
```
319319

320+
### Open.custom(source, [options])
321+
This function can be used to provide a custom source implementation. The source parameter expects a `stream` and a `size` function to be implemented. The size function should return a `Promise` that resolves the total size of the file. The stream function should return a `Readable` stream according to the supplied offset and length parameters.
322+
323+
Example:
324+
325+
```js
326+
// Custom source implementation for reading a zip file from Google Cloud Storage
327+
const { Storage } = require('@google-cloud/storage');
328+
329+
async function main() {
330+
const storage = new Storage();
331+
const bucket = storage.bucket('my-bucket');
332+
const zipFile = bucket.file('my-zip-file.zip');
333+
334+
const customSource = {
335+
stream: function(offset, length) {
336+
return zipFile.createReadStream({
337+
start: offset,
338+
end: length && offset + length
339+
})
340+
},
341+
size: async function() {
342+
const objMetadata = (await zipFile.getMetadata())[0];
343+
return objMetadata.size;
344+
}
345+
};
346+
347+
const directory = await unzipper.Open.custom(customSource);
348+
console.log('directory', directory);
349+
// ...
350+
}
351+
352+
main();
353+
```
354+
320355
### Open.[method].extract()
321356

322357
The directory object returned from `Open.[method]` provides an `extract` method which extracts all the files to a specified `path`, with an optional `concurrency` (default: 1).

‎lib/Open/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ module.exports = {
9292
}
9393
};
9494

95+
return directory(source, options);
96+
},
97+
98+
custom: function(source, options) {
9599
return directory(source, options);
96100
}
97101
};

‎test/openCustom.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
var test = require('tap').test;
4+
var fs = require('fs');
5+
var path = require('path');
6+
var unzip = require('../unzip');
7+
var Promise = require('bluebird');
8+
9+
test("get content of a single file entry out of a zip", function (t) {
10+
var archive = path.join(__dirname, '../testData/compressed-standard/archive.zip');
11+
12+
var customSource = {
13+
stream: function(offset,length) {
14+
return fs.createReadStream(archive, {start: offset, end: length && offset+length});
15+
},
16+
size: function() {
17+
return new Promise(function(resolve, reject) {
18+
fs.stat(archive, function(err, d) {
19+
if (err)
20+
reject(err);
21+
else
22+
resolve(d.size);
23+
});
24+
});
25+
}
26+
};
27+
28+
return unzip.Open.custom(customSource)
29+
.then(function(d) {
30+
var file = d.files.filter(function(file) {
31+
return file.path == 'file.txt';
32+
})[0];
33+
34+
return file.buffer()
35+
.then(function(str) {
36+
var fileStr = fs.readFileSync(path.join(__dirname, '../testData/compressed-standard/inflated/file.txt'), 'utf8');
37+
t.equal(str.toString(), fileStr);
38+
t.end();
39+
});
40+
});
41+
});

0 commit comments

Comments
 (0)
Please sign in to comment.