Skip to content

Commit

Permalink
Fix loss of project when saving to QGZ format and path contains non-a…
Browse files Browse the repository at this point in the history
…scii chars

Fixes #19567

(cherry picked from commit 7d7462c)
  • Loading branch information
nyalldawson committed Jan 24, 2019
1 parent e0a8de4 commit bfef851
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/core/qgsziputils.cpp
Expand Up @@ -22,6 +22,7 @@

#include "qgsmessagelog.h"
#include "qgsziputils.h"
#include "qgslogger.h"

#include <iostream>

Expand Down Expand Up @@ -66,7 +67,8 @@ bool QgsZipUtils::unzip( const QString &zipFilename, const QString &dir, QString
}

int rc = 0;
struct zip *z = zip_open( zipFilename.toStdString().c_str(), ZIP_CHECKCONS, &rc );
const QByteArray fileNamePtr = zipFilename.toUtf8();
struct zip *z = zip_open( fileNamePtr.constData(), ZIP_CHECKCONS, &rc );

if ( rc == ZIP_ER_OK && z )
{
Expand All @@ -84,8 +86,25 @@ bool QgsZipUtils::unzip( const QString &zipFilename, const QString &dir, QString
char *buf = new char[len];
if ( zip_fread( file, buf, len ) != -1 )
{
QFileInfo newFile( QDir( dir ), QString( stat.name ) );
std::ofstream( newFile.absoluteFilePath().toStdString() ).write( buf, len );
QString fileName( stat.name );
QFileInfo newFile( QDir( dir ), fileName );

// Create path for a new file if it does not exist.
if ( !newFile.absoluteDir().exists() )
{
if ( !QDir( dir ).mkpath( newFile.absolutePath() ) )
QgsMessageLog::logMessage( QStringLiteral( "Failed to create a subdirectory %1/%2" ).arg( dir ).arg( fileName ) );
}

QFile outFile( newFile.absoluteFilePath() );
if ( !outFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
QgsMessageLog::logMessage( QStringLiteral( "Could not write to %1" ).arg( newFile.absoluteFilePath() ) );
}
else
{
outFile.write( buf, len );
}
zip_fclose( file );
files.append( newFile.absoluteFilePath() );
}
Expand Down Expand Up @@ -128,7 +147,8 @@ bool QgsZipUtils::zip( const QString &zipFilename, const QStringList &files )
}

int rc = 0;
struct zip *z = zip_open( zipFilename.toStdString().c_str(), ZIP_CREATE, &rc );
const QByteArray fileNamePtr = zipFilename.toUtf8();
struct zip *z = zip_open( fileNamePtr.constData(), ZIP_CREATE, &rc );

if ( rc == ZIP_ER_OK && z )
{
Expand All @@ -143,25 +163,27 @@ bool QgsZipUtils::zip( const QString &zipFilename, const QStringList &files )
return false;
}

zip_source *src = zip_source_file( z, file.toStdString().c_str(), 0, 0 );
const QByteArray fileNamePtr = file.toUtf8();
zip_source *src = zip_source_file( z, fileNamePtr.constData(), 0, 0 );
if ( src )
{
const QByteArray fileInfoPtr = fileInfo.fileName().toUtf8();
#if LIBZIP_VERSION_MAJOR < 1
int rc = ( int ) zip_add( z, fileInfo.fileName().toStdString().c_str(), src );
int rc = ( int ) zip_add( z, fileInfoPtr.constData(), src );
#else
int rc = ( int ) zip_file_add( z, fileInfo.fileName().toStdString().c_str(), src, 0 );
int rc = ( int ) zip_file_add( z, fileInfoPtr.constData(), src, 0 );
#endif
if ( rc == -1 )
{
QString err = QObject::tr( "Error adding file: '%1'" ).arg( zip_strerror( z ) );
QString err = QObject::tr( "Error adding file '%1': %2" ).arg( file, zip_strerror( z ) );
QgsMessageLog::logMessage( err, QStringLiteral( "QgsZipUtils" ) );
zip_close( z );
return false;
}
}
else
{
QString err = QObject::tr( "Error creating data source: '%1'" ).arg( zip_strerror( z ) );
QString err = QObject::tr( "Error creating data source '%1': %2" ).arg( file, zip_strerror( z ) );
QgsMessageLog::logMessage( err, QStringLiteral( "QgsZipUtils" ) );
zip_close( z );
return false;
Expand All @@ -172,7 +194,7 @@ bool QgsZipUtils::zip( const QString &zipFilename, const QStringList &files )
}
else
{
QString err = QObject::tr( "Error creating zip archive: '%1'" ).arg( zip_strerror( z ) );
QString err = QObject::tr( "Error creating zip archive '%1': %2" ).arg( zipFilename, zip_strerror( z ) );
QgsMessageLog::logMessage( err, QStringLiteral( "QgsZipUtils" ) );
return false;
}
Expand Down

0 comments on commit bfef851

Please sign in to comment.