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 int count = zip_get_num_files( z );
75 for (
int i = 0; i < count; i++ )
77 zip_stat_index( z, i, 0, &stat );
78 size_t len = stat.size;
80 struct zip_file *file = zip_fopen_index( z, i, 0 );
81 char *buf =
new char[len];
82 if ( zip_fread( file, buf, len ) != -1 )
84 QString fileName( stat.name );
85 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, 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, rc ) );
134 if ( zipFilename.isEmpty() )
141 const QByteArray fileNamePtr = zipFilename.toUtf8();
142 struct zip *z = zip_open( fileNamePtr.constData(), ZIP_CREATE, &rc );
144 if ( rc == ZIP_ER_OK && z )
146 for (
const auto &file : files )
148 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 int rc = ( int ) zip_add( z, fileInfoPtr.constData(), src );
164 int rc = ( int ) zip_file_add( z, fileInfoPtr.constData(), src, 0 );
CORE_EXPORT bool unzip(const QString &zip, const QString &dir, QStringList &files)
Unzip a zip file in an output directory.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::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 zip(const QString &zip, const QStringList &files)
Zip the list of files in the zip file.