2012-06-01 15:23:19 +00:00
|
|
|
#include "VFSFileZip.h"
|
|
|
|
#include "VFSDirZip.h"
|
|
|
|
#include "VFSTools.h"
|
|
|
|
|
|
|
|
#include "VFSInternal.h"
|
|
|
|
|
|
|
|
#include "miniz.h"
|
|
|
|
|
|
|
|
VFS_NAMESPACE_START
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
ZipDir::ZipDir(ZipArchiveRef *handle, const char *fullpath, bool canLoad)
|
|
|
|
: Dir(fullpath, NULL)
|
|
|
|
, _archiveHandle(handle)
|
|
|
|
, _canLoad(canLoad)
|
|
|
|
, _couldLoad(canLoad)
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
ZipDir::~ZipDir()
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
|
|
|
|
void ZipDir::close()
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
_archiveHandle->close();
|
|
|
|
_canLoad = _couldLoad; // allow loading again after re-opening (in case archive was replaced)
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
DirBase *ZipDir::createNew(const char *fullpath) const
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
const ZipArchiveRef *czref = _archiveHandle;
|
|
|
|
ZipArchiveRef *zref = const_cast<ZipArchiveRef*>(czref);
|
|
|
|
return new ZipDir(zref, fullpath, false);
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
#define MZ ((mz_zip_archive*)_archiveHandle->mz)
|
|
|
|
|
|
|
|
void ZipDir::load()
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
if(!_canLoad)
|
|
|
|
return;
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
_archiveHandle->openRead();
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
const unsigned int files = mz_zip_reader_get_num_files(MZ);
|
2014-04-07 00:25:58 +00:00
|
|
|
const size_t len = fullnameLen() + 1; // +1 for trailing '/' when used as path name in addRecursive()
|
2012-06-01 15:23:19 +00:00
|
|
|
|
|
|
|
mz_zip_archive_file_stat fs;
|
|
|
|
for (unsigned int i = 0; i < files; ++i)
|
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
if(mz_zip_reader_is_file_encrypted(MZ, i))
|
2012-06-01 15:23:19 +00:00
|
|
|
continue;
|
2014-04-06 17:19:33 +00:00
|
|
|
if(!mz_zip_reader_file_stat(MZ, i, &fs))
|
2012-06-01 15:23:19 +00:00
|
|
|
continue;
|
2014-04-06 17:19:33 +00:00
|
|
|
if(mz_zip_reader_is_file_a_directory(MZ, i))
|
|
|
|
{
|
2014-04-15 13:04:33 +00:00
|
|
|
_createAndInsertSubtree(fs.m_filename);
|
2012-06-01 15:23:19 +00:00
|
|
|
continue;
|
2014-04-06 17:19:33 +00:00
|
|
|
}
|
2012-06-01 15:23:19 +00:00
|
|
|
if(getFile(fs.m_filename))
|
|
|
|
continue;
|
|
|
|
|
2014-04-23 03:26:03 +00:00
|
|
|
ZipFile *vf = new ZipFile(fs.m_filename, _archiveHandle, fs.m_file_index);
|
2014-04-06 17:19:33 +00:00
|
|
|
addRecursive(vf, len);
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
_canLoad = false;
|
2012-06-01 15:23:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
VFS_NAMESPACE_END
|