Exemplo n.º 1
0
/**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();
    }
}
Exemplo n.º 3
0
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;
}
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;
}