Example #1
0
/**
 * @brief Adds the content of a folder to the archive.
 * @param resourcesFolder Resources' folder.
 * @param folderName Folder to add to the archive.
 * @param archivedFile The archive file opened with QuaZip.
 */
void ExportManager::addToArchive(QDir resourcesFolder, QString folderName, QuaZipFile& archivedFile) {
	QDir folder = resourcesFolder.absolutePath() + QDir::separator() + folderName;
	QDirIterator it(folder.absolutePath());
	while(it.hasNext()) {
		QString filePath = it.next();
		QString filename = filePath;
		filename.remove(0, folder.absolutePath().size()+1);
		if(filename=="." || filename=="..") {
			continue;
		}
		QFile file(filePath);
		if(!file.open(QIODevice::ReadOnly)) {
			throw ExportException("Cannot open file "+filePath+".");
		}

		// Creates the file in the archive:
		QString filePathInArchive = filePath;
		filePathInArchive.remove(0, resourcesFolder.absolutePath().size()+1); // Removes '[..]/Resources/'.
		if(!archivedFile.open(QIODevice::WriteOnly, QuaZipNewInfo(filePathInArchive))) {
			throw ExportException("Cannot create file "+filePathInArchive+" in archive.");
		}

		// Writes the file's content:
		char c;
		while(file.getChar(&c) && archivedFile.putChar(c));
		archivedFile.close();
		file.close();
	}
}
Example #2
0
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;
}
Example #3
0
    foreach(QFileInfo fileInfo, files) {

        if (!fileInfo.isFile()) {
            continue;
        }

        QString fileNameWithRelativePath (fileInfo.filePath().remove(0, dir.absolutePath().length() + 1));

        inFile.setFileName(fileInfo.filePath());
        //KeyProvider kp;
        if (!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileNameWithRelativePath, fileInfo.filePath())/*, kp.GetKey().toAscii()*/)) {
          return false;
        }

        if (!inFile.open(QIODevice::ReadOnly)) {
            return false;
        }

        while (inFile.getChar(&c) && outFile.putChar(c));

        if (outFile.getZipError() != UNZ_OK) {
            return false;
        }

        outFile.close();

        if (outFile.getZipError() != UNZ_OK) {
            return false;
        }

        inFile.close();
    }
void QuaZipFilePrivate::setZipError(int zipError) const
{
  QuaZipFilePrivate *fakeThis = const_cast<QuaZipFilePrivate*>(this); // non-const
  fakeThis->zipError=zipError;
  if(zipError==UNZ_OK)
    q->setErrorString(QString());
  else
    q->setErrorString(QString("ZIP/UNZIP API error %1").arg(zipError));
}
Example #5
0
void QuaZipFile::setZipError (int zipError) const
{
  QuaZipFile *fakeThis = (QuaZipFile*) this; // non-const
  fakeThis->zipError = zipError;

  if(zipError == UNZ_OK)
    fakeThis->setErrorString (QString());
  else
      fakeThis->setErrorString (tr ("ZIP/UNZIP API error %1").arg(zipError));
}
Example #6
0
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);
         }
