Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Packaging

  • Loading branch information...
commit fac11e44763a0644b284646f4d79e39e21fc10f8 1 parent f3c8979
Jan Jongboom authored
100 README.md
Source Rendered
... ... @@ -1,18 +1,90 @@
1   -# README for a newly created project.
  1 +# node-native-zip
2 2
3   -There are a couple of things you should do first, before you can use all of Git's power:
  3 +All the current ZIP solutions for node.js are wrappers around existing zip executables, spawning on demand.
  4 +To all of you who rather have a native implementation of zip'ing in javascript there is node-native-zip.
  5 +This package works with `Buffer` objects, which allows you to do complex in-memory stuff with the least
  6 +amount of overhead.
4 7
5   - * Add a remote to this project: in the Cloud9 IDE command line, you can execute the following commands
6   - `git remote add [remote name] [remote url (eg. 'git@github.com:/ajaxorg/node_chat')]` [Enter]
7   - * Create new files inside your project
8   - * Add them to to Git by executing the following command
9   - `git add [file1, file2, file3, ...]` [Enter]
10   - * Create a commit which can be pushed to the remote you just added
11   - `git commit -m 'added new files'` [Enter]
12   - * Push the commit the remote
13   - `git push [remote name] master` [Enter]
  8 +It has been inspired by [JSZip](https://github.com/Stuk/jszip).
14 9
15   -That's it! If this doesn't work for you, please visit the excellent resources from [Github.com](http://help.github.com) and the [Pro Git](http://http://progit.org/book/) book.
16   -If you can't find your answers there, feel free to ask us via Twitter (@cloud9ide), [mailing list](groups.google.com/group/cloud9-ide) or IRC (#cloud9ide on freenode).
  10 +## How to install
17 11
18   -Happy coding!
  12 +Via NPM:
  13 +
  14 + npm install node-native-zip
  15 +
  16 +Via GIT:
  17 +
  18 + git clone git://github.com/janjongboom/node-native-zip.git
  19 +
  20 +## How to use
  21 +
  22 +There are two ways to feed files into a new .zip file. Either by adding `Buffer` objects, or by adding
  23 +an array of files.
  24 +
  25 +### Adding Buffer objects
  26 +
  27 + var fs = require("fs");
  28 + var zip = require("node-native-zip");
  29 +
  30 + var archive = new zip();
  31 +
  32 + archive.add("hello.txt", new Buffer("Hello world", "utf8"));
  33 +
  34 + var buffer = archive.toBuffer();
  35 + fs.writeFile("./test1.zip", buffer, function () {
  36 + console.log("Finished");
  37 + });
  38 +
  39 +### Adding files from the file system
  40 +
  41 + var fs = require("fs");
  42 + var zip = require("node-native-zip");
  43 +
  44 + var archive = new zip();
  45 +
  46 + archive.addFiles([
  47 + { name: "moehah.txt", path: "./test/moehah.txt" },
  48 + { name: "images/suz.jpg", path: "./test/images.jpg" }
  49 + ], function () {
  50 + var buff = archive.toBuffer();
  51 +
  52 + fs.writeFile("./test2.zip", buff, function () {
  53 + console.log("Finished");
  54 + });
  55 + }, function (err) {
  56 + console.log(err);
  57 + });
  58 +
  59 +## API Reference
  60 +
  61 +There are three API methods:
  62 +
  63 +* `add(name, data)`, the 'name' is the name within the .zip file. To create a folder structure, add '/'
  64 +* `addFiles(files, onComplete, onError)`, where files is an array containing objects in the form ` { name: "name/in/zip.file", path: "file-system.path" } `
  65 +* `toBuffer()`, creates a new buffer and writes the zip file to it
  66 +
  67 +## Compression?
  68 +
  69 +The library currently doesn't do any compression. It stores the files via STORE. Main reason is that the
  70 +compression call is synchronous at the moment, so the thread will block during compression, something to
  71 +avoid.
  72 +However, it is possible to add compression methods by implementing the following interface.
  73 +
  74 + module.exports = (function () {
  75 + return {
  76 + indicator : [ 0x00, 0x00 ],
  77 + compress : function (content) {
  78 + // content is a Buffer object.
  79 + // you have to return a new Buffer too.
  80 + }
  81 + };
  82 + }());
  83 +
  84 +The `indicator` is an array consisting of two bytes indicating the compression technology.
  85 +For example: `[ 0x00, 0x00]` is STORE, `[ 0x08, 0x00]` is DEFLATE.
  86 +
  87 +The `compress` function is a method that transforms an incoming `Buffer` into a new one.
  88 +
  89 +The easiest to implement is probably deflate, because there is a [sample](https://github.com/Stuk/jszip/blob/master/jszip-deflate.js)
  90 +in JSZip. You will only need to change the inner workings from string-based to Buffer-based.
1  index.js
... ... @@ -0,0 +1 @@
  1 +module.exports = require("./janzip/janzip");
235 janzip/janzip.js
... ... @@ -0,0 +1,235 @@
  1 +/**
  2 + * node.js zipping module
  3 + *
  4 + * (c) Jan Jongboom, 2011
  5 + */
  6 +var RollingBuffer = require("./rollingbuffer");
  7 +var fs = require("fs");
  8 +
  9 +var Zip = function () {
  10 + var _self = this;
  11 +
  12 + // simple wrapper to keep some stuff private
  13 + var exp = (function () {
  14 +
  15 + var files = [];
  16 +
  17 + /**
  18 + * Get the date / time parts of the file header
  19 + * @param {date} A date object
  20 + */
  21 + function getDateTimeHeaders (date) {
  22 + var dosTime, dosDate;
  23 +
  24 + dosTime = date.getHours();
  25 + dosTime = dosTime << 6;
  26 + dosTime = dosTime | date.getMinutes();
  27 + dosTime = dosTime << 5;
  28 + dosTime = dosTime | date.getSeconds() / 2;
  29 +
  30 + dosDate = date.getFullYear() - 1980;
  31 + dosDate = dosDate << 4;
  32 + dosDate = dosDate | (date.getMonth() + 1);
  33 + dosDate = dosDate << 5;
  34 + dosDate = dosDate | date.getDate();
  35 +
  36 + return {
  37 + date: dosDate,
  38 + time: dosTime
  39 + };
  40 + }
  41 +
  42 + /**
  43 + * Simple sum function
  44 + */
  45 + function getTotalBufLength (arr) {
  46 + var sum = 0;
  47 +
  48 + arr.forEach(function (f) { sum += f.length; });
  49 +
  50 + return sum;
  51 + }
  52 +
  53 + /**
  54 + * Generate a file header (as a buffer)
  55 + */
  56 + function getFileHeader (file, compressIndicator, compressedData) {
  57 + var dt = getDateTimeHeaders(new Date());
  58 +
  59 + var header = new RollingBuffer(26);
  60 +
  61 + // version + bit flag
  62 + writeBytes(header, [ 0x0A, 0x00, 0x00, 0x00 ]);
  63 + // compression method @todo multiple methods, this is STORE
  64 + writeBytes(header, compressIndicator);
  65 + // file time & date
  66 + header.writeInt16(dt.time);
  67 + header.writeInt16(dt.date);
  68 + // crc32
  69 + header.writeInt32(crc32(compressedData));
  70 + // compressed size
  71 + header.writeInt32(compressedData.length);
  72 + // uncompressed size
  73 + header.writeInt32(file.data.length);
  74 + // file name length
  75 + header.writeInt16(file.name.length);
  76 + // fill
  77 + writeBytes(header, [ 0x00, 0x00 ]);
  78 +
  79 + return header;
  80 + }
  81 +
  82 + /**
  83 + * Add a file to the current file
  84 + * @param {name} The name of the file (can include folder structure)
  85 + * @param {data} A buffer containing the data
  86 + */
  87 + function add (filename, data) {
  88 + files.push({ name: filename, data: data });
  89 + }
  90 +
  91 + /**
  92 + * Add multiple files to the current archive.
  93 + * Pass in an array containing { name: "", path: "" }
  94 + */
  95 + function addFiles (filenames, onCompleted, onError) {
  96 + var counter = 0;
  97 + var callback = function () {
  98 + if (++counter === filenames.length) {
  99 + clearTimeout(expirationTimer);
  100 + onCompleted();
  101 + }
  102 + };
  103 +
  104 + filenames.forEach(function (f) {
  105 + fs.readFile(f.path, function (err, data) {
  106 + if (err) {
  107 + onError(err);
  108 + }
  109 + else {
  110 + add (f.name, data);
  111 + }
  112 + callback();
  113 + });
  114 + });
  115 +
  116 + // there is a 1 minute expiration timer running
  117 + var expirationTimer = setTimeout(function () {
  118 + onError("Timeout reached when adding files");
  119 + }, 60 * 1000);
  120 + }
  121 +
  122 + /**
  123 + * Returns the ZIP archive as a buffer object
  124 + */
  125 + function toBuffer (zipMethod) {
  126 + zipMethod = zipMethod || store;
  127 +
  128 + var fileBuffers = [], dirBuffers = [];
  129 + var fileOffset = 0;
  130 +
  131 + files.map(function(file) {
  132 + var data = zipMethod.compress(file.data);
  133 + var fileHeader = getFileHeader(file, zipMethod.indicator, data);
  134 +
  135 + // write files
  136 + var fileBuffer = new RollingBuffer(4 + fileHeader.length + file.name.length + data.length);
  137 + writeBytes(fileBuffer, [0x50, 0x4b, 0x03, 0x04]); // 4
  138 + fileBuffer.appendBuffer(fileHeader); // hmm...
  139 + fileBuffer.write(file.name, "ascii");
  140 + fileBuffer.appendBuffer(data);
  141 +
  142 + // now create dir
  143 + var dirBuffer = new RollingBuffer(4 + 2 + fileHeader.length + 6 + 4 + 4 + file.name.length);
  144 + writeBytes(dirBuffer, [0x50, 0x4b, 0x01, 0x02]);
  145 + writeBytes(dirBuffer, [0x14, 0x00]);
  146 + dirBuffer.appendBuffer(fileHeader);
  147 + // comment length, disk start, file attributes
  148 + writeBytes(dirBuffer, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
  149 + // external file attributes, @todo
  150 + writeBytes(dirBuffer, [0x00, 0x00, 0x00, 0x00]);
  151 + // relative offset of local header
  152 + dirBuffer.writeInt32(fileOffset);
  153 + // file name
  154 + dirBuffer.write(file.name, "ascii");
  155 +
  156 + // update offset
  157 + fileOffset += fileBuffer.length;
  158 +
  159 + fileBuffers.push(fileBuffer);
  160 + dirBuffers.push(dirBuffer);
  161 + });
  162 +
  163 + var totalDirLength = getTotalBufLength(dirBuffers);
  164 + var totalFileLength = getTotalBufLength(fileBuffers);
  165 +
  166 + var dirEnd = new RollingBuffer(8 + 2 + 2 + 4 + 4 + 2);
  167 + writeBytes(dirEnd, [0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00]);
  168 + // total number of entries
  169 + dirEnd.writeInt16(fileBuffers.length);
  170 + dirEnd.writeInt16(fileBuffers.length);
  171 + // directory lengths
  172 + dirEnd.writeInt32(totalDirLength);
  173 + // file lengths
  174 + dirEnd.writeInt32(totalFileLength);
  175 + // and the end of file
  176 + writeBytes(dirEnd, [0x00, 0x00]);
  177 +
  178 + var buffer = new RollingBuffer(totalFileLength + totalDirLength + dirEnd.length);
  179 + fileBuffers.forEach(function (b) { buffer.appendBuffer(b); });
  180 + dirBuffers.forEach(function (b) { buffer.appendBuffer(b); });
  181 + buffer.appendBuffer(dirEnd);
  182 +
  183 + return buffer.getInternalBuffer();
  184 + }
  185 +
  186 + return {
  187 + add: add,
  188 + addFiles: addFiles,
  189 + toBuffer: toBuffer
  190 + };
  191 +
  192 + }());
  193 +
  194 + // expand this with the functions that we just created
  195 + Object.keys(exp).forEach(function (k) { _self[k] = exp[k]; });
  196 +};
  197 +
  198 +module.exports = Zip;
  199 +
  200 +// ====== Default zipping method ======
  201 +var store = (function () {
  202 + return {
  203 + indicator : [ 0x00, 0x00 ],
  204 + compress : function (content) {
  205 + return content;
  206 + }
  207 + };
  208 +}());
  209 +
  210 +// ====== Helper functions ======
  211 +function crc32 (buf) {
  212 + if (!buf || !buf.length) {
  213 + return [ 0x00, 0x00, 0x00, 0x00 ];
  214 + }
  215 +
  216 + var table = [ 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D ];
  217 +
  218 + var crc = 0 ^ -1;
  219 +
  220 + for (var ix = 0; ix < buf.length; ix++) {
  221 + var offset = (crc ^ buf[ix]) & 0xFF;
  222 + crc = (crc >>> 8) ^ table[offset];
  223 + }
  224 +
  225 + return crc ^ -1;
  226 +}
  227 +
  228 +/**
  229 + * Write a serie of Int8's to a buffer
  230 + */
  231 +function writeBytes (target, data) {
  232 + for (var ix = 0; ix < data.length; ix++) {
  233 + target.writeInt8(data[ix]);
  234 + }
  235 +}
53 janzip/rollingbuffer.js
... ... @@ -0,0 +1,53 @@
  1 +function RollingBuffer (arg0, arg1, arg2) {
  2 + var offset = 0x00;
  3 +
  4 + this.buf = new Buffer(arg0, arg1, arg2); // make this nice :)
  5 + this.length = this.buf.length;
  6 +
  7 + this.write = function (str, encoding) {
  8 + var written = this.buf.write(str, offset, encoding);
  9 + offset += written;
  10 + };
  11 +
  12 + this.appendBuffer = function (newBuffer) {
  13 + if (newBuffer instanceof Buffer) {
  14 + newBuffer.copy(this.buf, offset);
  15 + offset += newBuffer.length;
  16 + }
  17 + else if (newBuffer instanceof RollingBuffer) {
  18 + newBuffer.buf.copy(this.buf, offset);
  19 + offset += newBuffer.buf.length;
  20 + }
  21 + };
  22 +
  23 + this.toString = function (encoding) {
  24 + return this.buf.toString(encoding);
  25 + };
  26 +
  27 + this.writeInt8 = function (data) {
  28 + this.buf[offset] = data;
  29 + offset += 1;
  30 + };
  31 +
  32 + this.writeInt16 = function (data) {
  33 + this.buf[offset] = data & 0xff;
  34 + this.buf[offset + 1] = (data & 0xff00) >> 8;
  35 +
  36 + offset += 2;
  37 + };
  38 +
  39 + this.writeInt32 = function (data) {
  40 + this.buf[offset] = data & 0xff;
  41 + this.buf[offset + 1] = (data & 0xff00) >> 0x08;
  42 + this.buf[offset + 2] = (data & 0xff0000) >> 0x10;
  43 + this.buf[offset + 3] = (data & 0xff000000) >> 0x18;
  44 +
  45 + offset += 4;
  46 + };
  47 +
  48 + this.getInternalBuffer = function () {
  49 + return this.buf;
  50 + };
  51 +}
  52 +
  53 +module.exports = RollingBuffer;
8 package.json
... ... @@ -0,0 +1,8 @@
  1 +{
  2 + "name": "node-native-zip",
  3 + "description": "A ZIP library without external dependencies",
  4 + "author": "Jan Jongboom <janjongboom@gmail.com>",
  5 + "dependencies": { },
  6 + "devDependencies": { },
  7 + "engine": "node >= 0.4.9"
  8 +}
36 server.js
... ... @@ -1,27 +1,33 @@
1 1 var fs = require("fs");
  2 +var zip = require("./index");
2 3
3   -(function () {
4   - var zip = require("./jszip");
5   -
  4 +(function () {
6 5 var archive = new zip();
7   - archive.add("smile.txt", "Smile!");
8   - var buffer = archive.generate(false);
9 6
10   - console.log(typeof buffer, buffer);
  7 + archive.add("hello.txt", new Buffer("Hello world", "utf8"));
11 8
12   - fs.writeFile("./test_original.zip", new Buffer(buffer, 'base64'), function () {
13   - console.log("we're done");
  9 + var buffer = archive.toBuffer();
  10 + fs.writeFile("./test1.zip", buffer, function () {
  11 + console.log("Finished");
14 12 });
15   -}());
  13 +})
16 14
17 15 (function () {
18   - var zip = require("./janzip");
  16 + var zip = require("./index");
19 17
20 18 var archive = new zip();
21   - archive.add("smile.txt", new Buffer("Smile!", "utf8"));
22   - var buff = archive.toBuffer();
23   -
24   - fs.writeFile("./test_new.zip", buff, function () {
25   - console.log("im too");
  19 + archive.addFiles([
  20 + { name: "moehah.txt", path: "./test/moehah.txt" },
  21 + { name: "images/sam.jpg", path: "./test/images.jpg" }
  22 + ], function () {
  23 + var buff = archive.toBuffer();
  24 +
  25 + fs.writeFile("./test_new.zip", buff, function () {
  26 + console.log("im finished");
  27 + });
  28 + }, function (err) {
  29 + console.log(err);
26 30 });
  31 + //archive.add("smile.txt", new Buffer("Smile!", "utf8"));
  32 +
27 33 }())

0 comments on commit fac11e4

Please sign in to comment.
Something went wrong with that request. Please try again.