Skip to content

Commit

Permalink
Added back synchronous file read.
Browse files Browse the repository at this point in the history
  • Loading branch information
toots committed Sep 16, 2011
1 parent e1e25c4 commit c5d008e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
68 changes: 67 additions & 1 deletion src/node_zipfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void ZipFile::Initialize(Handle<Object> target) {
// functions
NODE_SET_PROTOTYPE_METHOD(constructor, "open", Open);
NODE_SET_PROTOTYPE_METHOD(constructor, "read", Read);
//NODE_SET_PROTOTYPE_METHOD(constructor, "readFileSync", readFileSync);
NODE_SET_PROTOTYPE_METHOD(constructor, "readFileSync", readFileSync);
NODE_SET_PROTOTYPE_METHOD(constructor, "close", Close);
NODE_SET_PROTOTYPE_METHOD(constructor, "addFile", Add_File);
NODE_SET_PROTOTYPE_METHOD(constructor, "replaceFile", Replace_File);
Expand Down Expand Up @@ -185,6 +185,72 @@ Handle<Value> ZipFile::Close(const Arguments& args)
return Undefined();
}

Handle<Value> ZipFile::readFileSync(const Arguments& args)
{
HandleScope scope;

if (args.Length() != 1 || !args[0]->IsString())
return ThrowException(Exception::TypeError(
String::New("first argument must be a file name inside the zip")));

std::string name = TOSTR(args[0]);

// TODO - enforce valid index
ZipFile* zf = ObjectWrap::Unwrap<ZipFile>(args.This());

if (zf->Busy())
return ThrowException(Exception::Error(String::New("Zipfile already in use..")));

struct zip_file *zf_ptr;

int idx = -1;

std::vector<std::string>::iterator it = std::find(zf->names.begin(), zf->names.end(), name);
if (it!=zf->names.end()) {
idx = distance(zf->names.begin(), it);
}

if (idx == -1) {
std::stringstream s;
s << "No file found by the name of: '" << name << "\n";
return ThrowException(Exception::Error(String::New(s.str().c_str())));
}

if ((zf_ptr=zip_fopen_index(zf->archive, idx, 0)) == NULL) {
zip_fclose(zf_ptr);
std::stringstream s;
s << "cannot open file #" << idx << " in " << name << ": archive error: " << zip_strerror(zf->archive) << "\n";
return ThrowException(Exception::Error(String::New(s.str().c_str())));
}

struct zip_stat st;
zip_stat_index(zf->archive, idx, 0, &st);

std::vector<unsigned char> data;
data.clear();
data.resize( st.size );

int result = 0;
result = (int)zip_fread( zf_ptr, reinterpret_cast<void*> (&data[0]), data.size() );

if (result < 0) {
zip_fclose(zf_ptr);
std::stringstream s;
s << "error reading file #" << idx << " in " << name << ": archive error: " << zip_file_strerror(zf_ptr) << "\n";
return ThrowException(Exception::Error(String::New(s.str().c_str())));
}

#if NODE_VERSION_AT_LEAST(0,3,0)
node::Buffer *retbuf = Buffer::New((char *)&data[0],data.size());
#else
node::Buffer *retbuf = Buffer::New(data.size());
std::memcpy(retbuf->data(), (char *)&data[0], data.size());
#endif

zip_fclose(zf_ptr);
return scope.Close(retbuf->handle_);
}

/* zipfile.read(buffer, pos, len, cb) -> cb(bytesRead, error) */
Handle<Value> ZipFile::Read(const Arguments& args)
{
Expand Down
12 changes: 6 additions & 6 deletions src/node_zipfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ class ZipFile: public node::ObjectWrap {
const AccessorInfo& info);

// Sync
//static Handle<Value> readFileSync(const Arguments& args);

// Async
static Handle<Value> Open(const Arguments& args);
static Handle<Value> readFileSync(const Arguments& args);
static Handle<Value> Close(const Arguments& args);
static Handle<Value> Add_File(const Arguments& args);
static Handle<Value> Replace_File(const Arguments& args);

// Async
static Handle<Value> Read(const Arguments& args);
static int EIO_Read(eio_req *req);
static int EIO_AfterRead(eio_req *req);
static Handle<Value> Close(const Arguments& args);
static Handle<Value> Add_File(const Arguments& args);
static Handle<Value> Replace_File(const Arguments& args);
static Handle<Value> Save(const Arguments& args);
static void *Save_Thread(void *data);
static void Save_Callback(EV_P_ ev_async *watcher, int revents);
Expand Down
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var zf = new zipfile.ZipFile('./data/folder.zip');
assert.equal(zf.count, 3); // one folder, two files
assert.deepEqual(zf.names, ['folder/', 'folder/one.txt', 'folder/two.txt']);

var data = zf.readFileSync('folder/one.txt')
assert.throws(function() { zf.readFileSync('foo')});

var zf = new zipfile.ZipFile('./data/world_merc.zip');
Expand Down

0 comments on commit c5d008e

Please sign in to comment.