30 return QFileInfo( filename ).suffix().compare( QLatin1String(
"qgz" ), Qt::CaseInsensitive ) == 0;
37 if ( !QFileInfo::exists( zipFilename ) )
39 QString err = QObject::tr(
"Error zip file does not exist: '%1'" ).arg( zipFilename );
43 else if ( zipFilename.isEmpty() )
45 QString err = QObject::tr(
"Error zip filename is empty" );
49 else if ( !QDir( dir ).exists( dir ) )
51 QString err = QObject::tr(
"Error output dir does not exist: '%1'" ).arg( dir );
55 else if ( !QFileInfo( dir ).isDir() )
57 QString err = QObject::tr(
"Error output dir is not a directory: '%1'" ).arg( dir );
61 else if ( !QFileInfo( dir ).isWritable() )
63 QString err = QObject::tr(
"Error output dir is not writable: '%1'" ).arg( dir );
69 struct zip *z = zip_open( zipFilename.toStdString().c_str(), ZIP_CHECKCONS, &rc );
71 if ( rc == ZIP_ER_OK && z )
73 int count = zip_get_num_files( z );
78 for (
int i = 0; i < count; i++ )
80 zip_stat_index( z, i, 0, &stat );
81 size_t len = stat.size;
83 struct zip_file *file = zip_fopen_index( z, i, 0 );
84 char *buf =
new char[len];
85 if ( zip_fread( file, buf, len ) != -1 )
87 QFileInfo newFile( QDir( dir ), QString( stat.name ) );
88 std::ofstream( newFile.absoluteFilePath().toStdString() ).write( buf, len );
90 files.append( newFile.absoluteFilePath() );
95 QString err = QObject::tr(
"Error reading file: '%1'" ).arg( zip_strerror( z ) );
104 QString err = QObject::tr(
"Error getting files: '%1'" ).arg( zip_strerror( z ) );
113 QString err = QObject::tr(
"Error opening zip archive: '%1'" ).arg( zip_strerror( z ) );
123 if ( zipFilename.isEmpty() )
125 QString err = QObject::tr(
"Error zip filename is empty" );
131 struct zip *z = zip_open( zipFilename.toStdString().c_str(), ZIP_CREATE, &rc );
133 if ( rc == ZIP_ER_OK && z )
135 for (
const auto &file : files )
137 QFileInfo fileInfo( file );
138 if ( !fileInfo.exists() )
140 QString err = QObject::tr(
"Error input file does not exist: '%1'" ).arg( file );
146 zip_source *src = zip_source_file( z, file.toStdString().c_str(), 0, 0 );
149 #if LIBZIP_VERSION_MAJOR < 1 150 int rc = ( int ) zip_add( z, fileInfo.fileName().toStdString().c_str(), src );
152 int rc = ( int ) zip_file_add( z, fileInfo.fileName().toStdString().c_str(), src, 0 );
156 QString err = QObject::tr(
"Error adding file: '%1'" ).arg( zip_strerror( z ) );
164 QString err = QObject::tr(
"Error creating data source: '%1'" ).arg( zip_strerror( z ) );
175 QString err = QObject::tr(
"Error creating zip archive: '%1'" ).arg( zip_strerror( z ) );
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning)
add a message to the instance (and create it if necessary)
CORE_EXPORT bool unzip(const QString &zip, const QString &dir, QStringList &files)
Unzip a zip file in an output directory.
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.