2012-06-01 15:23:19 +00:00
|
|
|
#include "VFSFileZip.h"
|
|
|
|
#include "VFSInternal.h"
|
|
|
|
#include "VFSTools.h"
|
|
|
|
#include "VFSDir.h"
|
2014-04-06 17:19:33 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "miniz.h"
|
2012-06-01 15:23:19 +00:00
|
|
|
|
|
|
|
VFS_NAMESPACE_START
|
|
|
|
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
ZipFile::ZipFile(const char *name, ZipArchiveRef *zref, vfspos uncompSize, unsigned int fileIdx)
|
|
|
|
: File(joinPath(zref->fullname(), name).c_str())
|
|
|
|
, _buf(NULL)
|
|
|
|
, _pos(0)
|
|
|
|
, _archiveHandle(zref)
|
|
|
|
, _uncompSize(uncompSize)
|
|
|
|
, _fileIdx(fileIdx)
|
|
|
|
, _mode("rb") // binary mode by default
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
ZipFile::~ZipFile()
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
close();
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
bool ZipFile::open(const char *mode /* = NULL */)
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
|
|
|
_pos = 0;
|
2014-04-06 17:19:33 +00:00
|
|
|
if(!mode)
|
|
|
|
mode = "rb";
|
|
|
|
if(_mode != mode)
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
delete [] _buf;
|
|
|
|
_buf = NULL;
|
2012-06-01 15:23:19 +00:00
|
|
|
_mode = mode;
|
|
|
|
}
|
|
|
|
return true; // does not have to be opened
|
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
bool ZipFile::isopen() const
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
|
|
|
return true; // is always open
|
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
bool ZipFile::iseof() const
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
return _pos >= _uncompSize;
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
void ZipFile::close()
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
//flush(); // TODO: write to zip file on close
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
delete []_buf;
|
|
|
|
_buf = NULL;
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
bool ZipFile::seek(vfspos pos, int whence)
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
const vfspos end = 0xFFFFFFFF;
|
|
|
|
switch(whence)
|
|
|
|
{
|
|
|
|
case SEEK_SET:
|
|
|
|
if(pos >= end) // zip files have uint32 range only
|
|
|
|
return false;
|
|
|
|
_pos = pos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_CUR:
|
|
|
|
if(_pos + pos >= end)
|
|
|
|
return false;
|
|
|
|
_pos += pos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_END:
|
|
|
|
if(pos >= _uncompSize)
|
|
|
|
return false;
|
|
|
|
_pos = _uncompSize - pos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-01 15:23:19 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
bool ZipFile::flush()
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
|
|
|
// FIXME: use this to actually write to zip file?
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
vfspos ZipFile::getpos() const
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
|
|
|
return _pos;
|
|
|
|
}
|
|
|
|
|
2014-04-07 02:16:15 +00:00
|
|
|
size_t ZipFile::read(void *dst, size_t bytes)
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
if(!_buf && !unpack())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
char *startptr = _buf + _pos;
|
|
|
|
char *endptr = _buf + size();
|
2014-04-07 02:16:15 +00:00
|
|
|
bytes = std::min<size_t>(endptr - startptr, bytes); // limit in case reading over buffer size
|
|
|
|
memcpy(dst, startptr, bytes); // binary copy
|
2012-06-01 15:23:19 +00:00
|
|
|
_pos += bytes;
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
2014-04-07 02:16:15 +00:00
|
|
|
size_t ZipFile::write(const void *src, size_t bytes)
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
// TODO: implement actually writing to the underlying Zip file.
|
2014-04-15 13:04:33 +00:00
|
|
|
//printf("NYI: ZipFile::write()");
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
return 0;
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
vfspos ZipFile::size()
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
return (vfspos)_uncompSize;
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
#define MZ ((mz_zip_archive*)_archiveHandle->mz)
|
|
|
|
|
|
|
|
bool ZipFile::unpack()
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
delete [] _buf;
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
if(!_archiveHandle->openRead())
|
2014-04-15 13:04:33 +00:00
|
|
|
{
|
|
|
|
//assert(0 && "ZipFile unpack: Failed to openRead");
|
2014-04-06 17:19:33 +00:00
|
|
|
return false; // can happen if the underlying zip file was deleted
|
2014-04-15 13:04:33 +00:00
|
|
|
}
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
// In case of text data, make sure the buffer is always terminated with '\0'.
|
|
|
|
// Won't hurt for binary data, so just do it in all cases.
|
|
|
|
size_t sz = (size_t)size();
|
|
|
|
_buf = new char[sz + 1];
|
2012-06-01 15:23:19 +00:00
|
|
|
if(!_buf)
|
2014-04-06 17:19:33 +00:00
|
|
|
return false;
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
if(!mz_zip_reader_extract_to_mem(MZ, _fileIdx, _buf, sz, 0))
|
|
|
|
{
|
2014-04-15 13:04:33 +00:00
|
|
|
//assert(0 && "ZipFile unpack: Failed mz_zip_reader_extract_to_mem");
|
2014-04-06 17:19:33 +00:00
|
|
|
delete [] _buf;
|
|
|
|
_buf = NULL;
|
|
|
|
return false; // this should not happen
|
|
|
|
}
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
_buf[sz] = 0;
|
|
|
|
if(_mode.find("b") == std::string::npos) // text mode?
|
|
|
|
{
|
|
|
|
_uncompSize = strnNLcpy(_buf, _buf);
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
return true;
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VFS_NAMESPACE_END
|