/**OK * Comprime il file fileName nel file fileCompressed. * Se la funzione fallisce restituisce false e cancella il file che si e tentato * di creare. * * La funzione fallisce se: * * non si riesce ad aprire l'oggetto zip; * * la compressione del file fallisce; * * non si riesce a chiudere l'oggetto zip; */ bool JlCompress::compressFile(QString fileCompressed, QString file) { // Creo lo zip QuaZip* zip = new QuaZip(QFileInfo(fileCompressed).absoluteFilePath()); QDir().mkpath(QFileInfo(fileCompressed).absolutePath()); if(!zip->open(QuaZip::mdCreate)) { delete zip; QFile::remove(fileCompressed); return false; } // Aggiungo il file if (!compressFile(zip,file,QFileInfo(file).fileName())) { delete zip; QFile::remove(fileCompressed); return false; } // Chiudo il file zip zip->close(); if(zip->getZipError()!=0) { delete zip; QFile::remove(fileCompressed); return false; } delete zip; return true; }
void DomainContentBackupManager::setup() { for (auto& rule : _backupRules) { removeOldBackupVersions(rule); } auto backups = getAllBackups(); for (auto& backup : backups) { QFile backupFile { backup.absolutePath }; if (!backupFile.open(QIODevice::ReadOnly)) { qCritical() << "Could not open file:" << backup.absolutePath; qCritical() << " ERROR:" << backupFile.errorString(); continue; } QuaZip zip { &backupFile }; if (!zip.open(QuaZip::mdUnzip)) { qCritical() << "Could not open backup archive:" << backup.absolutePath; qCritical() << " ERROR:" << zip.getZipError(); continue; } for (auto& handler : _backupHandlers) { handler->loadBackup(backup.id, zip); } zip.close(); } for (auto& handler : _backupHandlers) { handler->loadingComplete(); } }
bool CZipper::zip_directory (const QString &archpath, const QString &dir2pack) { QString archname = qstring_get_last_after (dir2pack, "/"); QString zipname (archpath); //zip name has a full path ending with .zip zipname.append ("/").append (archname).append (".zip"); QuaZip zip (zipname, settings->value ("zip_charset_out", "UTF-8").toString().trimmed()); if (! zip.open (QuaZip::mdCreate)) return false; QDir dir (dir2pack); QFileInfoList files = dir.entryInfoList(); QFile inFile; QuaZipFile outFile(&zip); foreach (QFileInfo file, files) { if (! file.isFile()) continue; inFile.setFileName (file.absoluteFilePath()); if (! inFile.open (QIODevice::ReadOnly)) return false; QString outfname (archname); outfname.append ("/").append (file.fileName()); if (! outFile.open (QIODevice::WriteOnly, QuaZipNewInfo (outfname, inFile.fileName()))) return false; QByteArray ba = inFile.readAll(); outFile.write (ba); outFile.close(); if (outFile.getZipError() != UNZ_OK) return false; inFile.close(); emit new_iteration (file); } zip.close(); if (zip.getZipError() != 0) return false; 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 CZipper::pack_prepared() { if (archive_fullpath.isEmpty()) return false; QString zipname (archive_fullpath); QuaZip zip (zipname, settings->value ("zip_charset_out", "UTF-8").toString().trimmed()); if (! zip.open (QuaZip::mdCreate)) return false; QFile inFile; QuaZipFile outFile (&zip); foreach (QString fi, files_list) { QFileInfo file (fi); if (! file.isFile()) continue; inFile.setFileName (file.absoluteFilePath()); if (! inFile.open (QIODevice::ReadOnly)) return false; QString outfname (archive_name); outfname.append ("/").append (file.fileName()); if (! outFile.open (QIODevice::WriteOnly, QuaZipNewInfo (outfname, inFile.fileName()))) return false; QByteArray ba = inFile.readAll(); outFile.write (ba); outFile.close(); if (outFile.getZipError() != UNZ_OK) return false; inFile.close(); emit new_iteration (file); }
/**OK * Comprime i file specificati in files nel file fileCompressed. * Se la funzione fallisce restituisce false e cancella il file che si e tentato * di creare. * * La funzione fallisce se: * * non si riesce ad aprire l'oggetto zip; * * la compressione di un file fallisce; * * non si riesce a chiudere l'oggetto zip; */ bool JlCompress::compressFiles(QString fileCompressed, QStringList files) { // Creo lo zip QuaZip* zip = new QuaZip(QFileInfo(fileCompressed).absoluteFilePath()); QDir().mkpath(QFileInfo(fileCompressed).absolutePath()); if(!zip->open(QuaZip::mdCreate)) { delete zip; QFile::remove(fileCompressed); return false; } // Comprimo i file QFileInfo info; Q_FOREACH (QString file, files) { info.setFile(file); if (!info.exists() || !compressFile(zip,file,info.fileName())) { delete zip; QFile::remove(fileCompressed); return false; } }
bool UBExportDocumentSetAdaptor::addDocumentToZip(const QModelIndex &pIndex, UBDocumentTreeModel *model, QuaZip &zip) { static int i = 0; i++; QModelIndex parentIndex = pIndex; if (!parentIndex.isValid()) { return false; } UBDocumentProxy *pDocumentProxy = model->proxyForIndex(parentIndex); if (pDocumentProxy) { // Q_ASSERT(QFileInfo(pDocumentProxy->persistencePath()).exists()); // UBForeighnObjectsHandler cleaner; // cleaner.cure(pDocumentProxy->persistencePath()); UniboardSankoreTransition document; QString documentPath(pDocumentProxy->persistencePath()); document.checkDocumentDirectory(documentPath); QDir documentDir = QDir(pDocumentProxy->persistencePath()); QuaZipFile zipFile(&zip); UBFileSystemUtils::compressDirInZip(documentDir, QFileInfo(documentPath).fileName() + "/", &zipFile, false); if(zip.getZipError() != 0) { qWarning("Export failed. Cause: zip.close(): %d", zip.getZipError()); } } for (int i = 0; i < model->rowCount(parentIndex); ++i) { QModelIndex curIndex = model->index(i, 0, parentIndex); if (!addDocumentToZip(curIndex, model, zip)) { return false; } } return true; }
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; }
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; }