diff --git a/.travis.yml b/.travis.yml index 4249139..7566d4c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,8 +3,8 @@ sudo: false language: node_js node_js: + - 8 - 6 - - 4 cache: directories: @@ -13,3 +13,6 @@ cache: install: - npm i -g npm@latest - npm install + +script: + - npm test diff --git a/adm-zip.js b/adm-zip.js index a55d794..92845be 100644 --- a/adm-zip.js +++ b/adm-zip.js @@ -358,7 +358,7 @@ module.exports = function (/*String*/input) { var entryName = item.entryName; - var target = sanitize(targetPath, pth.resolve(targetPath, maintainEntryPath ? entryName : pth.basename(entryName))); + var target = sanitize(targetPath, maintainEntryPath ? entryName : pth.basename(entryName)); if (item.isDirectory) { target = pth.resolve(target, ".."); @@ -369,9 +369,9 @@ module.exports = function (/*String*/input) { if (!content) { throw Utils.Errors.CANT_EXTRACT_FILE; } - var childName = sanitize(targetPath, child.entryName); + var childName = sanitize(targetPath, maintainEntryPath ? child.entryName : pth.basename(child.entryName)); - Utils.writeFileTo(pth.resolve(targetPath, maintainEntryPath ? childName : childName.substr(entryName.length)), content, overwrite); + Utils.writeFileTo(childName, content, overwrite); }); return true; } diff --git a/package.json b/package.json index af51901..dde3206 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,9 @@ "name": "adm-zip", "version": "0.4.12", "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", + "scripts": { + "test": "mocha test/mocha.js" + }, "keywords": [ "zip", "methods", @@ -30,5 +33,10 @@ }, "engines": { "node": ">=0.3.0" + }, + "devDependencies": { + "chai": "^4.1.2", + "mocha": "^5.2.0", + "rimraf": "^2.6.2" } -} \ No newline at end of file +} diff --git a/test/assets/issue-237-Twizzeld.zip b/test/assets/issue-237-Twizzeld.zip new file mode 100644 index 0000000..6155645 Binary files /dev/null and b/test/assets/issue-237-Twizzeld.zip differ diff --git a/test/mocha.js b/test/mocha.js new file mode 100644 index 0000000..755e620 --- /dev/null +++ b/test/mocha.js @@ -0,0 +1,115 @@ +const {expect} = require('chai'); +const Attr = require("../util").FileAttr; +const Zip = require("../adm-zip"); +const pth = require("path"); +const fs = require("fs"); +const rimraf = require("rimraf") + +describe('adm-zip', () => { + + const destination = './test/xxx' + + beforeEach(done => { + rimraf(destination, err => { + if (err) return done(err) + console.log('Cleared directory: ' + destination) + return done() + }) + }) + + it('zip.extractAllTo()', () => { + const zip = new Zip('./test/assets/ultra.zip'); + zip.extractAllTo(destination); + const files = walk(destination) + + expect(files.sort()).to.deep.equal([ + "./test/xxx/attributes_test/asd/New Text Document.txt", + "./test/xxx/attributes_test/blank file.txt", + "./test/xxx/attributes_test/New folder/hidden.txt", + "./test/xxx/attributes_test/New folder/hidden_readonly.txt", + "./test/xxx/attributes_test/New folder/readonly.txt", + "./test/xxx/utes_test/New folder/somefile.txt" + ].sort()); + }) + + it('zip.extractEntryTo(entry, destination, false, true)', () => { + const destination = './test/xxx' + const zip = new Zip('./test/assets/ultra.zip'); + var zipEntries = zip.getEntries(); + zipEntries.forEach(e => zip.extractEntryTo(e, destination, false, true)); + + const files = walk(destination) + expect(files.sort()).to.deep.equal([ + "./test/xxx/blank file.txt", + "./test/xxx/hidden.txt", + "./test/xxx/hidden_readonly.txt", + "./test/xxx/New Text Document.txt", + "./test/xxx/readonly.txt", + "./test/xxx/somefile.txt" + ].sort()); + }) + + it('zip.extractEntryTo(entry, destination, true, true)', () => { + const destination = './test/xxx' + const zip = new Zip('./test/assets/ultra.zip'); + var zipEntries = zip.getEntries(); + zipEntries.forEach(e => zip.extractEntryTo(e, destination, true, true)); + + const files = walk(destination) + expect(files.sort()).to.deep.equal([ + "./test/xxx/attributes_test/asd/New Text Document.txt", + "./test/xxx/attributes_test/blank file.txt", + "./test/xxx/attributes_test/New folder/hidden.txt", + "./test/xxx/attributes_test/New folder/hidden_readonly.txt", + "./test/xxx/attributes_test/New folder/readonly.txt", + "./test/xxx/utes_test/New folder/somefile.txt" + ].sort()); + }) + + it('passes issue-237-Twizzeld test case', () => { + const zip = new Zip('./test/assets/issue-237-Twizzeld.zip'); + const zipEntries = zip.getEntries(); + zipEntries.forEach(function (zipEntry) { + if (!zipEntry.isDirectory) { + zip.extractEntryTo(zipEntry, './', false, true); + // This should create text.txt on the desktop. + // It will actually create two, but the first is overwritten by the second. + } + }); + let text = fs.readFileSync('./text.txt').toString() + expect(text).to.equal('ride em cowboy!') + fs.unlinkSync('./text.txt') + }) +}) + +function walk(dir) { + let results = []; + const list = fs.readdirSync(dir); + list.forEach(function (file) { + file = dir + '/' + file; + const stat = fs.statSync(file); + if (stat && stat.isDirectory()) { + /* Recurse into a subdirectory */ + results = results.concat(walk(file)); + } else { + /* Is a file */ + results.push(file); + } + }); + return results; +} + +function walkD(dir) { + let results = []; + const list = fs.readdirSync(dir); + list.forEach(function (file) { + file = dir + '/' + file; + const stat = fs.statSync(file); + if (stat && stat.isDirectory()) { + /* Recurse into a subdirectory */ + results = results.concat(walk(file)); + results.push(file); + } + }); + return results; +} \ No newline at end of file