QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsziputils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsziputils.cpp
3 ---------------------
4 begin : Jul 2017
5 copyright : (C) 2017 by Paul Blottiere
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsziputils.h"
17
18#include <fstream>
19#include <iostream>
20#include <zip.h>
21#include <zlib.h>
22
23#include "qgslogger.h"
24#include "qgsmessagelog.h"
25
26#include <QDir>
27#include <QFileInfo>
28
29bool QgsZipUtils::isZipFile( const QString &filename )
30{
31 return QFileInfo( filename ).suffix().compare( QLatin1String( "qgz" ), Qt::CaseInsensitive ) == 0;
32}
33
34bool QgsZipUtils::unzip( const QString &zipFilename, const QString &dir, QStringList &files, bool checkConsistency )
35{
36 files.clear();
37
38 if ( !QFileInfo::exists( zipFilename ) )
39 {
40 QgsMessageLog::logMessage( QObject::tr( "Error zip file does not exist: '%1'" ).arg( zipFilename ) );
41 return false;
42 }
43 else if ( zipFilename.isEmpty() )
44 {
45 QgsMessageLog::logMessage( QObject::tr( "Error zip filename is empty" ) );
46 return false;
47 }
48 else if ( !QDir( dir ).exists( dir ) )
49 {
50 QgsMessageLog::logMessage( QObject::tr( "Error output dir does not exist: '%1'" ).arg( dir ) );
51 return false;
52 }
53 else if ( !QFileInfo( dir ).isDir() )
54 {
55 QgsMessageLog::logMessage( QObject::tr( "Error output dir is not a directory: '%1'" ).arg( dir ) );
56 return false;
57 }
58 else if ( !QFileInfo( dir ).isWritable() )
59 {
60 QgsMessageLog::logMessage( QObject::tr( "Error output dir is not writable: '%1'" ).arg( dir ) );
61 return false;
62 }
63
64 int rc = 0;
65 const QByteArray fileNamePtr = zipFilename.toUtf8();
66 struct zip *z = zip_open( fileNamePtr.constData(), checkConsistency ? ZIP_CHECKCONS : 0, &rc );
67
68 if ( rc == ZIP_ER_OK && z )
69 {
70 const int count = zip_get_num_entries( z, ZIP_FL_UNCHANGED );
71 if ( count != -1 )
72 {
73 struct zip_stat stat;
74
75 for ( int i = 0; i < count; i++ )
76 {
77 zip_stat_index( z, i, 0, &stat );
78 const size_t len = stat.size;
79
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 )
83 {
84 const QString fileName( stat.name );
85 if ( fileName.endsWith( "/" ) )
86 {
87 continue;
88 }
89
90 const QFileInfo newFile( QDir( dir ), fileName );
91
92 if ( !QString( QDir::cleanPath( newFile.absolutePath() ) + QStringLiteral( "/" ) ).startsWith( QDir( dir ).absolutePath() + QStringLiteral( "/" ) ) )
93 {
94 QgsMessageLog::logMessage( QObject::tr( "Skipped file %1 outside of the directory %2" ).arg(
95 newFile.absoluteFilePath(),
96 QDir( dir ).absolutePath()
97 ) );
98 continue;
99 }
100
101 // Create path for a new file if it does not exist.
102 if ( !newFile.absoluteDir().exists() )
103 {
104 if ( !QDir( dir ).mkpath( newFile.absolutePath() ) )
105 QgsMessageLog::logMessage( QObject::tr( "Failed to create a subdirectory %1/%2" ).arg( dir ).arg( fileName ) );
106 }
107
108 QFile outFile( newFile.absoluteFilePath() );
109 if ( !outFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
110 {
111 QgsMessageLog::logMessage( QObject::tr( "Could not write to %1" ).arg( newFile.absoluteFilePath() ) );
112 }
113 else
114 {
115 outFile.write( buf.get(), len );
116 }
117 zip_fclose( file );
118 files.append( newFile.absoluteFilePath() );
119 }
120 else
121 {
122 zip_fclose( file );
123 QgsMessageLog::logMessage( QObject::tr( "Error reading file: '%1'" ).arg( zip_strerror( z ) ) );
124 return false;
125 }
126 }
127 }
128 else
129 {
130 zip_close( z );
131 QgsMessageLog::logMessage( QObject::tr( "Error getting files: '%1'" ).arg( zip_strerror( z ) ) );
132 return false;
133 }
134
135 zip_close( z );
136 }
137 else
138 {
139 QgsMessageLog::logMessage( QObject::tr( "Error opening zip archive: '%1' (Error code: %2)" ).arg( z ? zip_strerror( z ) : zipFilename ).arg( rc ) );
140 return false;
141 }
142
143 return true;
144}
145
146bool QgsZipUtils::zip( const QString &zipFilename, const QStringList &files, bool overwrite )
147{
148 if ( zipFilename.isEmpty() )
149 {
150 QgsMessageLog::logMessage( QObject::tr( "Error zip filename is empty" ) );
151 return false;
152 }
153
154 int rc = 0;
155 const QByteArray zipFileNamePtr = zipFilename.toUtf8();
156 struct zip *z = zip_open( zipFileNamePtr.constData(), overwrite ? ( ZIP_CREATE | ZIP_TRUNCATE ) : ZIP_CREATE, &rc );
157
158 if ( rc == ZIP_ER_OK && z )
159 {
160 for ( const auto &file : files )
161 {
162 const QFileInfo fileInfo( file );
163 if ( !fileInfo.exists() )
164 {
165 QgsMessageLog::logMessage( QObject::tr( "Error input file does not exist: '%1'" ).arg( file ) );
166 zip_close( z );
167 return false;
168 }
169
170 const QByteArray fileNamePtr = file.toUtf8();
171 zip_source *src = zip_source_file( z, fileNamePtr.constData(), 0, 0 );
172 if ( src )
173 {
174 const QByteArray fileInfoPtr = fileInfo.fileName().toUtf8();
175#if LIBZIP_VERSION_MAJOR < 1
176 rc = ( int ) zip_add( z, fileInfoPtr.constData(), src );
177#else
178 rc = ( int ) zip_file_add( z, fileInfoPtr.constData(), src, 0 );
179#endif
180 if ( rc == -1 )
181 {
182 QgsMessageLog::logMessage( QObject::tr( "Error adding file '%1': %2" ).arg( file, zip_strerror( z ) ) );
183 zip_close( z );
184 return false;
185 }
186 }
187 else
188 {
189 QgsMessageLog::logMessage( QObject::tr( "Error creating data source '%1': %2" ).arg( file, zip_strerror( z ) ) );
190 zip_close( z );
191 return false;
192 }
193 }
194
195 zip_close( z );
196 }
197 else
198 {
199 QgsMessageLog::logMessage( QObject::tr( "Error creating zip archive '%1': %2" ).arg( zipFilename, z ? zip_strerror( z ) : zipFilename ) );
200 return false;
201 }
202
203 return true;
204}
205
206bool QgsZipUtils::decodeGzip( const QByteArray &bytesIn, QByteArray &bytesOut )
207{
208 return decodeGzip( bytesIn.constData(), bytesIn.count(), bytesOut );
209}
210
211bool QgsZipUtils::decodeGzip( const char *bytesIn, std::size_t size, QByteArray &bytesOut )
212{
213 unsigned char *bytesInPtr = reinterpret_cast<unsigned char *>( const_cast<char *>( bytesIn ) );
214 uint bytesInLeft = static_cast<uint>( size );
215
216 const uint CHUNK = 16384;
217 unsigned char out[CHUNK];
218 const int DEC_MAGIC_NUM_FOR_GZIP = 16;
219
220 // allocate inflate state
221 z_stream strm;
222 strm.zalloc = Z_NULL;
223 strm.zfree = Z_NULL;
224 strm.opaque = Z_NULL;
225 strm.avail_in = 0;
226 strm.next_in = Z_NULL;
227
228 int ret = inflateInit2( &strm, MAX_WBITS + DEC_MAGIC_NUM_FOR_GZIP );
229 if ( ret != Z_OK )
230 {
231 inflateEnd( &strm );
232 return false;
233 }
234
235 while ( ret != Z_STREAM_END ) // done when inflate() says it's done
236 {
237 // prepare next chunk
238 const uint bytesToProcess = std::min( CHUNK, bytesInLeft );
239 strm.next_in = bytesInPtr;
240 strm.avail_in = bytesToProcess;
241 bytesInPtr += bytesToProcess;
242 bytesInLeft -= bytesToProcess;
243
244 if ( bytesToProcess == 0 )
245 break; // we end with an error - no more data but inflate() wants more data
246
247 // run inflate() on input until output buffer not full
248 do
249 {
250 strm.avail_out = CHUNK;
251 strm.next_out = out;
252 ret = inflate( &strm, Z_NO_FLUSH );
253 Q_ASSERT( ret != Z_STREAM_ERROR ); // state not clobbered
254 if ( ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR )
255 {
256 inflateEnd( &strm );
257 return false;
258 }
259 const unsigned have = CHUNK - strm.avail_out;
260 bytesOut.append( QByteArray::fromRawData( reinterpret_cast<const char *>( out ), static_cast<int>( have ) ) );
261 }
262 while ( strm.avail_out == 0 );
263 }
264
265 inflateEnd( &strm );
266 return ret == Z_STREAM_END;
267}
268
269bool QgsZipUtils::encodeGzip( const QByteArray &bytesIn, QByteArray &bytesOut )
270{
271 unsigned char *bytesInPtr = reinterpret_cast<unsigned char *>( const_cast<char *>( bytesIn.constData() ) );
272 const uint bytesInLeft = static_cast<uint>( bytesIn.count() );
273
274 const uint CHUNK = 16384;
275 unsigned char out[CHUNK];
276 const int DEC_MAGIC_NUM_FOR_GZIP = 16;
277
278 // allocate deflate state
279 z_stream strm;
280 strm.zalloc = Z_NULL;
281 strm.zfree = Z_NULL;
282 strm.opaque = Z_NULL;
283
284 int ret = deflateInit2( &strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + DEC_MAGIC_NUM_FOR_GZIP, 8, Z_DEFAULT_STRATEGY );
285 if ( ret != Z_OK )
286 return false;
287
288 strm.avail_in = bytesInLeft;
289 strm.next_in = bytesInPtr;
290
291 // run deflate() on input until output buffer not full, finish
292 // compression if all of source has been read in
293 do
294 {
295 strm.avail_out = CHUNK;
296 strm.next_out = out;
297 ret = deflate( &strm, Z_FINISH ); // no bad return value
298 Q_ASSERT( ret != Z_STREAM_ERROR ); // state not clobbered
299
300 const unsigned have = CHUNK - strm.avail_out;
301 bytesOut.append( QByteArray::fromRawData( reinterpret_cast<const char *>( out ), static_cast<int>( have ) ) );
302 }
303 while ( strm.avail_out == 0 );
304 Q_ASSERT( ret == Z_STREAM_END ); // stream will be complete
305
306 // clean up and return
307 deflateEnd( &strm );
308 return true;
309}
310
311const QStringList QgsZipUtils::files( const QString &zip )
312{
313 if ( zip.isEmpty() && !QFileInfo::exists( zip ) )
314 {
315 return QStringList();
316 }
317 QStringList files;
318
319 int rc = 0;
320 const QByteArray fileNamePtr = zip.toUtf8();
321 struct zip *z = zip_open( fileNamePtr.constData(), 0, &rc );
322
323 if ( rc == ZIP_ER_OK && z )
324 {
325 const int count = zip_get_num_entries( z, ZIP_FL_UNCHANGED );
326 if ( count != -1 )
327 {
328 struct zip_stat stat;
329
330 for ( int i = 0; i < count; i++ )
331 {
332 zip_stat_index( z, i, 0, &stat );
333 files << QString( stat.name );
334 }
335 }
336
337 zip_close( z );
338 }
339
340 return files;
341}
342
343bool QgsZipUtils::extractFileFromZip( const QString &zipFilename, const QString &filenameInZip, QByteArray &bytesOut )
344{
345 if ( !QFileInfo::exists( zipFilename ) )
346 {
347 QgsMessageLog::logMessage( QObject::tr( "Error zip file does not exist: '%1'" ).arg( zipFilename ) );
348 return false;
349 }
350
351 if ( filenameInZip.isEmpty() )
352 {
353 QgsMessageLog::logMessage( QObject::tr( "Error file name in zip is empty" ) );
354 return false;
355 }
356
357 int err = 0;
358 const QByteArray zipFilenamePtr = zipFilename.toUtf8();
359 struct zip *z = zip_open( zipFilenamePtr.constData(), 0, &err );
360 if ( !z )
361 {
362 zip_error_t error;
363 zip_error_init_with_code( &error, err );
364 QgsMessageLog::logMessage( QObject::tr( "Error opening zip archive '%1': %2" ).arg( zipFilename, zip_error_strerror( &error ) ) );
365 zip_error_fini( &error );
366 return false;
367 }
368
369 const QByteArray filenameInZipPtr = filenameInZip.toUtf8();
370 struct zip_stat st;
371 zip_stat_init( &st );
372 if ( zip_stat( z, filenameInZipPtr.constData(), 0, &st ) != 0 )
373 {
374 QgsMessageLog::logMessage( QObject::tr( "File '%1' not found in zip archive '%2': %3" ).arg( filenameInZip, zipFilename, zip_strerror( z ) ) );
375 zip_close( z );
376 return false;
377 }
378
379 zip_file *zf = zip_fopen( z, filenameInZipPtr.constData(), 0 );
380 if ( !zf )
381 {
382 QgsMessageLog::logMessage( QObject::tr( "Could not open file '%1' in zip archive '%2': %3" ).arg( filenameInZip, zipFilename, zip_strerror( z ) ) );
383 zip_close( z );
384 return false;
385 }
386
387 bytesOut.resize( static_cast<int>( st.size ) );
388 zip_int64_t readBytes = zip_fread( zf, bytesOut.data(), st.size );
389
390 zip_fclose( zf );
391 zip_close( z );
392
393 // If successful, the number of bytes actually read is returned.
394 if ( static_cast<zip_uint64_t>( readBytes ) != st.size )
395 {
396 QgsMessageLog::logMessage( QObject::tr( "Error reading file '%1' from zip archive '%2'." ).arg( filenameInZip, zipFilename ) );
397 return false;
398 }
399
400 return true;
401}
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
static bool extractFileFromZip(const QString &zipFilename, const QString &filenameInZip, QByteArray &bytesOut)
Extracts a file from a zip archive, returns true on success.
static bool isZipFile(const QString &filename)
Returns true if the file name is a zipped file ( i.e with a '.qgz' extension, false otherwise.
static bool zip(const QString &zip, const QStringList &files, bool overwrite=false)
Zip the list of files in the zip file.
static bool unzip(const QString &zip, const QString &dir, QStringList &files, bool checkConsistency=true)
Unzip a zip file in an output directory.
static bool decodeGzip(const QByteArray &bytesIn, QByteArray &bytesOut)
Decodes gzip byte stream, returns true on success.
static bool encodeGzip(const QByteArray &bytesIn, QByteArray &bytesOut)
Encodes gzip byte stream, returns true on success.
static const QStringList files(const QString &zip)
Returns the list of files within a zip file.