Example #7
0
void QZip::zipDir(QuaZipFile &outFile,const QString &zipPath,const QString &zipTPath){
    QDir dir(zipPath);
    dir.setCurrent(zipPath);
    QFileInfoList files=dir.entryInfoList();

    QFile inFile;
    char c;
    foreach(QFileInfo file, files) {
        if(file.isDir()){
            if(!file.absoluteFilePath().endsWith(".")){
                QString zipDirecName = zipTPath==""?file.fileName():zipTPath+"\\"+file.fileName();
                zipDir(outFile,file.absoluteFilePath(),zipDirecName);
                dir.setCurrent(zipPath);
            }
            continue;//
        }
        inFile.setFileName(file.fileName());
        if(!inFile.open(QIODevice::ReadOnly)) {
            qWarning("testCreate(): inFile.open(): %s", inFile.errorString().toLocal8Bit().constData());
            return;
        }
        QString zipDirName = zipTPath==""?inFile.fileName():zipTPath+"\\"+inFile.fileName();
        if(!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(zipDirName, inFile.fileName()))) {
            qWarning("testCreate(): outFile.open(): %d", outFile.getZipError());
            return;
        }
        while(inFile.getChar(&c)&&outFile.putChar(c));
        if(outFile.getZipError()!=UNZ_OK) {
            qWarning("testCreate(): outFile.putChar(): %d", outFile.getZipError());
            return;
        }
        outFile.close();
        if(outFile.getZipError()!=UNZ_OK) {
            qWarning("testCreate(): outFile.close(): %d", outFile.getZipError());
            return;
        }
        inFile.close();
    }
}
Example #8
0
	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 ();
	}
Example #9
0
	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;
	}
Example #10
0
//! @brief extract currently opened archive
//! @brief dest path to extract archive to, can be filename when extracting a
//!             single file.
//! @brief file file to extract from archive, full archive if empty.
//! @return true on success, false otherwise
bool ZipUtil::extractArchive(QString& dest, QString file)
{
    qDebug() << "[ZipUtil] extractArchive" << dest << file;
    bool result = true;
    if(!m_zip) {
        return false;
    }
    QuaZipFile *currentFile = new QuaZipFile(m_zip);
    int entries = m_zip->getEntriesCount();
    int current = 0;
    // construct the filename when extracting a single file from an archive.
    // if the given destination is a full path use it as output name,
    // otherwise use it as path to place the file as named in the archive.
    QString singleoutfile;
    if(!file.isEmpty() && QFileInfo(dest).isDir()) {
        singleoutfile = dest + "/" + file;
    }
    else if(!file.isEmpty()){
        singleoutfile = dest;
    }
    for(bool more = m_zip->goToFirstFile(); more; more = m_zip->goToNextFile())
    {
        ++current;
        // if the entry is a path ignore it. Path existence is ensured separately.
        if(m_zip->getCurrentFileName().split("/").last() == "")
            continue;
        // some tools set the MS-DOS file attributes. Check those for D flag,
        // since in some cases a folder entry does not end with a /
        QuaZipFileInfo fi;
        currentFile->getFileInfo(&fi);
        if(fi.externalAttr & 0x10) // FAT entry bit 4 indicating directory
            continue;

        QString outfilename;
        if(!singleoutfile.isEmpty()
                && QFileInfo(m_zip->getCurrentFileName()).fileName() == file) {
            outfilename = singleoutfile;
        }
        else if(singleoutfile.isEmpty()) {
            outfilename = dest + "/" + m_zip->getCurrentFileName();
        }
        if(outfilename.isEmpty())
            continue;
        QFile outputFile(outfilename);
        // make sure the output path exists
        if(!QDir().mkpath(QFileInfo(outfilename).absolutePath())) {
            result = false;
            emit logItem(tr("Creating output path failed"), LOGERROR);
            qDebug() << "[ZipUtil] creating output path failed for:"
                     << outfilename;
            break;
        }
        if(!outputFile.open(QFile::WriteOnly)) {
            result = false;
            emit logItem(tr("Creating output file failed"), LOGERROR);
            qDebug() << "[ZipUtil] creating output file failed:"
                     << outfilename;
            break;
        }
        currentFile->open(QIODevice::ReadOnly);
        outputFile.write(currentFile->readAll());
        if(currentFile->getZipError() != UNZ_OK) {
            result = false;
            emit logItem(tr("Error during Zip operation"), LOGERROR);
            qDebug() << "[ZipUtil] QuaZip error:" << currentFile->getZipError()
                     << "on file" << currentFile->getFileName();
            break;
        }
        currentFile->close();
        outputFile.close();

        emit logProgress(current, entries);
    }
    delete currentFile;
    emit logProgress(1, 1);

    return result;
}