Skip to content

Commit

Permalink
Merge pull request #238 from apexearth/master
Browse files Browse the repository at this point in the history
Fix #237 and other issues with .extractEntryTo()
  • Loading branch information
cthackers committed Oct 11, 2018
2 parents b73d0c8 + bdc6eac commit c31dcd4
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 5 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ sudo: false
language: node_js

node_js:
- 8
- 6
- 4

cache:
directories:
Expand All @@ -13,3 +13,6 @@ cache:
install:
- npm i -g npm@latest
- npm install

script:
- npm test
6 changes: 3 additions & 3 deletions adm-zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, "..");
Expand All @@ -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;
}
Expand Down
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -30,5 +33,10 @@
},
"engines": {
"node": ">=0.3.0"
},
"devDependencies": {
"chai": "^4.1.2",
"mocha": "^5.2.0",
"rimraf": "^2.6.2"
}
}
}
Binary file added test/assets/issue-237-Twizzeld.zip
Binary file not shown.
115 changes: 115 additions & 0 deletions test/mocha.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit c31dcd4

Please sign in to comment.