void ExportEPUB::CreateEncryptionXML(const QString &fullfolderpath) { QTemporaryFile file; if (!file.open()) { boost_throw(CannotOpenFile() << errinfo_file_fullpath(file.fileName().toStdString()) << errinfo_file_errorstring(file.errorString().toStdString()) ); } EncryptionXmlWriter enc(*m_Book, file); enc.WriteXML(); // Write to disk immediately file.flush(); QFile::copy(file.fileName(), fullfolderpath + "/" + ENCRYPTION_XML_FILE_NAME); }
// Accepts a full path to an HTML file. // Reads the file, detects the encoding // and returns the text converted to Unicode. QString HTMLEncodingResolver::ReadHTMLFile( const QString &fullfilepath ) { QFile file( fullfilepath ); // Check if we can open the file if ( !file.open( QFile::ReadOnly ) ) { boost_throw( CannotOpenFile() << errinfo_file_fullpath( file.fileName().toStdString() ) << errinfo_file_errorstring( file.errorString().toStdString() ) ); } QByteArray data = file.readAll(); return Utility::ConvertLineEndings( GetCodecForHTML( data ).toUnicode( data ) ); }
void ExportEPUB::SaveFolderAsEpubToLocation(const QString &fullfolderpath, const QString &fullfilepath) { QString tempFile = fullfolderpath + "-tmp.epub"; QDateTime timeNow = QDateTime::currentDateTime(); zip_fileinfo fileInfo; #ifdef Q_OS_WIN32 zlib_filefunc64_def ffunc; fill_win32_filefunc64W(&ffunc); zipFile zfile = zipOpen2_64(Utility::QStringToStdWString(QDir::toNativeSeparators(tempFile)).c_str(), APPEND_STATUS_CREATE, NULL, &ffunc); #else zipFile zfile = zipOpen64(QDir::toNativeSeparators(tempFile).toUtf8().constData(), APPEND_STATUS_CREATE); #endif if (zfile == NULL) { boost_throw(CannotOpenFile() << errinfo_file_fullpath(tempFile.toStdString())); } memset(&fileInfo, 0, sizeof(fileInfo)); fileInfo.tmz_date.tm_sec = timeNow.time().second(); fileInfo.tmz_date.tm_min = timeNow.time().minute(); fileInfo.tmz_date.tm_hour = timeNow.time().hour(); fileInfo.tmz_date.tm_mday = timeNow.date().day(); fileInfo.tmz_date.tm_mon = timeNow.date().month() - 1; fileInfo.tmz_date.tm_year = timeNow.date().year(); // Write the mimetype. This must be uncompressed and the first entry in the archive. if (zipOpenNewFileInZip64(zfile, "mimetype", &fileInfo, NULL, 0, NULL, 0, NULL, Z_NO_COMPRESSION, 0, 0) != Z_OK) { zipClose(zfile, NULL); QFile::remove(tempFile); boost_throw(CannotStoreFile() << errinfo_file_fullpath("mimetype")); } if (zipWriteInFileInZip(zfile, EPUB_MIME_DATA, (unsigned int)strlen(EPUB_MIME_DATA)) != Z_OK) { zipCloseFileInZip(zfile); zipClose(zfile, NULL); QFile::remove(tempFile); boost_throw(CannotStoreFile() << errinfo_file_fullpath("mimetype")); } zipCloseFileInZip(zfile); // Write all the files in our directory path to the archive. QDirIterator it(fullfolderpath, QDir::Files | QDir::NoDotAndDotDot | QDir::Readable | QDir::Hidden, QDirIterator::Subdirectories); while (it.hasNext()) { it.next(); QString relpath = it.filePath().remove(fullfolderpath); while (relpath.startsWith("/")) { relpath = relpath.remove(0, 1); } // Add the file entry to the archive. // We should check the uncompressed file size. If it's over >= 0xffffffff the last parameter (zip64) should be 1. if (zipOpenNewFileInZip4_64(zfile, relpath.toUtf8().constData(), &fileInfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, 8, 0, 15, 8, Z_DEFAULT_STRATEGY, NULL, 0, 0x0b00, 1<<11, 0) != Z_OK) { zipClose(zfile, NULL); QFile::remove(tempFile); boost_throw(CannotStoreFile() << errinfo_file_fullpath(relpath.toStdString())); } // Open the file on disk. We will read this and write what we read into // the archive. QFile dfile(it.filePath()); if (!dfile.open(QIODevice::ReadOnly)) { zipCloseFileInZip(zfile); zipClose(zfile, NULL); QFile::remove(tempFile); boost_throw(CannotOpenFile() << errinfo_file_fullpath(it.fileName().toStdString())); } // Write the data from the file on disk into the archive. char buff[BUFF_SIZE] = {0}; qint64 read = 0; while ((read = dfile.read(buff, BUFF_SIZE)) > 0) { if (zipWriteInFileInZip(zfile, buff, read) != Z_OK) { dfile.close(); zipCloseFileInZip(zfile); zipClose(zfile, NULL); QFile::remove(tempFile); boost_throw(CannotStoreFile() << errinfo_file_fullpath(relpath.toStdString())); } } dfile.close(); // There was an error reading the file on disk. if (read < 0) { zipCloseFileInZip(zfile); zipClose(zfile, NULL); QFile::remove(tempFile); boost_throw(CannotStoreFile() << errinfo_file_fullpath(relpath.toStdString())); } if (zipCloseFileInZip(zfile) != Z_OK) { zipClose(zfile, NULL); QFile::remove(tempFile); boost_throw(CannotStoreFile() << errinfo_file_fullpath(relpath.toStdString())); } } zipClose(zfile, NULL); // Overwrite the contents of the real file with the contents from the temp // file we saved the data do. We do this instead of simply copying the file // because a file copy will lose extended attributes such as labels on OS X. QFile temp_epub(tempFile); if (!temp_epub.open(QFile::ReadOnly)) { boost_throw(CannotOpenFile() << errinfo_file_fullpath(tempFile.toStdString())); } QFile real_epub(fullfilepath); if (!real_epub.open(QFile::WriteOnly | QFile::Truncate)) { temp_epub.close(); boost_throw(CannotWriteFile() << errinfo_file_fullpath(fullfilepath.toStdString())); } // Copy the contents from the temp file to the real file. char buff[BUFF_SIZE] = {0}; qint64 read = 0; qint64 written = 0; while ((read = temp_epub.read(buff, BUFF_SIZE)) > 0) { written = real_epub.write(buff, read); if (written != read) { temp_epub.close(); real_epub.close(); QFile::remove(tempFile); boost_throw(CannotCopyFile() << errinfo_file_fullpath(fullfilepath.toStdString())); } } if (read == -1) { temp_epub.close(); real_epub.close(); QFile::remove(tempFile); boost_throw(CannotCopyFile() << errinfo_file_fullpath(fullfilepath.toStdString())); } temp_epub.close(); real_epub.close(); QFile::remove(tempFile); }