-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Project may be zipped/unzipped
- Loading branch information
1 parent
33247cc
commit 86389d1
Showing
7 changed files
with
300 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/*************************************************************************** | ||
qgsarchive.cpp | ||
---------------- | ||
begin : July 07, 2017 | ||
copyright : (C) 2017 by Paul Blottiere | ||
email : paul.blottiere@oslandia.com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsarchive.h" | ||
#include "qgsziputils.h" | ||
#include "qgsmessagelog.h" | ||
|
||
QgsArchive::QgsArchive() | ||
: mDir( new QTemporaryDir() ) | ||
{ | ||
} | ||
|
||
QgsArchive::~QgsArchive() | ||
{ | ||
} | ||
|
||
QString QgsArchive::dir() const | ||
{ | ||
return mDir->path(); | ||
} | ||
|
||
void QgsArchive::clear() | ||
{ | ||
mDir.reset( new QTemporaryDir() ); | ||
mFilename.clear(); | ||
mFiles.clear(); | ||
} | ||
|
||
bool QgsArchive::zip( const QString &filename ) | ||
{ | ||
// create a temporary path | ||
QTemporaryFile tmpFile; | ||
tmpFile.open(); | ||
tmpFile.close(); | ||
|
||
// zip content | ||
if ( ! QgsZipUtils::zip( tmpFile.fileName(), mFiles ) ) | ||
{ | ||
QString err = QObject::tr( "Unable to zip content" ); | ||
QgsMessageLog::logMessage( err, QStringLiteral( "QgsArchive" ) ); | ||
return false; | ||
} | ||
|
||
// remove existing zip file | ||
if ( QFile::exists( filename ) ) | ||
QFile::remove( filename ); | ||
|
||
// save zip archive | ||
if ( ! tmpFile.rename( filename ) ) | ||
{ | ||
QString err = QObject::tr( "Unable to save zip file '%1'" ).arg( filename ); | ||
QgsMessageLog::logMessage( err, QStringLiteral( "QgsArchive" ) ); | ||
return false; | ||
} | ||
|
||
// keep the zip filename | ||
tmpFile.setAutoRemove( false ); | ||
mFilename = filename; | ||
|
||
return true; | ||
} | ||
|
||
bool QgsArchive::unzip( const QString &filename ) | ||
{ | ||
clear(); | ||
|
||
QgsZipUtils::unzip( filename, mDir->path(), mFiles ); | ||
mFilename = filename; | ||
|
||
return ! projectFile().isEmpty(); | ||
} | ||
|
||
void QgsArchive::addFile( const QString &file ) | ||
{ | ||
mFiles.append( file ); | ||
} | ||
|
||
QString QgsArchive::filename() const | ||
{ | ||
return mFilename; | ||
} | ||
|
||
QString QgsArchive::projectFile() const | ||
{ | ||
Q_FOREACH ( const QString &file, mFiles ) | ||
{ | ||
QFileInfo fileInfo( file ); | ||
if ( "qgs" == fileInfo.suffix().toLower() ) | ||
return file; | ||
} | ||
|
||
return QString(); | ||
} | ||
|
||
QStringList QgsArchive::files() const | ||
{ | ||
return mFiles; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/*************************************************************************** | ||
qgsarchive.h | ||
---------------- | ||
begin : July 07, 2017 | ||
copyright : (C) 2017 by Paul Blottiere | ||
email : paul.blottiere@oslandia.com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSARCHIVE_H | ||
#define QGSARCHIVE_H | ||
|
||
#include "qgis_core.h" | ||
#include <QStringList> | ||
#include <QTemporaryFile> | ||
#include <QTemporaryDir> | ||
#include <memory> | ||
|
||
/** | ||
* \class QgsArchive | ||
* \ingroup core | ||
* \brief Class allowing to manage the zip/unzip actions on project | ||
* \since QGIS 3.0 | ||
*/ | ||
class CORE_EXPORT QgsArchive | ||
{ | ||
public: | ||
|
||
/** | ||
* Constructor for QgsArchive | ||
*/ | ||
QgsArchive(); | ||
~QgsArchive(); | ||
|
||
bool zip( const QString &zipFilename ); | ||
|
||
bool unzip( const QString &zipFilename ); | ||
|
||
void clear(); | ||
|
||
void addFile( const QString &filename ); | ||
|
||
QString filename() const; | ||
|
||
QString projectFile() const; | ||
|
||
QStringList files() const; | ||
|
||
QString dir() const; | ||
|
||
private: | ||
// used when unzip is performed | ||
std::unique_ptr<QTemporaryDir> mDir; | ||
|
||
// content of the archive | ||
QStringList mFiles; | ||
|
||
// zip filename | ||
QString mFilename; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters