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-23 03:26:03 +00:00
|
|
|
ZipFile::ZipFile(const char *name, ZipArchiveRef *zref, unsigned int fileIdx)
|
2014-04-06 17:19:33 +00:00
|
|
|
: File(joinPath(zref->fullname(), name).c_str())
|
|
|
|
, _buf(NULL)
|
|
|
|
, _pos(0)
|
|
|
|
, _archiveHandle(zref)
|
2014-04-23 03:26:03 +00:00
|
|
|
, _bufSize(0)
|
2014-04-06 17:19:33 +00:00
|
|
|
, _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";
|
2017-01-12 22:52:59 +00:00
|
|
|
else if(strchr(mode, 'w') || strchr(mode, 'a'))
|
|
|
|
return false; // writing not yet supported
|
2014-04-06 17:19:33 +00:00
|
|
|
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-23 03:26:03 +00:00
|
|
|
return _pos >= _bufSize;
|
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
|
|
|
|
2017-01-12 22:52:59 +00:00
|
|
|
delete [] _buf;
|
2014-04-06 17:19:33 +00:00
|
|
|
_buf = NULL;
|
2014-04-23 03:26:03 +00:00
|
|
|
_bufSize = 0;
|
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:
|
2014-04-23 03:26:03 +00:00
|
|
|
if(pos >= _bufSize)
|
2014-04-06 17:19:33 +00:00
|
|
|
return false;
|
2014-04-23 03:26:03 +00:00
|
|
|
_pos = _bufSize - pos;
|
2014-04-06 17:19:33 +00:00
|
|
|
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-23 03:26:03 +00:00
|
|
|
#define MZ ((mz_zip_archive*)_archiveHandle->mz)
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
vfspos ZipFile::size()
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-23 03:26:03 +00:00
|
|
|
if(_buf && _bufSize)
|
|
|
|
return _bufSize;
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2014-04-23 03:26:03 +00:00
|
|
|
if(!_archiveHandle->openRead())
|
|
|
|
return npos;
|
|
|
|
|
|
|
|
// FIXME: this is not 100% safe. The file index may change if the zip file is changed externally while closed
|
|
|
|
mz_zip_archive_file_stat zstat;
|
|
|
|
if(!mz_zip_reader_file_stat(MZ, _fileIdx, &zstat))
|
|
|
|
return npos;
|
|
|
|
|
|
|
|
return (vfspos)zstat.m_uncomp_size;
|
|
|
|
}
|
2014-04-06 17:19:33 +00:00
|
|
|
|
|
|
|
bool ZipFile::unpack()
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-23 03:26:03 +00:00
|
|
|
close(); // delete the buffer
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2014-04-23 03:26:03 +00:00
|
|
|
const vfspos sz = size(); // will reopen the file
|
2017-01-12 22:52:59 +00:00
|
|
|
if(sz == npos)
|
2014-04-23 03:26:03 +00:00
|
|
|
return false;
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2017-01-12 22:52:59 +00:00
|
|
|
_buf = new char[size_t(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
|
|
|
|
2017-01-12 22:52:59 +00:00
|
|
|
if(!mz_zip_reader_extract_to_mem(MZ, _fileIdx, _buf, (size_t)sz, 0))
|
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-23 03:26:03 +00:00
|
|
|
_bufSize = sz;
|
|
|
|
|
|
|
|
// 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.
|
2014-04-06 17:19:33 +00:00
|
|
|
_buf[sz] = 0;
|
|
|
|
if(_mode.find("b") == std::string::npos) // text mode?
|
|
|
|
{
|
2014-04-23 03:26:03 +00:00
|
|
|
_bufSize = (vfspos)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
|