Permalink
Browse files

Adding different zip module

  • Loading branch information...
1 parent bff2a69 commit dbafad12b3bac085b06cc1ae8652cac65e8aa05f @donnfelker donnfelker committed Sep 11, 2012
Showing with 48 additions and 7 deletions.
  1. +1 −0 .gitignore
  2. +2 −1 package.json
  3. +45 −6 routes/generate.js
View
@@ -13,5 +13,6 @@ results
node_modules
npm-debug.log
+tmp
#android-bootstrap
View
@@ -9,7 +9,8 @@
"express": "3.0.0rc3",
"jade": "*",
"wrench": "~1.3.9",
- "async": "~0.1.22"
+ "async": "~0.1.22",
+ "node-native-zip": "git://github.com/donnfelker/node-native-zip.git#master"
},
"engines": {
"node": "0.8.x",
View
@@ -2,7 +2,8 @@ var wrench = require('wrench'),
util = require('util'),
spawn = require('child_process').spawn,
fs = require('fs'),
- async = require('async');
+ async = require('async'),
+ zip = require("node-native-zip");
/*
* Project generator route.
*/
@@ -64,18 +65,56 @@ exports.index = function(req, res) {
createSourceDirectories(destDir, packageName);
copySourceDirectories(destDir, packageName);
removeBootstrapDirectories(destDir);
-
- sendZipToResponse(res, destDir, function() {
- wrench.rmdirSyncRecursive(destDir, false);
- });
-
+
+ sendContentAsZip(destDir, res);
+
}
});
// res.json(200, { message : "done"});
// return;
}
+function sendContentAsZip(destDir, res) {
+
+ var fileObjects = getFileObjectsFrom(destDir, wrench.readdirSyncRecursive(destDir));
+
+ var archive = new zip();
+ archive.addFiles(fileObjects, function(err) {
+ if(err) {
+ console.log(err);
+ res.statusCode = 500;
+ res.end();
+ } else {
+
+ archive.toBuffer(function(buff) {
+
+ res.contentType('zip');
+ res.setHeader('Content-disposition', 'attachment; filename=android-bootstrap.zip');
+ res.send(buff);
+ res.end();
+
+ wrench.rmdirSyncRecursive(destDir, false)
+ });
+
+
+ }
+
+ });
+
+}
+
+function getFileObjectsFrom(destDir, files) {
+ var fileObjs = []
+ for(var i=0; i<files.length;i++) {
+ var filePath = destDir + "/" + files[i];
+ var stats = fs.lstatSync(filePath);
+ if(!stats.isDirectory())
+ fileObjs.push({ name: files[i], path: filePath });
+ }
+ return fileObjs;
+}
+
function generateFileFunc(file, packageName, appName) {
return function(callback) {
generateFile(file, packageName, appName, callback);

0 comments on commit dbafad1

Please sign in to comment.