31 return QFileInfo( filename ).suffix().compare( QLatin1String(
"qgz" ), Qt::CaseInsensitive ) == 0;
38 if ( !QFileInfo::exists( zipFilename ) )
40 QString err = QObject::tr(
"Error zip file does not exist: '%1'" ).arg( zipFilename );
44 else if ( zipFilename.isEmpty() )
46 QString err = QObject::tr(
"Error zip filename is empty" );
50 else if ( !QDir( dir ).exists( dir ) )
52 QString err = QObject::tr(
"Error output dir does not exist: '%1'" ).arg( dir );
56 else if ( !QFileInfo( dir ).isDir() )
58 QString err = QObject::tr(
"Error output dir is not a directory: '%1'" ).arg( dir );
62 else if ( !QFileInfo( dir ).isWritable() )
64 QString err = QObject::tr(
"Error output dir is not writable: '%1'" ).arg( dir );
70 const QByteArray fileNamePtr = zipFilename.toUtf8();
71 struct zip *z = zip_open( fileNamePtr.constData(), ZIP_CHECKCONS, &rc );
73 if ( rc == ZIP_ER_OK && z )
75 int count = zip_get_num_files( z );
80 for (
int i = 0; i < count; i++ )
82 zip_stat_index( z, i, 0, &stat );
83 size_t len = stat.size;
85 struct zip_file *file = zip_fopen_index( z, i, 0 );
86 char *buf =
new char[len];
87 if ( zip_fread( file, buf, len ) != -1 )
89 QString fileName( stat.name );
90 QFileInfo newFile( QDir( dir ), fileName );
93 if ( !newFile.absoluteDir().exists() )
95 if ( !QDir( dir ).mkpath( newFile.absolutePath() ) )
99 QFile outFile( newFile.absoluteFilePath() );
100 if ( !outFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
106 outFile.write( buf, len );
109 files.append( newFile.absoluteFilePath() );
114 QString err = QObject::tr(
"Error reading file: '%1'" ).arg( zip_strerror( z ) );
123 QString err = QObject::tr(
"Error getting files: '%1'" ).arg( zip_strerror( z ) );
132 QString err = QObject::tr(
"Error opening zip archive: '%1'" ).arg( z ? zip_strerror( z ) : zipFilename );
142 if ( zipFilename.isEmpty() )
144 QString err = QObject::tr(
"Error zip filename is empty" );
150 const QByteArray fileNamePtr = zipFilename.toUtf8();
151 struct zip *z = zip_open( fileNamePtr.constData(), ZIP_CREATE, &rc );
153 if ( rc == ZIP_ER_OK && z )
155 for (
const auto &file : files )
157 QFileInfo fileInfo( file );
158 if ( !fileInfo.exists() )
160 QString err = QObject::tr(
"Error input file does not exist: '%1'" ).arg( file );
166 const QByteArray fileNamePtr = file.toUtf8();
167 zip_source *src = zip_source_file( z, fileNamePtr.constData(), 0, 0 );
170 const QByteArray fileInfoPtr = fileInfo.fileName().toUtf8();
171 #if LIBZIP_VERSION_MAJOR < 1 172 int rc = ( int ) zip_add( z, fileInfoPtr.constData(), src );
174 int rc = ( int ) zip_file_add( z, fileInfoPtr.constData(), src, 0 );
178 QString err = QObject::tr(
"Error adding file '%1': %2" ).arg( file, zip_strerror( z ) );
186 QString err = QObject::tr(
"Error creating data source '%1': %2" ).arg( file, zip_strerror( z ) );
197 QString err = QObject::tr(
"Error creating zip archive '%1': %2" ).arg( zipFilename, zip_strerror( z ) );
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.