Skip to content

Commit c31dcd4

Browse files
authored
Merge pull request #238 from apexearth/master
Fix #237 and other issues with .extractEntryTo()
2 parents b73d0c8 + bdc6eac commit c31dcd4

File tree

5 files changed

+131
-5
lines changed

5 files changed

+131
-5
lines changed

Diff for: .travis.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ sudo: false
33
language: node_js
44

55
node_js:
6+
- 8
67
- 6
7-
- 4
88

99
cache:
1010
directories:
@@ -13,3 +13,6 @@ cache:
1313
install:
1414
- npm i -g npm@latest
1515
- npm install
16+
17+
script:
18+
- npm test

Diff for: adm-zip.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ module.exports = function (/*String*/input) {
358358

359359
var entryName = item.entryName;
360360

361-
var target = sanitize(targetPath, pth.resolve(targetPath, maintainEntryPath ? entryName : pth.basename(entryName)));
361+
var target = sanitize(targetPath, maintainEntryPath ? entryName : pth.basename(entryName));
362362

363363
if (item.isDirectory) {
364364
target = pth.resolve(target, "..");
@@ -369,9 +369,9 @@ module.exports = function (/*String*/input) {
369369
if (!content) {
370370
throw Utils.Errors.CANT_EXTRACT_FILE;
371371
}
372-
var childName = sanitize(targetPath, child.entryName);
372+
var childName = sanitize(targetPath, maintainEntryPath ? child.entryName : pth.basename(child.entryName));
373373

374-
Utils.writeFileTo(pth.resolve(targetPath, maintainEntryPath ? childName : childName.substr(entryName.length)), content, overwrite);
374+
Utils.writeFileTo(childName, content, overwrite);
375375
});
376376
return true;
377377
}

Diff for: package.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"name": "adm-zip",
33
"version": "0.4.12",
44
"description": "Javascript implementation of zip for nodejs with support for electron original-fs. Allows user to create or extract zip files both in memory or to/from disk",
5+
"scripts": {
6+
"test": "mocha test/mocha.js"
7+
},
58
"keywords": [
69
"zip",
710
"methods",
@@ -30,5 +33,10 @@
3033
},
3134
"engines": {
3235
"node": ">=0.3.0"
36+
},
37+
"devDependencies": {
38+
"chai": "^4.1.2",
39+
"mocha": "^5.2.0",
40+
"rimraf": "^2.6.2"
3341
}
34-
}
42+
}

Diff for: test/assets/issue-237-Twizzeld.zip

535 Bytes
Binary file not shown.

Diff for: test/mocha.js

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
const {expect} = require('chai');
2+
const Attr = require("../util").FileAttr;
3+
const Zip = require("../adm-zip");
4+
const pth = require("path");
5+
const fs = require("fs");
6+
const rimraf = require("rimraf")
7+
8+
describe('adm-zip', () => {
9+
10+
const destination = './test/xxx'
11+
12+
beforeEach(done => {
13+
rimraf(destination, err => {
14+
if (err) return done(err)
15+
console.log('Cleared directory: ' + destination)
16+
return done()
17+
})
18+
})
19+
20+
it('zip.extractAllTo()', () => {
21+
const zip = new Zip('./test/assets/ultra.zip');
22+
zip.extractAllTo(destination);
23+
const files = walk(destination)
24+
25+
expect(files.sort()).to.deep.equal([
26+
"./test/xxx/attributes_test/asd/New Text Document.txt",
27+
"./test/xxx/attributes_test/blank file.txt",
28+
"./test/xxx/attributes_test/New folder/hidden.txt",
29+
"./test/xxx/attributes_test/New folder/hidden_readonly.txt",
30+
"./test/xxx/attributes_test/New folder/readonly.txt",
31+
"./test/xxx/utes_test/New folder/somefile.txt"
32+
].sort());
33+
})
34+
35+
it('zip.extractEntryTo(entry, destination, false, true)', () => {
36+
const destination = './test/xxx'
37+
const zip = new Zip('./test/assets/ultra.zip');
38+
var zipEntries = zip.getEntries();
39+
zipEntries.forEach(e => zip.extractEntryTo(e, destination, false, true));
40+
41+
const files = walk(destination)
42+
expect(files.sort()).to.deep.equal([
43+
"./test/xxx/blank file.txt",
44+
"./test/xxx/hidden.txt",
45+
"./test/xxx/hidden_readonly.txt",
46+
"./test/xxx/New Text Document.txt",
47+
"./test/xxx/readonly.txt",
48+
"./test/xxx/somefile.txt"
49+
].sort());
50+
})
51+
52+
it('zip.extractEntryTo(entry, destination, true, true)', () => {
53+
const destination = './test/xxx'
54+
const zip = new Zip('./test/assets/ultra.zip');
55+
var zipEntries = zip.getEntries();
56+
zipEntries.forEach(e => zip.extractEntryTo(e, destination, true, true));
57+
58+
const files = walk(destination)
59+
expect(files.sort()).to.deep.equal([
60+
"./test/xxx/attributes_test/asd/New Text Document.txt",
61+
"./test/xxx/attributes_test/blank file.txt",
62+
"./test/xxx/attributes_test/New folder/hidden.txt",
63+
"./test/xxx/attributes_test/New folder/hidden_readonly.txt",
64+
"./test/xxx/attributes_test/New folder/readonly.txt",
65+
"./test/xxx/utes_test/New folder/somefile.txt"
66+
].sort());
67+
})
68+
69+
it('passes issue-237-Twizzeld test case', () => {
70+
const zip = new Zip('./test/assets/issue-237-Twizzeld.zip');
71+
const zipEntries = zip.getEntries();
72+
zipEntries.forEach(function (zipEntry) {
73+
if (!zipEntry.isDirectory) {
74+
zip.extractEntryTo(zipEntry, './', false, true);
75+
// This should create text.txt on the desktop.
76+
// It will actually create two, but the first is overwritten by the second.
77+
}
78+
});
79+
let text = fs.readFileSync('./text.txt').toString()
80+
expect(text).to.equal('ride em cowboy!')
81+
fs.unlinkSync('./text.txt')
82+
})
83+
})
84+
85+
function walk(dir) {
86+
let results = [];
87+
const list = fs.readdirSync(dir);
88+
list.forEach(function (file) {
89+
file = dir + '/' + file;
90+
const stat = fs.statSync(file);
91+
if (stat && stat.isDirectory()) {
92+
/* Recurse into a subdirectory */
93+
results = results.concat(walk(file));
94+
} else {
95+
/* Is a file */
96+
results.push(file);
97+
}
98+
});
99+
return results;
100+
}
101+
102+
function walkD(dir) {
103+
let results = [];
104+
const list = fs.readdirSync(dir);
105+
list.forEach(function (file) {
106+
file = dir + '/' + file;
107+
const stat = fs.statSync(file);
108+
if (stat && stat.isDirectory()) {
109+
/* Recurse into a subdirectory */
110+
results = results.concat(walk(file));
111+
results.push(file);
112+
}
113+
});
114+
return results;
115+
}

0 commit comments

Comments
 (0)