ZipEntry *ZipEntry::Create(const wxString &path) { wxFile file(path); int error; auto zipFile = zip_fdopen(file.fd(), ZIP_RDONLY, &error); file.Detach(); if (zipFile == nullptr) { throw error; } auto mutex = new wxMutex(); ZipEntry *root = new ZipEntry(zipFile, mutex, ""); std::vector<wxString> innerPaths; int num_entries = zip_get_num_entries(zipFile, ZIP_FL_UNCHANGED); for (int i = 0; i < num_entries; i++) { auto path = wxString(zip_get_name(zipFile, i, ZIP_FL_UNCHANGED)); innerPaths.push_back(path); } std::sort(innerPaths.begin(), innerPaths.end()); std::map<wxString, ZipEntry *> entryMap; entryMap[""] = root; for (auto &path : innerPaths) { AddChildrenFromPath(zipFile, mutex, entryMap, path); } return root; }
ZipReader::ZipReader(QIODevice* device) : m_archive(0) { QFile* file = qobject_cast<QFile*>(device); if (file) { m_archive = zip_fdopen(file->handle(), 0, 0); if (m_archive) { readFileInfo(); } } }
struct zip *subsurface_zip_open_readonly(const char *path, int flags, int *errorp) { #if defined(LIBZIP_VERSION_MAJOR) /* libzip 0.10 has zip_fdopen, let's use it since zip_open doesn't have a * wchar_t version */ int fd = subsurface_open(path, O_RDONLY | O_BINARY, 0); struct zip *ret = zip_fdopen(fd, flags, errorp); if (!ret) close(fd); return ret; #else return zip_open(path, flags, errorp); #endif }
bool KaraokePlayable_ZIP::init() { // We use zip_fdopen instead of zip_open because latter does not support Unicode file names. // We also can't use QFile + handle since zip_fdopen takes over the handle, and it is impossible // to have QFile not closing it on delete (the functions which allows disabling it doesn't take the file name) #if defined (Q_OS_WIN) QVector<wchar_t> wcharbuf( m_baseFile.length() + 1); m_baseFile.toWCharArray( wcharbuf.data() ); int fd = _wopen( wcharbuf.constData(), _O_BINARY | _O_RDONLY ); #else int fd = open( QFile::encodeName( m_baseFile ).constData(), O_RDONLY ); #endif if ( fd < 0 ) { m_errorMsg = "Could not open file"; return false; } // Open the ZIP archive: http://www.nih.at/libzip/zip_fdopen.html int errcode; m_zip = zip_fdopen( fd, 0, &errcode ); if ( !m_zip ) { #if defined (Q_OS_WIN) _close( fd ); #else close( fd ); #endif m_errorMsg = QString( "Error %1 opening ZIP archive") .arg( errcode ); return false; } return true; }