Permalink
Browse files

finish support for node v0.6

  • Loading branch information...
1 parent 9a1a5fd commit 44db7a6fcd16d1c8b08830d482f8db779cd1db2e Dane Springmeyer committed Nov 8, 2011
Showing with 48 additions and 52 deletions.
  1. +13 −12 README.md
  2. +4 −4 bin/unzip.js
  3. +1 −1 src/_zipfile.cc
  4. +19 −27 src/node_zipfile.cpp
  5. +11 −8 src/node_zipfile.hpp
View
@@ -1,26 +1,27 @@
-
-# Node-Zipfile
+# node-zipfile
- Bindings to [libzip](http://nih.at/libzip/libzip.html) for handling zipfiles in [node](http://nodejs.org).
+Bindings to [libzip](http://nih.at/libzip/libzip.html) for handling zipfile archives in [node](http://nodejs.org).
-# Example
+## Example
- > var zipfile = require('zipfile')
- > var zf = new zipfile.ZipFile('./data/world_merc.zip')
- > zf
+ var zipfile = require('zipfile');
+ var zf = new zipfile.ZipFile('./test/data/world_merc.zip');
+ zf
{ names: [ 'world_merc.dbf', 'world_merc.prj', 'world_merc.shp', 'world_merc.shx' ],
count: 4 }
- > var buffer = zf.readFileSync('world_merc.prj')
- > buffer.toString()
+ var buffer = zf.readFileSync('world_merc.prj');
+ buffer.toString()
'PROJCS["Google Maps Global Mercator",GEOGCS .... '
## Depends
- node >= 0.2.4 (development headers)
-
- libzip (optionally)
+node-zipfile 0.3.x depends on node v0.6 and above
+
+node-zipfile 0.1.x-0.2.x works with node v0.2-v0.4
+
+libzip (optionally, otherwise will build against bundled version)
## Installation
View
@@ -3,11 +3,10 @@
var zip = require('zipfile');
var fs = require('fs');
var path = require('path');
-var constants = require('constants');
var usage = 'usage: unzip.js <zipfile>';
-var file = process.ARGV[2];
+var file = process.argv[2];
if (!file) {
console.log(usage);
process.exit(1);
@@ -18,11 +17,12 @@ var zf = new zip.ZipFile(file);
zf.names.forEach(function(name) {
var uncompressed = path.join('.', name);
var dirname = path.dirname(uncompressed);
- fs.mkdir(dirname, '0755' , function(err) {
- if (err && err.errno != constants.EEXIST) throw err;
+ fs.mkdir(dirname, 0755 , function(err) {
+ if (err && !err.code.match(/^EEXIST/)) throw err;
if (path.extname(name)) {
var buffer = zf.readFileSync(name);
fd = fs.openSync(uncompressed, 'w');
+ console.log('unzipping: ' + name);
fs.writeSync(fd, buffer, 0, buffer.length, null);
fs.closeSync(fd);
}
View
@@ -19,7 +19,7 @@ extern "C" {
ZipFile::Initialize(target);
// node-zipfile version
- target->Set(String::NewSymbol("version"), String::New("0.2.4"));
+ target->Set(String::NewSymbol("version"), String::New("0.3.0"));
// versions of deps
Local<Object> versions = Object::New();
View
@@ -1,22 +1,21 @@
#include "node_zipfile.hpp"
-// stl
+#include <node_buffer.h>
+
+// std
#include <sstream>
#include <vector>
#include <cstring>
#include <algorithm>
-#include <node_buffer.h>
#define TOSTR(obj) (*String::Utf8Value((obj)->ToString()))
Persistent<FunctionTemplate> ZipFile::constructor;
void ZipFile::Initialize(Handle<Object> target) {
-
HandleScope scope;
-
constructor = Persistent<FunctionTemplate>::New(FunctionTemplate::New(ZipFile::New));
constructor->InstanceTemplate()->SetInternalFieldCount(1);
constructor->SetClassName(String::NewSymbol("ZipFile"));
@@ -29,7 +28,7 @@ void ZipFile::Initialize(Handle<Object> target) {
constructor->InstanceTemplate()->SetAccessor(String::NewSymbol("count"), get_prop);
constructor->InstanceTemplate()->SetAccessor(String::NewSymbol("names"), get_prop);
- target->Set(String::NewSymbol("ZipFile"),constructor->GetFunction());
+ target->Set(String::NewSymbol("ZipFile"), constructor->GetFunction());
}
ZipFile::ZipFile(std::string const& file_name)
@@ -57,7 +56,7 @@ Handle<Value> ZipFile::New(const Arguments& args)
struct zip *za;
int err;
char errstr[1024];
- if ((za=zip_open(input_file.c_str(), 0, &err)) == NULL) {
+ if ((za = zip_open(input_file.c_str(), 0, &err)) == NULL) {
zip_error_to_str(errstr, sizeof(errstr), err, errno);
std::stringstream s;
s << "cannot open file: " << input_file << " error: " << errstr << "\n";
@@ -70,7 +69,7 @@ Handle<Value> ZipFile::New(const Arguments& args)
int num = zip_get_num_files(za);
zf->names_.reserve(num);
int i = 0;
- for (i=0; i<num; i++) {
+ for (i = 0; i < num; i++) {
struct zip_stat st;
zip_stat_index(za, i, 0, &st);
zf->names_.push_back(st.name);
@@ -79,7 +78,6 @@ Handle<Value> ZipFile::New(const Arguments& args)
zf->archive_ = za;
zf->Wrap(args.This());
return args.This();
-
}
Handle<Value> ZipFile::get_prop(Local<String> property,
@@ -98,10 +96,10 @@ Handle<Value> ZipFile::get_prop(Local<String> property,
{
a->Set(i,String::New(zf->names_[i].c_str()));
}
-
+
return scope.Close(a);
}
- return Undefined();
+ return Undefined();
}
Handle<Value> ZipFile::readFileSync(const Arguments& args)
@@ -146,8 +144,8 @@ Handle<Value> ZipFile::readFileSync(const Arguments& args)
data.clear();
data.resize( st.size );
- int result = 0;
- result = (int)zip_fread( zf_ptr, reinterpret_cast<void*> (&data[0]), data.size() );
+ int result = 0;
+ result = static_cast<int>(zip_fread( zf_ptr, reinterpret_cast<void*> (&data[0]), data.size() ));
if (result < 0) {
zip_fclose(zf_ptr);
@@ -156,7 +154,7 @@ Handle<Value> ZipFile::readFileSync(const Arguments& args)
return ThrowException(Exception::Error(String::New(s.str().c_str())));
}
- node::Buffer *retbuf = Buffer::New((char *)&data[0],data.size());
+ node::Buffer *retbuf = Buffer::New(reinterpret_cast<char *>(&data[0]),data.size());
zip_fclose(zf_ptr);
return scope.Close(retbuf->handle_);
}
@@ -203,7 +201,7 @@ Handle<Value> ZipFile::readFile(const Arguments& args)
struct zip *za;
int err;
char errstr[1024];
- if ((za=zip_open(zf->file_name_.c_str() , 0, &err)) == NULL) {
+ if ((za = zip_open(zf->file_name_.c_str() , 0, &err)) == NULL) {
zip_error_to_str(errstr, sizeof(errstr), err, errno);
std::stringstream s;
s << "cannot open file: " << zf->file_name_ << " error: " << errstr << "\n";
@@ -217,17 +215,18 @@ Handle<Value> ZipFile::readFile(const Arguments& args)
closure->error = false;
closure->name = name;
closure->cb = Persistent<Function>::New(Handle<Function>::Cast(args[args.Length()-1]));
- uv_queue_work(uv_default_loop(), &closure->request, EIO_ReadFile, EIO_AfterReadFile);
+ uv_queue_work(uv_default_loop(), &closure->request, Work_ReadFile, Work_AfterReadFile);
+ uv_ref(uv_default_loop());
zf->Ref();
return Undefined();
}
-void ZipFile::EIO_ReadFile(uv_work_t* req)
+void ZipFile::Work_ReadFile(uv_work_t* req)
{
closure_t *closure = static_cast<closure_t *>(req->data);
- struct zip_file *zf_ptr=NULL;
+ struct zip_file *zf_ptr = NULL;
int idx = -1;
@@ -276,36 +275,29 @@ void ZipFile::EIO_ReadFile(uv_work_t* req)
zip_fclose(zf_ptr);
}
-void ZipFile::EIO_AfterReadFile(uv_work_t* req)
+void ZipFile::Work_AfterReadFile(uv_work_t* req)
{
HandleScope scope;
closure_t *closure = static_cast<closure_t *>(req->data);
- ev_unref(EV_DEFAULT_UC);
TryCatch try_catch;
if (closure->error) {
Local<Value> argv[1] = { Exception::Error(String::New(closure->error_name.c_str())) };
closure->cb->Call(Context::GetCurrent()->Global(), 1, argv);
} else {
- #if NODE_VERSION_AT_LEAST(0,3,0)
- node::Buffer *retbuf = Buffer::New((char *)&closure->data[0],closure->data.size());
- #else
- node::Buffer *retbuf = Buffer::New(closure->data.size());
- std::memcpy(retbuf->data(), (char *)&closure->data[0], closure->data.size());
- #endif
+ node::Buffer *retbuf = Buffer::New((char *)&closure->data[0],closure->data.size());
Local<Value> argv[2] = { Local<Value>::New(Null()), Local<Value>::New(retbuf->handle_) };
closure->cb->Call(Context::GetCurrent()->Global(), 2, argv);
}
if (try_catch.HasCaught()) {
FatalException(try_catch);
- //try_catch.ReThrow();
}
closure->zf->Unref();
+ uv_unref(uv_default_loop());
closure->cb.Dispose();
delete closure;
}
-
View
@@ -1,25 +1,28 @@
#ifndef __NODE_ZIPFILE_H__
#define __NODE_ZIPFILE_H__
+// libzip
+extern "C"
+{
+#include <zlib.h>
+#include <errno.h>
+#include <zip.h>
+}
+
#include <v8.h>
#include <node.h>
#include <node_object_wrap.h>
-#include <node_version.h>
// stl
#include <string>
#include <vector>
-// libzip
-#include <zlib.h>
-#include <errno.h>
-#include <zip.h>
-//#include <zipint.h>
using namespace v8;
using namespace node;
class ZipFile: public node::ObjectWrap {
+
public:
static Persistent<FunctionTemplate> constructor;
static void Initialize(Handle<Object> target);
@@ -33,8 +36,8 @@ class ZipFile: public node::ObjectWrap {
// Async
static Handle<Value> readFile(const Arguments& args);
- static void EIO_ReadFile(uv_work_t* req);
- static void EIO_AfterReadFile(uv_work_t* req);
+ static void Work_ReadFile(uv_work_t* req);
+ static void Work_AfterReadFile(uv_work_t* req);
ZipFile(std::string const& file_name);

0 comments on commit 44db7a6

Please sign in to comment.