31 return QFileInfo( filename ).suffix().compare( QLatin1String(
"qgz" ), Qt::CaseInsensitive ) == 0;
38 if ( !QFileInfo::exists( zipFilename ) )
43 else if ( zipFilename.isEmpty() )
48 else if ( !QDir( dir ).exists( dir ) )
53 else if ( !QFileInfo( dir ).isDir() )
58 else if ( !QFileInfo( dir ).isWritable() )
65 const QByteArray fileNamePtr = zipFilename.toUtf8();
66 struct zip *z = zip_open( fileNamePtr.constData(), ZIP_CHECKCONS, &rc );
68 if ( rc == ZIP_ER_OK && z )
70 const int count = zip_get_num_files( z );
75 for (
int i = 0; i < count; i++ )
77 zip_stat_index( z, i, 0, &stat );
78 const size_t len = stat.size;
80 struct zip_file *file = zip_fopen_index( z, i, 0 );
81 const std::unique_ptr< char[] > buf(
new char[len] );
82 if ( zip_fread( file, buf.get(), len ) != -1 )
84 const QString fileName( stat.name );
85 const QFileInfo newFile( QDir( dir ), fileName );
88 if ( !newFile.absoluteDir().exists() )
90 if ( !QDir( dir ).mkpath( newFile.absolutePath() ) )
94 QFile outFile( newFile.absoluteFilePath() );
95 if ( !outFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
101 outFile.write( buf.get(), len );
104 files.append( newFile.absoluteFilePath() );
125 QgsMessageLog::logMessage( QObject::tr(
"Error opening zip archive: '%1' (Error code: %2)" ).arg( z ? zip_strerror( z ) : zipFilename ).arg( rc ) );
134 if ( zipFilename.isEmpty() )
141 const QByteArray zipFileNamePtr = zipFilename.toUtf8();
142 struct zip *z = zip_open( zipFileNamePtr.constData(), ZIP_CREATE, &rc );
144 if ( rc == ZIP_ER_OK && z )
146 for (
const auto &file : files )
148 const QFileInfo fileInfo( file );
149 if ( !fileInfo.exists() )
156 const QByteArray fileNamePtr = file.toUtf8();
157 zip_source *src = zip_source_file( z, fileNamePtr.constData(), 0, 0 );
160 const QByteArray fileInfoPtr = fileInfo.fileName().toUtf8();
161 #if LIBZIP_VERSION_MAJOR < 1
162 rc = ( int ) zip_add( z, fileInfoPtr.constData(), src );
164 rc = ( int ) zip_file_add( z, fileInfoPtr.constData(), src, 0 );
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
CORE_EXPORT bool isZipFile(const QString &filename)
Returns true if the file name is a zipped file ( i.e with a '.qgz' extension, false otherwise.
CORE_EXPORT bool unzip(const QString &zip, const QString &dir, QStringList &files)
Unzip a zip file in an output directory.
CORE_EXPORT bool zip(const QString &zip, const QStringList &files)
Zip the list of files in the zip file.