bool CZipper::read_as_utf8 (const QString &archname, const QString &fname) { QuaZip zip (archname, settings->value ("zip_charset_in", "UTF-8").toString().trimmed()); if (! zip.open (QuaZip::mdUnzip)) return false; zip.setCurrentFile (fname); if (! zip.hasCurrentFile()) return false; QuaZipFileInfo info; if (! zip.getCurrentFileInfo (&info)) return false; QuaZipFile file (&zip); if (! file.open (QIODevice::ReadOnly)) return false; QByteArray ba = file.readAll(); string_data = QString::fromUtf8 (ba.data()); file.close(); zip.close(); return true; }
static QString CreateTempFile(QuaZip& zip, QString zipFilename) { if (!zip.setCurrentFile(zipFilename)) { ccLog::Warning(QString("[Photoscan] Failed to locate '%1' in the Photoscan archive").arg(zipFilename)); return QString(); } //decompress the file QuaZipFile zipFile(&zip); if (!zipFile.open(QFile::ReadOnly)) { ccLog::Warning(QString("[Photoscan] Failed to extract '%1' from Photoscan archive").arg(zipFilename)); return QString(); } QDir tempDir = QDir::temp(); QString tempFilename = tempDir.absoluteFilePath(zipFilename); QFile tempFile(tempFilename); if (!tempFile.open(QFile::WriteOnly)) { ccLog::Warning(QString("[Photoscan] Failed to create temp file '%1'").arg(tempFilename)); return QString(); } tempFile.write(zipFile.readAll()); tempFile.close(); return tempFilename; }
bool FindSubtitlesWindow::extractFile(QuaZip &zip, const QString &filename, const QString &output_name) { qDebug("FindSubtitlesWindow::extractFile: '%s', save as '%s'", filename.toUtf8().constData(), output_name.toUtf8().constData()); if (QFile::exists(output_name)) { if (QMessageBox::question(this, tr("Overwrite?"), tr("The file %1 already exits, overwrite?").arg(output_name), QMessageBox::Yes, QMessageBox::No) == QMessageBox::No) { return false; } } if (!zip.setCurrentFile(filename)) { qDebug("FindSubtitlesWindow::extractFile: can't select file %s", filename.toUtf8().constData()); return false; } // Saving char c; QuaZipFile file(&zip); QFile out(output_name); if (!file.open(QIODevice::ReadOnly)) { qWarning("FindSubtitlesWindow::extractFile: can't open file for reading: %d", file.getZipError()); return false; } if (out.open(QIODevice::WriteOnly)) { // Slow like hell (on GNU/Linux at least), but it is not my fault. // Not ZIP/UNZIP package's fault either. // The slowest thing here is out.putChar(c). while (file.getChar(&c)) out.putChar(c); out.close(); file.close(); } else { qWarning("FindSubtitlesWindow::extractFile: can't open %s for writing", output_name.toUtf8().constData()); return false; } return true; }
BookParser_f MakeBookParser (const QString& filename) { if (filename.endsWith (".fb2")) return FB2::Parse; else if (filename.endsWith (".epub")) return EPUB::Parse; else if (filename.endsWith (".mobi") || filename.endsWith (".prc")) return Mobi::Parse; else if (filename.endsWith (".zip")) { QuaZip *zip = new QuaZip (filename); std::shared_ptr<void> scopeGuard (nullptr, [zip] (void*) { zip->close (); delete zip; }); if (!zip->open (QuaZip::mdUnzip)) { qWarning () << Q_FUNC_INFO << "unable to open file " << filename << "as zip archive"; return BookParser_f (); } for (const auto& info : zip->getFileInfoList ()) { if (info.name.endsWith (".fb2")) { QString fileNameInArch = info.name; zip->setCurrentFile (fileNameInArch); QuaZipFile file (zip); if (!file.open (QIODevice::ReadOnly)) continue; const quint32 size = info.uncompressedSize; auto ba = file.readAll (); return [ba, size, filename, fileNameInArch] (const QString&) -> Book { auto book = FB2::ParseFB2Content (ba); book.AddedData_ = QDateTime::currentDateTime (); book.Rate_ = NoStar; book.OriginalPath_ = filename; book.Size_ = size; book.Content_ = ba; if (book.TitleInfo_.Title_.isEmpty()) book.TitleInfo_.Title_ = QObject::tr ("Unknown"); Author author; author.Name_ = QObject::tr ("Unknown"); if (book.TitleInfo_.Authors_.isEmpty ()) book.TitleInfo_.Authors_ << author; if (book.TitleInfo_.Authors_.at (0).Name_.isEmpty ()) book.TitleInfo_.Authors_ [0] = author; return book; }; } } } return BookParser_f (); }
Book Parse (const QString& filename) { QuaZip *zip = new QuaZip (filename); std::shared_ptr<void> scopeGuard (nullptr, [zip] (void*) { zip->close (); delete zip; }); Book book; book.OriginalPath_ = filename; if (!zip->open (QuaZip::mdUnzip)) { qWarning () << Q_FUNC_INFO << "unable to open file " << filename << "as zip archive"; return book; } for (const auto& opfPath : GetOPFPaths (zip)) { zip->setCurrentFile (opfPath); QuaZipFile opfFile (zip); if (!opfFile.open (QIODevice::ReadOnly)) { qWarning () << Q_FUNC_INFO << "unable to open opf file"; return book; } QDomDocument document; QString errorMsg; int errorLine = -1, errorColumn = -1; if (!document.setContent (opfFile.readAll (), &errorMsg, &errorLine, &errorColumn)) { qWarning () << Q_FUNC_INFO << errorMsg << "in line:" << errorLine << "column:" << errorColumn; return book; } FillEPubBookInfo (document, book, zip); book.AddedData_ = QDateTime::currentDateTime (); book.Rate_ = NoStar; book.Size_ = QFileInfo (filename).size (); book.IsValid_ = true; if (book.TitleInfo_.Title_.isEmpty()) book.TitleInfo_.Title_ = QObject::tr ("Unknown"); Author author; author.Name_ = QObject::tr ("Unknown"); if (book.TitleInfo_.Authors_.isEmpty ()) book.TitleInfo_.Authors_ << author; if (book.TitleInfo_.Authors_.at (0).Name_.isEmpty ()) book.TitleInfo_.Authors_ [0] = author; } return book; }