mitk::NavigationTool::Pointer mitk::NavigationToolReader::DoRead(std::string filename) { //decompress all files into a temporary directory std::ifstream file(filename.c_str(), std::ios::binary); if (!file.good()) { m_ErrorMessage = "Cannot open '" + filename + "' for reading"; return nullptr; } std::string tempDirectory = m_ToolfilePath + GetFileWithoutPath(filename); Poco::Zip::Decompress unzipper(file, Poco::Path(tempDirectory)); unzipper.decompressAllFiles(); //use SceneSerialization to load the DataStorage mitk::SceneIO::Pointer mySceneIO = mitk::SceneIO::New(); mitk::DataStorage::Pointer loadedStorage = mySceneIO->LoadScene(tempDirectory + Poco::Path::separator() + GetFileWithoutPath(filename) + ".storage"); if (loadedStorage->GetAll()->size() == 0 || loadedStorage.IsNull()) { m_ErrorMessage = "Invalid file: cannot parse tool data."; return nullptr; } //convert the DataStorage back to a NavigationTool-Object mitk::DataNode::Pointer myNode = loadedStorage->GetAll()->ElementAt(0); mitk::NavigationTool::Pointer returnValue = ConvertDataNodeToNavigationTool(myNode, tempDirectory); //delete the data-storage file which is not needed any more. The toolfile must be left in the temporary directory becauses it is linked in the datatreenode of the tool std::remove((std::string(tempDirectory + Poco::Path::separator() + GetFileWithoutPath(filename) + ".storage")).c_str()); return returnValue; }
bool CUpdateManager::internal_do_update(CUpdateInfo& ui) { CString filename = IuCommonFunctions::IUTempFolder + ui.packageName() +_T(".zip"); std::string filenamea= IuCoreUtils::WstringToUtf8((LPCTSTR)filename); IU_ConfigureProxy(nm); nm.setOutputFile( filenamea); m_statusCallback->updateStatus(nCurrentIndex, TR("Downloading file ")+ ui.downloadUrl()); nm.doGet(IuCoreUtils::WstringToUtf8((LPCTSTR) ui.downloadUrl())); if(nm.responseCode() != 200) { WriteLog(logError,_T("Update Engine"),TR("Error while updating component ") + ui.packageName() + CString(_T("\r\nHTTP response code: "))+IuCoreUtils::Utf8ToWstring(IuCoreUtils::int64_tToString(nm.responseCode())).c_str()+_T("\r\n")+ IuCoreUtils::Utf8ToWstring(nm.errorString()).c_str(),CString("URL=")+ui.downloadUrl()); return 0; } CString hash = ui.getHash(); hash.MakeLower(); if( hash != IuCoreUtils::Utf8ToWstring(IuCoreUtils::CryptoUtils::CalcMD5HashFromFile(IuCoreUtils::WstringToUtf8((LPCTSTR)filename))).c_str() || ui.getHash().IsEmpty()) { updateStatus(0, CString(TR("MD5 check of the update package failed "))+IuCoreUtils::ExtractFileName(IuCoreUtils::WstringToUtf8((LPCTSTR)filename)).c_str()); return 0; } CUnzipper unzipper(filename); CString unzipFolder = IuCommonFunctions::IUTempFolder + ui.packageName(); if(!unzipper.UnzipTo(unzipFolder)) { updateStatus(0, TR("Unable to unpack archive ")+ filename); return 0; } CUpdatePackage updatePackage; updatePackage.setUpdateStatusCallback(this); if(!updatePackage.LoadUpdateFromFile(unzipFolder + _T("\\")+_T("package.xml"))) { MessageBox(0,TR("Could not read ") + ui.packageName(),0,0); return false; } if(!updatePackage.doUpdate()) return false; CString finishText; finishText.Format(TR("Update finished. Updated %d of %d files "), updatePackage.updatedFileCount(), updatePackage.totalFileCount()); m_statusCallback->updateStatus(nCurrentIndex, finishText ); ui.SaveToFile(ui.fileName()); m_nSuccessPackageUpdates++; return true; }
void mitk::NavigationToolStorageDeserializer::decompressFiles(std::string filename,std::string path) { std::ifstream file( filename.c_str(), std::ios::binary ); if (!file.good()) { m_ErrorMessage = "Cannot open '" + filename + "' for reading"; mitkThrowException(mitk::IGTException)<<"Cannot open"+filename+" for reading"; } try { Poco::Zip::Decompress unzipper( file, Poco::Path( path ) ); unzipper.decompressAllFiles(); file.close(); } catch(Poco::IllegalStateException e) //temporary solution: replace this by defined exception handling later! { m_ErrorMessage = "Error: wrong file format! \n (please only load tool storage files)"; mitkThrowException(mitk::IGTException) << m_ErrorMessage; } }
LONGLONG CUnzipper::GetUnzipSize(LPCTSTR zipFilePath) { CUnzipper unzipper(zipFilePath); return unzipper.GetUnzipSize(); }
NodeTransitPtr SceneFileHandlerBase::read( std::istream &is, const Char8 *fileNameOrExtension, GraphOpSeq *graphOpSeq , Resolver resolver ) { SceneFileType *type = getFileType(fileNameOrExtension); NodeUnrecPtr scene = NULL; if(!fileNameOrExtension) { SWARNING << "cannot read NULL extension" << std::endl; return NodeTransitPtr(scene); } if(type != NULL) { SINFO << "try to read stream as " << type->getName() << std::endl; // check for fileio read callback if(_readFP != NULL) { initReadProgress(is); scene = _readFP(type, is, fileNameOrExtension); terminateReadProgress(); } else { if(isGZip(is)) { SINFO << "Detected gzip compressed stream." << std::endl; #ifdef OSG_WITH_ZLIB initReadProgress(is); zip_istream unzipper(is); scene = type->read(unzipper, fileNameOrExtension, resolver ? resolver : _oGlobalResolver); if(scene != NULL) { if(unzipper.check_crc() == true) { SINFO << "Compressed stream has correct checksum." << std::endl; } else { SWARNING << "Compressed stream has wrong checksum." << std::endl; } } terminateReadProgress(); #else SFATAL << "Compressed streams are not supported! Configure " << "with --enable-png --with-png=DIR options." << std::endl; #endif } else { initReadProgress(is); scene = type->read(is, fileNameOrExtension, resolver ? resolver : _oGlobalResolver); terminateReadProgress(); } } if(scene != NULL) { if(graphOpSeq != NULL) graphOpSeq->traverse(scene); SINFO << "read ok:" << std::endl; } else { SWARNING << "could not read " << std::endl; } } else { SWARNING << "could not read unknown file format" << std::endl; } commitChanges(); return NodeTransitPtr(scene); }
NodePtr SceneFileHandler::read(std::istream &is, const Char8* fileNameOrExtension, GraphOpSeq *graphOpSeq) { SceneFileType *type = getFileType(fileNameOrExtension); NodePtr scene = NullFC; if(!fileNameOrExtension) { SWARNING << "cannot read NULL extension" << std::endl; return NullFC; } if (type) { SINFO << "try to read stream as " << type->getName() << std::endl; // check for fileio read callback if(_readFP != NULL) { initReadProgress(is); scene = _readFP(type, is, fileNameOrExtension); terminateReadProgress(); } else { if(isGZip(is)) { SINFO << "Detected gzip compressed stream." << std::endl; #ifdef OSG_ZSTREAM_SUPPORTED initReadProgress(is); zip_istream unzipper(is); scene = type->read(unzipper, fileNameOrExtension); if(scene != NullFC) { if(unzipper.check_crc()) SINFO << "Compressed stream has correct checksum." << std::endl; else SFATAL << "Compressed stream has wrong checksum." << std::endl; } terminateReadProgress(); #else SFATAL << "Compressed streams are not supported! Configure with --enable-png --with-png=DIR options." << std::endl; #endif } else { initReadProgress(is); scene = type->read(is, fileNameOrExtension); terminateReadProgress(); } } if(scene != NullFC) { if(graphOpSeq != NULL) graphOpSeq->run(scene); SINFO << "read ok:" << std::endl; } else { SWARNING << "could not read " << std::endl; } } else { SWARNING << "could not read unknown file format" << std::endl; } return scene; }
mitk::DataStorage::Pointer mitk::SceneIO::LoadScene( const std::string& filename, DataStorage* pStorage, bool clearStorageFirst ) { // prepare data storage DataStorage::Pointer storage = pStorage; if ( storage.IsNull() ) { storage = StandaloneDataStorage::New().GetPointer(); } if ( clearStorageFirst ) { try { storage->Remove( storage->GetAll() ); } catch(...) { MITK_ERROR << "DataStorage cannot be cleared properly."; } } // test input filename if ( filename.empty() ) { MITK_ERROR << "No filename given. Not possible to load scene."; return NULL; } // test if filename can be read std::ifstream file( filename.c_str(), std::ios::binary ); if (!file.good()) { MITK_ERROR << "Cannot open '" << filename << "' for reading"; return NULL; } // get new temporary directory m_WorkingDirectory = CreateEmptyTempDirectory(); if (m_WorkingDirectory.empty()) { MITK_ERROR << "Could not create temporary directory. Cannot open scene files."; return NULL; } // unzip all filenames contents to temp dir m_UnzipErrors = 0; Poco::Zip::Decompress unzipper( file, Poco::Path( m_WorkingDirectory ) ); unzipper.EError += Poco::Delegate<SceneIO, std::pair<const Poco::Zip::ZipLocalFileHeader, const std::string> >(this, &SceneIO::OnUnzipError); unzipper.EOk += Poco::Delegate<SceneIO, std::pair<const Poco::Zip::ZipLocalFileHeader, const Poco::Path> >(this, &SceneIO::OnUnzipOk); unzipper.decompressAllFiles(); unzipper.EError -= Poco::Delegate<SceneIO, std::pair<const Poco::Zip::ZipLocalFileHeader, const std::string> >(this, &SceneIO::OnUnzipError); unzipper.EOk -= Poco::Delegate<SceneIO, std::pair<const Poco::Zip::ZipLocalFileHeader, const Poco::Path> >(this, &SceneIO::OnUnzipOk); if ( m_UnzipErrors ) { MITK_ERROR << "There were " << m_UnzipErrors << " errors unzipping '" << filename << "'. Will attempt to read whatever could be unzipped."; } // test if index.xml exists // parse index.xml with TinyXML TiXmlDocument document( m_WorkingDirectory + Poco::Path::separator() + "index.xml" ); if (!document.LoadFile()) { MITK_ERROR << "Could not open/read/parse " << m_WorkingDirectory << "/index.xml\nTinyXML reports: " << document.ErrorDesc() << std::endl; return NULL; } SceneReader::Pointer reader = SceneReader::New(); if ( !reader->LoadScene( document, m_WorkingDirectory, storage ) ) { MITK_ERROR << "There were errors while loading scene file " << filename << ". Your data may be corrupted"; } // delete temp directory try { Poco::File deleteDir( m_WorkingDirectory ); deleteDir.remove(true); // recursive } catch(...) { MITK_ERROR << "Could not delete temporary directory " << m_WorkingDirectory; } // return new data storage, even if empty or uncomplete (return as much as possible but notify calling method) return storage; }