UNPKG

2.52 kBJavaScriptView Raw
1'use strict';
2
3var test = require('tap').test;
4var fs = require('fs');
5var path = require('path');
6var temp = require('temp');
7var dirdiff = require('dirdiff');
8var streamBuffers = require("stream-buffers");
9var unzip = require('../');
10
11test("uncompressed archive", function (t) {
12 var archive = path.join(__dirname, '../testData/uncompressed/archive.zip');
13
14 temp.mkdir('node-unzip-', function (err, dirPath) {
15 if (err) {
16 throw err;
17 }
18 var unzipExtractor = unzip.Extract({ path: dirPath });
19 unzipExtractor.on('error', function(err) {
20 throw err;
21 });
22 unzipExtractor.on('close', testExtractionResults);
23
24 fs.createReadStream(archive).pipe(unzipExtractor);
25
26 function testExtractionResults() {
27 dirdiff(path.join(__dirname, '../testData/uncompressed/inflated'), dirPath, {
28 fileContents: true
29 }, function (err, diffs) {
30 if (err) {
31 throw err;
32 }
33 t.equal(diffs.length, 0, 'extracted directory contents');
34 t.end();
35 });
36 }
37 });
38});
39
40test("compressed archive", function (t) {
41 var archive = path.join(__dirname, '../testData/compressed/archive.zip');
42
43 temp.mkdir('node-unzip-', function (err, dirPath) {
44 if (err) {
45 throw err;
46 }
47 var unzipExtractor = unzip.Extract({ path: dirPath });
48 unzipExtractor.on('error', function(err) {
49 throw err;
50 });
51 unzipExtractor.on('close', testExtractionResults);
52
53 fs.createReadStream(archive).pipe(unzipExtractor);
54
55 function testExtractionResults() {
56 dirdiff(path.join(__dirname, '../testData/compressed/inflated'), dirPath, {
57 fileContents: true
58 }, function (err, diffs) {
59 if (err) {
60 throw err;
61 }
62 t.equal(diffs.length, 0, 'extracted directory contents');
63 t.end();
64 });
65 }
66 });
67});
68
69test("pipe a single file entry out of a zip", function (t) {
70 var archive = path.join(__dirname, '../testData/compressed/archive.zip');
71
72 fs.createReadStream(archive)
73 .pipe(unzip.Parse())
74 .on('entry', function(entry) {
75 if (entry.path === 'file.txt') {
76 var writableStream = new streamBuffers.WritableStreamBuffer();
77 writableStream.on('close', function () {
78 var str = writableStream.getContentsAsString('utf8');
79 var fileStr = fs.readFileSync(path.join(__dirname, '../testData/compressed/inflated/file.txt'), 'utf8')
80 t.equal(str, fileStr);
81 t.end();
82 });
83 entry.pipe(writableStream);
84 }
85 });
86});
\No newline at end of file