├── .gitignore ├── Makefile ├── README.md ├── bin ├── nodezip └── packer ├── lib ├── nodezip-cli.js └── nodezip.js ├── package.json └── test └── nodezip_spec.js /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | node_modules 15 | npm-debug.log 16 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | bin/packer 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # node-zip 2 | 3 | 4 | node-zip - Zip/Unzip files ported from JSZip 5 | 6 | ## Installation 7 | 8 | 9 | ```sh 10 | npm install node-zip 11 | ``` 12 | 13 | ## Usage 14 | 15 | ### Complete example, zip multiple files 16 | 17 | ```js 18 | var fs = require('fs'); 19 | var path = require('path'); 20 | 21 | // The zip library needs to be instantiated: 22 | var zip = new require('node-zip')(); 23 | 24 | // You can add multiple files by performing subsequent calls to zip.file(); 25 | // the first argument is how you want the file to be named inside your zip, 26 | // the second is the actual data: 27 | zip.file('file1.txt', fs.readFileSync(path.join(__dirname, 'file1.txt'))); 28 | zip.file('file2.txt', fs.readFileSync(path.join(__dirname, 'file2.txt'))); 29 | 30 | var data = zip.generate({ base64:false, compression: 'DEFLATE' }); 31 | 32 | // it's important to use *binary* encode 33 | fs.writeFileSync('test.zip', data, 'binary'); 34 | ``` 35 | 36 | ------------ 37 | 38 | You can also load directly: 39 | 40 | ```js 41 | require('node-zip'); 42 | var zip = new JSZip(data, options) 43 | ... 44 | ``` 45 | 46 | ### Zip text into file: 47 | 48 | ```js 49 | var zip = new require('node-zip')(); 50 | 51 | zip.file('test.file', 'hello there'); 52 | var data = zip.generate({base64:false,compression:'DEFLATE'}); 53 | console.log(data); // ugly data 54 | ``` 55 | 56 | ### Unzip: 57 | 58 | ```js 59 | var zip = new require('node-zip')(data, {base64: false, checkCRC32: true}); 60 | console.log(zip.files['test.file']); // hello there 61 | ``` 62 | 63 | ### Write to a file (IMPORTANT: use *binary* encode, thanks to @Acek) 64 | 65 | ```js 66 | var fs = require("fs"); 67 | zip.file('test.txt', 'hello there'); 68 | var data = zip.generate({base64:false,compression:'DEFLATE'}); 69 | fs.writeFileSync('test.zip', data, 'binary'); 70 | ``` 71 | ## Testing 72 | 73 | ```sh 74 | npm install -g jasmine-node 75 | jasmine-node test 76 | ``` 77 | 78 | ## Manual 79 | 80 | node-zip uses JSZip, please refer to their website for further information: 81 | http://stuartk.com/jszip/ 82 | 83 | ## Contributors 84 | 85 | > David Duponchel [@dduponchel](https://github.com/dduponchel) 86 | 87 | Feel free to send your pull requests and contribute to this project 88 | 89 | ## License 90 | 91 | MIT -------------------------------------------------------------------------------- /bin/nodezip: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | var fs=require("fs"),JSZip=require("jszip"),path=require("path"),args=process.argv.slice(2),zip=new JSZip;if(/-h|-H|--help|-\?/.test(args)||!args.length){printHelp()}else{var command=args.shift();if(command=="-c"){var zipfile=args.shift();console.log("Creating %s...",zipfile);args.forEach(function(file){if(fs.existsSync(file)){addFile(file)}else{console.error("Error: file %s not found.",file);process.exit(2)}});console.log("Deflating...");fs.writeFileSync(zipfile,zip.generate({type:"nodebuffer",compression:"DEFLATE"}));console.log("Done.")}else if(command=="-x"){var zipfile=args.shift();var destination=args.shift();var zipdata=fs.readFileSync(zipfile);console.log("Reading %s...",zipfile);try{var zip=new JSZip(zipdata,{checkCRC32:true})}catch(e){console.error("Error: invalid file");process.exit(2)}Object.keys(zip.files).forEach(function(filepath){file=zip.files[filepath];if(destination)filepath=destination+path.sep+filepath;if(file.options.dir){console.log(" Creating",filepath);mkdirRecursively(filepath)}else{console.log(" Inflating",filepath);fs.writeFileSync(filepath,file.asNodeBuffer())}});console.log("Done.")}else{console.error("Error: wrong command");printHelp()}}function printHelp(){console.error("Usage:");console.error(" -c zipfile file1 [file2] [...] Create zip file with file/directory list");console.error(" -x zipfile [destination] Extract zip file");console.error(" -h | -H | --help | -? Show help");process.exit(1)}function addFile(filepath){if(fs.lstatSync(filepath).isDirectory()){console.log(" Adding folder",filepath);zip.folder(filepath);var directory=fs.readdirSync(filepath);directory.forEach(function(subfilepath){addFile(path.join(filepath,subfilepath))})}else{console.log(" Adding file",filepath);zip.file(filepath,fs.readFileSync(filepath,"binary"))}}function mkdirRecursively(folderpath,mode){try{fs.mkdirSync(folderpath,mode);return true}catch(e){if(e.errno==34){mkdirRecursively(path.dirname(folderpath),mode);mkdirRecursively(folderpath,mode)}else if(e.errno==47){return true}else{console.log("Error: Unable to create folder %s (errno: %s)",folderpath,e.errno);process.exit(2)}}} -------------------------------------------------------------------------------- /bin/packer: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | type -P uglifyjs &>/dev/null || { npm install uglify-js -g; } 3 | echo "#!/usr/bin/env node" > bin/nodezip 4 | cat lib/nodezip-cli.js >> bin/nodezip.tmp 5 | uglifyjs bin/nodezip.tmp >> bin/nodezip 6 | rm bin/nodezip.tmp 7 | chmod +x bin/nodezip 8 | echo "done: bin/nodezip" -------------------------------------------------------------------------------- /lib/nodezip-cli.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'), 2 | JSZip = require('jszip'), 3 | path = require('path'), 4 | args = process.argv.slice(2), 5 | zip = new JSZip(); 6 | 7 | if(/-h|-H|--help|-\?/.test(args)||!args.length) { 8 | printHelp(); 9 | } else { 10 | var command = args.shift(); 11 | if(command == '-c') { 12 | var zipfile = args.shift(); 13 | console.log('Creating %s...', zipfile); 14 | args.forEach(function(file) { 15 | if(fs.existsSync(file)) { 16 | addFile(file); 17 | } else { 18 | console.error('Error: file %s not found.', file); 19 | process.exit(2); 20 | } 21 | }); 22 | console.log("Deflating...") 23 | fs.writeFileSync(zipfile, zip.generate({type:"nodebuffer", compression:'DEFLATE'})); 24 | console.log("Done.") 25 | } else if(command == '-x') { 26 | var zipfile = args.shift(); 27 | var destination = args.shift(); 28 | var zipdata = fs.readFileSync(zipfile); 29 | console.log('Reading %s...', zipfile); 30 | try { 31 | var zip = new JSZip(zipdata, {checkCRC32: true}); 32 | } catch(e) { 33 | console.error("Error: invalid file"); 34 | process.exit(2); 35 | } 36 | Object.keys(zip.files).forEach(function(filepath) { 37 | file = zip.files[filepath]; 38 | if (destination) filepath = destination + path.sep + filepath 39 | if(file.options.dir) { 40 | console.log(' Creating', filepath); 41 | mkdirRecursively(filepath); 42 | } else { 43 | console.log(' Inflating', filepath); 44 | // TODO: add prompt if file exists 45 | fs.writeFileSync(filepath, file.asNodeBuffer()); 46 | } 47 | }); 48 | console.log('Done.'); 49 | } else { 50 | console.error('Error: wrong command') 51 | printHelp(); 52 | } 53 | } 54 | 55 | function printHelp() { 56 | console.error('Usage:'); 57 | console.error(' -c zipfile file1 [file2] [...] Create zip file with file/directory list'); 58 | console.error(' -x zipfile [destination] Extract zip file'); 59 | console.error(' -h | -H | --help | -? Show help'); 60 | process.exit(1); 61 | } 62 | 63 | function addFile(filepath) { 64 | if(fs.lstatSync(filepath).isDirectory()) { 65 | console.log(" Adding folder", filepath); 66 | zip.folder(filepath); 67 | var directory = fs.readdirSync(filepath); 68 | directory.forEach(function(subfilepath) { 69 | addFile(path.join(filepath,subfilepath)); 70 | }); 71 | } else { 72 | console.log(" Adding file", filepath) 73 | zip.file(filepath, fs.readFileSync(filepath, 'binary')); 74 | } 75 | } 76 | 77 | function mkdirRecursively(folderpath, mode) { 78 | try { 79 | fs.mkdirSync(folderpath, mode); 80 | return true; 81 | } catch(e) { 82 | if (e.errno == 34) { 83 | mkdirRecursively(path.dirname(folderpath), mode); 84 | mkdirRecursively(folderpath, mode); 85 | } else if (e.errno == 47) { 86 | return true; 87 | } else { 88 | console.log("Error: Unable to create folder %s (errno: %s)", folderpath, e.errno) 89 | process.exit(2); 90 | } 91 | } 92 | }; 93 | -------------------------------------------------------------------------------- /lib/nodezip.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var JSZip = require('jszip'); 3 | 4 | global.JSZip = JSZip; 5 | module.exports = function(data, options) { return new JSZip(data, options) }; 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "Diego Araos (http://wehack.it/)", 3 | "name": "node-zip", 4 | "description": "node-zip - Zip/Unzip files ported from JSZip", 5 | "version": "1.1.1", 6 | "homepage": "https://github.com/daraosn/node-zip", 7 | "repository": { 8 | "url": "git://github.com/daraosn/node-zip.git" 9 | }, 10 | "license": "MIT", 11 | "contributors": [ 12 | { 13 | "name": "David Duponchel", 14 | "email": "d.duponchel@gmail.com" 15 | } 16 | ], 17 | "keywords": [ 18 | "zip", 19 | "unzip", 20 | "jszip", 21 | "node-zip", 22 | "compression" 23 | ], 24 | "bin": { 25 | "nodezip": "bin/nodezip" 26 | }, 27 | "main": "lib/nodezip.js", 28 | "dependencies": { 29 | "jszip" : "2.5.0" 30 | }, 31 | "devDependencies": {}, 32 | "optionalDependencies": {}, 33 | "engines": { 34 | "node": "*" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /test/nodezip_spec.js: -------------------------------------------------------------------------------- 1 | describe('nodezip', function() { 2 | beforeEach(function() { 3 | this.nodezip = require('..')(); 4 | }); 5 | 6 | describe('when initialized', function() { 7 | it('should load JSZip in this.nodezip', function() { 8 | expect(this.nodezip.options).not.toBeNull(); 9 | }); 10 | 11 | it('should declare JSZip', function() { 12 | expect(JSZip).not.toBeNull(); 13 | }); 14 | }); 15 | 16 | describe('when archiving a dummy file', function() { 17 | beforeEach(function() { 18 | this.fs = require("fs"); 19 | this.dummyFile = this.nodezip.file('test.file', 'hello there'); 20 | this.dummyFileData = this.dummyFile.generate({base64:false,compression:'DEFLATE'}); 21 | }); 22 | 23 | it('should contain valid data', function() { 24 | expect(this.dummyFileData).not.toBeNull(); 25 | expect(this.dummyFileData).toMatch(/^PK/); 26 | expect(this.dummyFileData).toMatch(/test.file/); 27 | }); 28 | 29 | it('should be able to write file', function() { 30 | this.fs.writeFileSync('test.zip', this.dummyFileData, 'binary'); 31 | expect(this.fs.lstatSync('test.zip')).not.toBeNull() 32 | }); 33 | 34 | it('should be able to deflate file', function() { 35 | this.dummyFileData = this.fs.readFileSync('test.zip', 'binary'); 36 | this.dummyFile = new JSZip(this.dummyFileData, {base64: false, checkCRC32: true}); 37 | expect(this.dummyFile.files['test.file'].asText()).toEqual("hello there"); 38 | this.fs.unlink('test.zip'); 39 | }); 40 | }); 41 | }); --------------------------------------------------------------------------------