ckb-next  v0.2.8 at branch master
ckb-next driver for corsair devices
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JlCompress.cpp
Go to the documentation of this file.
1 /*
2 Copyright (C) 2010 Roberto Pompermaier
3 Copyright (C) 2005-2014 Sergey A. Tachenov
4 
5 This file is part of QuaZIP.
6 
7 QuaZIP is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 2.1 of the License, or
10 (at your option) any later version.
11 
12 QuaZIP is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with QuaZIP. If not, see <http://www.gnu.org/licenses/>.
19 
20 See COPYING file for the full LGPL text.
21 
22 Original ZIP package is copyrighted by Gilles Vollant and contributors,
23 see quazip/(un)zip.h files for details. Basically it's the zlib license.
24 */
25 
26 #include "JlCompress.h"
27 #include <QDebug>
28 
29 static bool copyData(QIODevice &inFile, QIODevice &outFile)
30 {
31  while (!inFile.atEnd()) {
32  char buf[4096];
33  qint64 readLen = inFile.read(buf, 4096);
34  if (readLen <= 0)
35  return false;
36  if (outFile.write(buf, readLen) != readLen)
37  return false;
38  }
39  return true;
40 }
41 
53 bool JlCompress::compressFile(QuaZip* zip, QString fileName, QString fileDest) {
54  // zip: oggetto dove aggiungere il file
55  // fileName: nome del file reale
56  // fileDest: nome del file all'interno del file compresso
57 
58  // Controllo l'apertura dello zip
59  if (!zip) return false;
60  if (zip->getMode()!=QuaZip::mdCreate &&
61  zip->getMode()!=QuaZip::mdAppend &&
62  zip->getMode()!=QuaZip::mdAdd) return false;
63 
64  // Apro il file originale
65  QFile inFile;
66  inFile.setFileName(fileName);
67  if(!inFile.open(QIODevice::ReadOnly)) return false;
68 
69  // Apro il file risulato
70  QuaZipFile outFile(zip);
71  if(!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileDest, inFile.fileName()))) return false;
72 
73  // Copio i dati
74  if (!copyData(inFile, outFile) || outFile.getZipError()!=UNZ_OK) {
75  return false;
76  }
77 
78  // Chiudo i file
79  outFile.close();
80  if (outFile.getZipError()!=UNZ_OK) return false;
81  inFile.close();
82 
83  return true;
84 }
85 
103 bool JlCompress::compressSubDir(QuaZip* zip, QString dir, QString origDir, bool recursive) {
104  // zip: oggetto dove aggiungere il file
105  // dir: cartella reale corrente
106  // origDir: cartella reale originale
107  // (path(dir)-path(origDir)) = path interno all'oggetto zip
108 
109  // Controllo l'apertura dello zip
110  if (!zip) return false;
111  if (zip->getMode()!=QuaZip::mdCreate &&
112  zip->getMode()!=QuaZip::mdAppend &&
113  zip->getMode()!=QuaZip::mdAdd) return false;
114 
115  // Controllo la cartella
116  QDir directory(dir);
117  if (!directory.exists()) return false;
118 
119  QDir origDirectory(origDir);
120  if (dir != origDir) {
121  QuaZipFile dirZipFile(zip);
122  if (!dirZipFile.open(QIODevice::WriteOnly,
123  QuaZipNewInfo(origDirectory.relativeFilePath(dir) + "/", dir), 0, 0, 0)) {
124  return false;
125  }
126  dirZipFile.close();
127  }
128 
129 
130  // Se comprimo anche le sotto cartelle
131  if (recursive) {
132  // Per ogni sotto cartella
133  QFileInfoList files = directory.entryInfoList(QDir::AllDirs|QDir::NoDotAndDotDot);
134  Q_FOREACH (QFileInfo file, files) {
135  // Comprimo la sotto cartella
136  if(!compressSubDir(zip,file.absoluteFilePath(),origDir,recursive)) return false;
137  }
138  }
139 
140  // Per ogni file nella cartella
141  QFileInfoList files = directory.entryInfoList(QDir::Files);
142  Q_FOREACH (QFileInfo file, files) {
143  // Se non e un file o e il file compresso che sto creando
144  if(!file.isFile()||file.absoluteFilePath()==zip->getZipName()) continue;
145 
146  // Creo il nome relativo da usare all'interno del file compresso
147  QString filename = origDirectory.relativeFilePath(file.absoluteFilePath());
148 
149  // Comprimo il file
150  if (!compressFile(zip,file.absoluteFilePath(),filename)) return false;
151  }
152 
153  return true;
154 }
155 
170 bool JlCompress::extractFile(QuaZip* zip, QString fileName, QString fileDest) {
171  // zip: oggetto dove aggiungere il file
172  // filename: nome del file reale
173  // fileincompress: nome del file all'interno del file compresso
174 
175  // Controllo l'apertura dello zip
176  if (!zip) return false;
177  if (zip->getMode()!=QuaZip::mdUnzip) return false;
178 
179  // Apro il file compresso
180  if (!fileName.isEmpty())
181  zip->setCurrentFile(fileName);
182  QuaZipFile inFile(zip);
183  if(!inFile.open(QIODevice::ReadOnly) || inFile.getZipError()!=UNZ_OK) return false;
184 
185  // Controllo esistenza cartella file risultato
186  QDir curDir;
187  if (fileDest.endsWith('/')) {
188  if (!curDir.mkpath(fileDest)) {
189  return false;
190  }
191  } else {
192  if (!curDir.mkpath(QFileInfo(fileDest).absolutePath())) {
193  return false;
194  }
195  }
196 
197  QuaZipFileInfo64 info;
198  if (!zip->getCurrentFileInfo(&info))
199  return false;
200 
201  QFile::Permissions srcPerm = info.getPermissions();
202  if (fileDest.endsWith('/') && QFileInfo(fileDest).isDir()) {
203  if (srcPerm != 0) {
204  QFile(fileDest).setPermissions(srcPerm);
205  }
206  return true;
207  }
208 
209  // Apro il file risultato
210  QFile outFile;
211  outFile.setFileName(fileDest);
212  if(!outFile.open(QIODevice::WriteOnly)) return false;
213 
214  // Copio i dati
215  if (!copyData(inFile, outFile) || inFile.getZipError()!=UNZ_OK) {
216  outFile.close();
217  removeFile(QStringList(fileDest));
218  return false;
219  }
220  outFile.close();
221 
222  // Chiudo i file
223  inFile.close();
224  if (inFile.getZipError()!=UNZ_OK) {
225  removeFile(QStringList(fileDest));
226  return false;
227  }
228 
229  if (srcPerm != 0) {
230  outFile.setPermissions(srcPerm);
231  }
232  return true;
233 }
234 
241 bool JlCompress::removeFile(QStringList listFile) {
242  bool ret = true;
243  // Per ogni file
244  for (int i=0; i<listFile.count(); i++) {
245  // Lo elimino
246  ret = ret && QFile::remove(listFile.at(i));
247  }
248  return ret;
249 }
250 
253 
263 bool JlCompress::compressFile(QString fileCompressed, QString file) {
264  // Creo lo zip
265  QuaZip zip(fileCompressed);
266  QDir().mkpath(QFileInfo(fileCompressed).absolutePath());
267  if(!zip.open(QuaZip::mdCreate)) {
268  QFile::remove(fileCompressed);
269  return false;
270  }
271 
272  // Aggiungo il file
273  if (!compressFile(&zip,file,QFileInfo(file).fileName())) {
274  QFile::remove(fileCompressed);
275  return false;
276  }
277 
278  // Chiudo il file zip
279  zip.close();
280  if(zip.getZipError()!=0) {
281  QFile::remove(fileCompressed);
282  return false;
283  }
284 
285  return true;
286 }
287 
298 bool JlCompress::compressFiles(QString fileCompressed, QStringList files) {
299  // Creo lo zip
300  QuaZip zip(fileCompressed);
301  QDir().mkpath(QFileInfo(fileCompressed).absolutePath());
302  if(!zip.open(QuaZip::mdCreate)) {
303  QFile::remove(fileCompressed);
304  return false;
305  }
306 
307  // Comprimo i file
308  QFileInfo info;
309  Q_FOREACH (QString file, files) {
310  info.setFile(file);
311  if (!info.exists() || !compressFile(&zip,file,info.fileName())) {
312  QFile::remove(fileCompressed);
313  return false;
314  }
315  }
316 
317  // Chiudo il file zip
318  zip.close();
319  if(zip.getZipError()!=0) {
320  QFile::remove(fileCompressed);
321  return false;
322  }
323 
324  return true;
325 }
326 
338 bool JlCompress::compressDir(QString fileCompressed, QString dir, bool recursive) {
339  // Creo lo zip
340  QuaZip zip(fileCompressed);
341  QDir().mkpath(QFileInfo(fileCompressed).absolutePath());
342  if(!zip.open(QuaZip::mdCreate)) {
343  QFile::remove(fileCompressed);
344  return false;
345  }
346 
347  // Aggiungo i file e le sotto cartelle
348  if (!compressSubDir(&zip,dir,dir,recursive)) {
349  QFile::remove(fileCompressed);
350  return false;
351  }
352 
353  // Chiudo il file zip
354  zip.close();
355  if(zip.getZipError()!=0) {
356  QFile::remove(fileCompressed);
357  return false;
358  }
359 
360  return true;
361 }
362 
365 
377 QString JlCompress::extractFile(QString fileCompressed, QString fileName, QString fileDest) {
378  // Apro lo zip
379  QuaZip zip(fileCompressed);
380  if(!zip.open(QuaZip::mdUnzip)) {
381  return QString();
382  }
383 
384  // Estraggo il file
385  if (fileDest.isEmpty())
386  fileDest = fileName;
387  if (!extractFile(&zip,fileName,fileDest)) {
388  return QString();
389  }
390 
391  // Chiudo il file zip
392  zip.close();
393  if(zip.getZipError()!=0) {
394  removeFile(QStringList(fileDest));
395  return QString();
396  }
397  return QFileInfo(fileDest).absoluteFilePath();
398 }
399 
412 QStringList JlCompress::extractFiles(QString fileCompressed, QStringList files, QString dir) {
413  // Creo lo zip
414  QuaZip zip(fileCompressed);
415  if(!zip.open(QuaZip::mdUnzip)) {
416  return QStringList();
417  }
418 
419  // Estraggo i file
420  QStringList extracted;
421  for (int i=0; i<files.count(); i++) {
422  QString absPath = QDir(dir).absoluteFilePath(files.at(i));
423  if (!extractFile(&zip, files.at(i), absPath)) {
424  removeFile(extracted);
425  return QStringList();
426  }
427  extracted.append(absPath);
428  }
429 
430  // Chiudo il file zip
431  zip.close();
432  if(zip.getZipError()!=0) {
433  removeFile(extracted);
434  return QStringList();
435  }
436 
437  return extracted;
438 }
439 
451 QStringList JlCompress::extractDir(QString fileCompressed, QString dir) {
452  // Apro lo zip
453  QuaZip zip(fileCompressed);
454  if(!zip.open(QuaZip::mdUnzip)) {
455  return QStringList();
456  }
457 
458  QDir directory(dir);
459  QStringList extracted;
460  if (!zip.goToFirstFile()) {
461  return QStringList();
462  }
463  do {
464  QString name = zip.getCurrentFileName();
465  QString absFilePath = directory.absoluteFilePath(name);
466  if (!extractFile(&zip, "", absFilePath)) {
467  removeFile(extracted);
468  return QStringList();
469  }
470  extracted.append(absFilePath);
471  } while (zip.goToNextFile());
472 
473  // Chiudo il file zip
474  zip.close();
475  if(zip.getZipError()!=0) {
476  removeFile(extracted);
477  return QStringList();
478  }
479 
480  return extracted;
481 }
482 
492 QStringList JlCompress::getFileList(QString fileCompressed) {
493  // Apro lo zip
494  QuaZip* zip = new QuaZip(QFileInfo(fileCompressed).absoluteFilePath());
495  if(!zip->open(QuaZip::mdUnzip)) {
496  delete zip;
497  return QStringList();
498  }
499 
500  // Estraggo i nomi dei file
501  QStringList lst;
502  QuaZipFileInfo64 info;
503  for(bool more=zip->goToFirstFile(); more; more=zip->goToNextFile()) {
504  if(!zip->getCurrentFileInfo(&info)) {
505  delete zip;
506  return QStringList();
507  }
508  lst << info.name;
509  //info.name.toLocal8Bit().constData()
510  }
511 
512  // Chiudo il file zip
513  zip->close();
514  if(zip->getZipError()!=0) {
515  delete zip;
516  return QStringList();
517  }
518  delete zip;
519 
520  return lst;
521 }
522 
static QStringList extractDir(QString fileCompressed, QString dir=QString())
Extract a whole archive.
Definition: JlCompress.cpp:451
static bool compressFile(QuaZip *zip, QString fileName, QString fileDest)
Compress a single file.
Definition: JlCompress.cpp:53
bool goToFirstFile()
Sets the current file to the first file in the archive.
Definition: quazip.cpp:466
QString getCurrentFileName() const
Returns the current file name.
Definition: quazip.cpp:551
#define UNZ_OK
Definition: unzip.h:79
Information about a file to be created.
Definition: quazipnewinfo.h:50
static bool compressSubDir(QuaZip *parentZip, QString dir, QString parentDir, bool recursive=true)
Compress a subdirectory.
Definition: JlCompress.cpp:103
ZIP file was opened for adding files in the archive.
Definition: quazip.h:106
Mode getMode() const
Returns the mode in which ZIP file was opened.
Definition: quazip.cpp:614
ZIP file was created with open() call.
Definition: quazip.h:97
virtual void close()
Closes the file.
Definition: quazipfile.cpp:432
int getZipError() const
Returns the error code of the last operation.
Definition: quazip.cpp:624
static bool compressDir(QString fileCompressed, QString dir=QString(), bool recursive=true)
Compress a whole directory.
Definition: JlCompress.cpp:338
static bool removeFile(QStringList listFile)
Remove some files.
Definition: JlCompress.cpp:241
int getZipError() const
Returns the error code returned by the last ZIP/UNZIP API call.
Definition: quazipfile.cpp:494
QFile::Permissions getPermissions() const
Get the file permissions.
QString getZipName() const
Returns the name of the ZIP file.
Definition: quazip.cpp:602
bool goToNextFile()
Sets the current file to the next file in the archive.
Definition: quazip.cpp:478
static QStringList getFileList(QString fileCompressed)
Get the file list.
Definition: JlCompress.cpp:492
Information about a file inside archive (with zip64 support).
static bool extractFile(QuaZip *zip, QString fileName, QString fileDest)
Extract a single file.
Definition: JlCompress.cpp:170
bool setCurrentFile(const QString &fileName, CaseSensitivity cs=csDefault)
Sets current file by its name.
Definition: quazip.cpp:408
ZIP file is open for reading files inside it.
Definition: quazip.h:96
void close()
Closes ZIP file.
Definition: quazip.cpp:324
static QStringList extractFiles(QString fileCompressed, QStringList files, QString dir=QString())
Extract a list of files.
Definition: JlCompress.cpp:412
ZIP archive.
Definition: quazip.h:84
A file inside ZIP archive.
Definition: quazipfile.h:74
static bool copyData(QIODevice &inFile, QIODevice &outFile)
Definition: JlCompress.cpp:29
virtual bool open(OpenMode mode)
Opens a file for reading.
Definition: quazipfile.cpp:221
ZIP file was opened in append mode.
Definition: quazip.h:98
bool open(Mode mode, zlib_filefunc_def *ioApi=NULL)
Opens ZIP file.
Definition: quazip.cpp:215
static bool compressFiles(QString fileCompressed, QStringList files)
Compress a list of files.
Definition: JlCompress.cpp:298
QString name
File name.
bool getCurrentFileInfo(QuaZipFileInfo *info) const
Retrieves information about the current file.
Definition: quazip.cpp:492