misc/quazip/JlCompress.cpp
author nemo
Sun, 11 Sep 2011 10:46:53 -0400
changeset 5856 ed97138dc414
parent 5752 ea95ee97c805
permissions -rw-r--r--
Should prevent a crasher when drowning while firing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5752
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
     1
#include "JlCompress.h"
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
     2
#include <QDebug>
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
     3
/**OK
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
     4
 * Comprime il file fileName, nell'oggetto zip, con il nome fileDest.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
     5
 *
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
     6
 * La funzione fallisce se:
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
     7
 * * zip==NULL;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
     8
 * * l'oggetto zip è stato aperto in una modalità non compatibile con l'aggiunta di file;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
     9
 * * non è possibile aprire il file d'origine;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    10
 * * non è possibile creare il file all'interno dell'oggetto zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    11
 * * si è rilevato un errore nella copia dei dati;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    12
 * * non è stato possibile chiudere il file all'interno dell'oggetto zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    13
 */
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    14
bool JlCompress::compressFile(QuaZip* zip, QString fileName, QString fileDest) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    15
    // zip: oggetto dove aggiungere il file
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    16
    // fileName: nome del file reale
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    17
    // fileDest: nome del file all'interno del file compresso
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    18
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    19
    // Controllo l'apertura dello zip
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    20
    if (!zip) return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    21
    if (zip->getMode()!=QuaZip::mdCreate &&
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    22
        zip->getMode()!=QuaZip::mdAppend &&
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    23
        zip->getMode()!=QuaZip::mdAdd) return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    24
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    25
    // Apro il file originale
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    26
    QFile inFile;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    27
    inFile.setFileName(fileName);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    28
    if(!inFile.open(QIODevice::ReadOnly)) return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    29
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    30
    // Apro il file risulato
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    31
    QuaZipFile outFile(zip);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    32
    if(!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileDest, inFile.fileName()))) return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    33
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    34
    // Copio i dati
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    35
    char c;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    36
    while(inFile.getChar(&c)&&outFile.putChar(c));
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    37
    if(outFile.getZipError()!=UNZ_OK) return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    38
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    39
    // Chiudo i file
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    40
    outFile.close();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    41
    if (outFile.getZipError()!=UNZ_OK) return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    42
    inFile.close();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    43
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    44
    return true;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    45
}
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    46
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    47
/**OK
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    48
 * Comprime la cartella dir nel file fileCompressed, se recursive è true allora
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    49
 * comprime anche le sotto cartelle. I nomi dei file preceduti dal path creato
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    50
 * togliendo il pat della cartella origDir al path della cartella dir.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    51
 * Se la funzione fallisce restituisce false e cancella il file che si è tentato
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    52
 * di creare.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    53
 *
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    54
 * La funzione fallisce se:
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    55
 * * zip==NULL;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    56
 * * l'oggetto zip è stato aperto in una modalità non compatibile con l'aggiunta di file;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    57
 * * la cartella dir non esiste;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    58
 * * la compressione di una sotto cartella fallisce (1);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    59
 * * la compressione di un file fallisce;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    60
 * (1) La funzione si richiama in maniera ricorsiva per comprimere le sotto cartelle
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    61
 * dunque gli errori di compressione di una sotto cartella sono gli stessi di questa
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    62
 * funzione.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    63
 */
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    64
bool JlCompress::compressSubDir(QuaZip* zip, QString dir, QString origDir, bool recursive) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    65
    // zip: oggetto dove aggiungere il file
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    66
    // dir: cartella reale corrente
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    67
    // origDir: cartella reale originale
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    68
    // (path(dir)-path(origDir)) = path interno all'oggetto zip
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    69
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    70
    // Controllo l'apertura dello zip
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    71
    if (!zip) return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    72
    if (zip->getMode()!=QuaZip::mdCreate &&
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    73
        zip->getMode()!=QuaZip::mdAppend &&
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    74
        zip->getMode()!=QuaZip::mdAdd) return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    75
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    76
    // Controllo la cartella
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    77
    QDir directory(dir);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    78
    if (!directory.exists()) return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    79
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    80
    // Se comprimo anche le sotto cartelle
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    81
    if (recursive) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    82
        // Per ogni sotto cartella
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    83
        QFileInfoList files = directory.entryInfoList(QDir::AllDirs|QDir::NoDotAndDotDot);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    84
        foreach (QFileInfo file, files) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    85
            // Comprimo la sotto cartella
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    86
            if(!compressSubDir(zip,file.absoluteFilePath(),origDir,recursive)) return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    87
        }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    88
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    89
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    90
    // Per ogni file nella cartella
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    91
    QFileInfoList files = directory.entryInfoList(QDir::Files);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    92
    QDir origDirectory(origDir);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    93
    foreach (QFileInfo file, files) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    94
        // Se non è un file o è il file compresso che sto creando
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    95
        if(!file.isFile()||file.absoluteFilePath()==zip->getZipName()) continue;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    96
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    97
        // Creo il nome relativo da usare all'interno del file compresso
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    98
        QString filename = origDirectory.relativeFilePath(file.absoluteFilePath());
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
    99
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   100
        // Comprimo il file
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   101
        if (!compressFile(zip,file.absoluteFilePath(),filename)) return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   102
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   103
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   104
    return true;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   105
}
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   106
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   107
/**OK
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   108
 * Estrae il file fileName, contenuto nell'oggetto zip, con il nome fileDest.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   109
 * Se la funzione fallisce restituisce false e cancella il file che si è tentato di estrarre.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   110
 *
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   111
 * La funzione fallisce se:
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   112
 * * zip==NULL;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   113
 * * l'oggetto zip è stato aperto in una modalità non compatibile con l'estrazione di file;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   114
 * * non è possibile aprire il file all'interno dell'oggetto zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   115
 * * non è possibile creare il file estratto;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   116
 * * si è rilevato un errore nella copia dei dati (1);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   117
 * * non è stato possibile chiudere il file all'interno dell'oggetto zip (1);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   118
 *
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   119
 * (1): prima di uscire dalla funzione cancella il file estratto.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   120
 */
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   121
bool JlCompress::extractFile(QuaZip* zip, QString fileName, QString fileDest) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   122
    // zip: oggetto dove aggiungere il file
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   123
    // filename: nome del file reale
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   124
    // fileincompress: nome del file all'interno del file compresso
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   125
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   126
    // Controllo l'apertura dello zip
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   127
    if (!zip) return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   128
    if (zip->getMode()!=QuaZip::mdUnzip) return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   129
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   130
    // Apro il file compresso
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   131
    zip->setCurrentFile(fileName);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   132
    QuaZipFile inFile(zip);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   133
    if(!inFile.open(QIODevice::ReadOnly) || inFile.getZipError()!=UNZ_OK) return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   134
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   135
    // Controllo esistenza cartella file risultato
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   136
    QDir().mkpath(QFileInfo(fileDest).absolutePath());
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   137
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   138
    // Apro il file risultato
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   139
    QFile outFile;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   140
    outFile.setFileName(fileDest);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   141
    if(!outFile.open(QIODevice::WriteOnly)) return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   142
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   143
    // Copio i dati
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   144
    char c;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   145
    while(inFile.getChar(&c)) outFile.putChar(c);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   146
    if (inFile.getZipError()!=UNZ_OK) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   147
        removeFile(QStringList(fileDest));
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   148
        return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   149
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   150
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   151
    // Chiudo i file
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   152
    inFile.close();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   153
    if (inFile.getZipError()!=UNZ_OK) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   154
        removeFile(QStringList(fileDest));
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   155
        return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   156
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   157
    outFile.close();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   158
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   159
    return true;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   160
}
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   161
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   162
/**
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   163
 * Rimuove i file il cui nome è specificato all'interno di listFile.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   164
 * Restituisce true se tutti i file sono stati cancellati correttamente, attenzione
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   165
 * perchè può restituire false anche se alcuni file non esistevano e si è tentato
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   166
 * di cancellarli.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   167
 */
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   168
bool JlCompress::removeFile(QStringList listFile) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   169
    bool ret = true;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   170
    // Per ogni file
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   171
    for (int i=0; i<listFile.count(); i++) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   172
        // Lo elimino
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   173
        ret = ret && QFile::remove(listFile.at(i));
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   174
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   175
    return ret;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   176
}
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   177
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   178
////////////////////////////////////////////////////////////////////////////////
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   179
////////////////////////////////////////////////////////////////////////////////
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   180
/**OK
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   181
 * Comprime il file fileName nel file fileCompressed.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   182
 * Se la funzione fallisce restituisce false e cancella il file che si è tentato
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   183
 * di creare.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   184
 *
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   185
 * La funzione fallisce se:
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   186
 * * non si riesce ad aprire l'oggetto zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   187
 * * la compressione del file fallisce;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   188
 * * non si riesce a chiudere l'oggetto zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   189
 */
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   190
bool JlCompress::compressFile(QString fileCompressed, QString file) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   191
    // Creo lo zip
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   192
    QuaZip* zip  = new QuaZip(QFileInfo(fileCompressed).absoluteFilePath());
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   193
    QDir().mkpath(QFileInfo(fileCompressed).absolutePath());
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   194
    if(!zip->open(QuaZip::mdCreate)) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   195
        delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   196
        QFile::remove(fileCompressed);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   197
        return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   198
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   199
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   200
    // Aggiungo il file
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   201
    if (!compressFile(zip,file,QFileInfo(file).fileName())) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   202
        delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   203
        QFile::remove(fileCompressed);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   204
        return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   205
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   206
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   207
    // Chiudo il file zip
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   208
    zip->close();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   209
    if(zip->getZipError()!=0) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   210
        delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   211
        QFile::remove(fileCompressed);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   212
        return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   213
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   214
    delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   215
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   216
    return true;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   217
}
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   218
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   219
/**OK
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   220
 * Comprime i file specificati in files nel file fileCompressed.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   221
 * Se la funzione fallisce restituisce false e cancella il file che si è tentato
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   222
 * di creare.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   223
 *
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   224
 * La funzione fallisce se:
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   225
 * * non si riesce ad aprire l'oggetto zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   226
 * * la compressione di un file fallisce;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   227
 * * non si riesce a chiudere l'oggetto zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   228
 */
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   229
bool JlCompress::compressFiles(QString fileCompressed, QStringList files) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   230
    // Creo lo zip
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   231
    QuaZip* zip  = new QuaZip(QFileInfo(fileCompressed).absoluteFilePath());
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   232
    QDir().mkpath(QFileInfo(fileCompressed).absolutePath());
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   233
    if(!zip->open(QuaZip::mdCreate)) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   234
        delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   235
        QFile::remove(fileCompressed);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   236
        return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   237
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   238
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   239
    // Comprimo i file
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   240
    QFileInfo info;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   241
    foreach (QString file, files) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   242
        info.setFile(file);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   243
        if (!info.exists() || !compressFile(zip,file,info.fileName())) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   244
            delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   245
            QFile::remove(fileCompressed);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   246
            return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   247
        }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   248
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   249
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   250
    // Chiudo il file zip
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   251
    zip->close();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   252
    if(zip->getZipError()!=0) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   253
        delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   254
        QFile::remove(fileCompressed);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   255
        return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   256
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   257
    delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   258
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   259
    return true;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   260
}
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   261
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   262
/**OK
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   263
 * Comprime la cartella dir nel file fileCompressed, se recursive è true allora
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   264
 * comprime anche le sotto cartelle.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   265
 * Se la funzione fallisce restituisce false e cancella il file che si è tentato
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   266
 * di creare.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   267
 *
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   268
 * La funzione fallisce se:
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   269
 * * non si riesce ad aprire l'oggetto zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   270
 * * la compressione di un file fallisce;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   271
 * * non si riesce a chiudere l'oggetto zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   272
 */
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   273
bool JlCompress::compressDir(QString fileCompressed, QString dir, bool recursive) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   274
    // Creo lo zip
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   275
    QuaZip* zip  = new QuaZip(QFileInfo(fileCompressed).absoluteFilePath());
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   276
    QDir().mkpath(QFileInfo(fileCompressed).absolutePath());
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   277
    if(!zip->open(QuaZip::mdCreate)) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   278
        delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   279
        QFile::remove(fileCompressed);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   280
        return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   281
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   282
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   283
    // Aggiungo i file e le sotto cartelle
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   284
    if (!compressSubDir(zip,dir,dir,recursive)<0) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   285
        delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   286
        QFile::remove(fileCompressed);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   287
        return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   288
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   289
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   290
    // Chiudo il file zip
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   291
    zip->close();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   292
    if(zip->getZipError()!=0) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   293
        delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   294
        QFile::remove(fileCompressed);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   295
        return false;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   296
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   297
    delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   298
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   299
    return true;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   300
}
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   301
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   302
////////////////////////////////////////////////////////////////////////////////
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   303
////////////////////////////////////////////////////////////////////////////////
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   304
/**OK
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   305
 * Estrae il file fileName, contenuto nel file fileCompressed, con il nome fileDest.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   306
 * Se fileDest = "" allora il file viene estratto con lo stesso nome con cui è
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   307
 * stato compresso.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   308
 * Se la funzione fallisce cancella il file che si è tentato di estrarre.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   309
 * Restituisce il nome assoluto del file estratto.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   310
 *
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   311
 * La funzione fallisce se:
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   312
 * * non si riesce ad aprire l'oggetto zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   313
 * * l'estrazione del file fallisce;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   314
 * * non si riesce a chiudere l'oggetto zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   315
 */
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   316
QString JlCompress::extractFile(QString fileCompressed, QString fileName, QString fileDest) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   317
    // Apro lo zip
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   318
    QuaZip* zip  = new QuaZip(QFileInfo(fileCompressed).absoluteFilePath());
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   319
    if(!zip->open(QuaZip::mdUnzip)) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   320
        delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   321
        return QString();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   322
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   323
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   324
    // Estraggo il file
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   325
    if (fileDest.isEmpty()) fileDest = fileName;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   326
    if (!extractFile(zip,fileName,fileDest)) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   327
        delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   328
        return QString();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   329
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   330
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   331
    // Chiudo il file zip
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   332
    zip->close();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   333
    if(zip->getZipError()!=0) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   334
        removeFile(QStringList(fileDest));
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   335
        return QString();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   336
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   337
    delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   338
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   339
    return QFileInfo(fileDest).absoluteFilePath();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   340
}
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   341
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   342
/**OK
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   343
 * Estrae i file specificati in files, contenuti nel file fileCompressed, nella
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   344
 * cartella dir. La struttura a cartelle del file compresso viene rispettata.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   345
 * Se dir = "" allora il file viene estratto nella cartella corrente.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   346
 * Se la funzione fallisce cancella i file che si è tentato di estrarre.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   347
 * Restituisce i nomi assoluti dei file estratti.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   348
 *
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   349
 * La funzione fallisce se:
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   350
 * * non si riesce ad aprire l'oggetto zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   351
 * * l'estrazione di un file fallisce;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   352
 * * non si riesce a chiudere l'oggetto zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   353
 */
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   354
QStringList JlCompress::extractFiles(QString fileCompressed, QStringList files, QString dir) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   355
    // Creo lo zip
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   356
    QuaZip* zip  = new QuaZip(QFileInfo(fileCompressed).absoluteFilePath());
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   357
    if(!zip->open(QuaZip::mdUnzip)) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   358
        delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   359
        return QStringList();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   360
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   361
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   362
    // Estraggo i file
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   363
    for (int i=0; i<files.count(); i++) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   364
        if (!extractFile(zip, files.at(i), QDir(dir).absoluteFilePath(files.at(i)))) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   365
            delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   366
            removeFile(files);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   367
            return QStringList();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   368
        }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   369
        files[i] = QDir(dir).absoluteFilePath(files.at(i));
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   370
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   371
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   372
    // Chiudo il file zip
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   373
    zip->close();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   374
    if(zip->getZipError()!=0) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   375
        delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   376
        removeFile(files);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   377
        return QStringList();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   378
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   379
    delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   380
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   381
    return files;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   382
}
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   383
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   384
/**OK
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   385
 * Estrae il file fileCompressed nella cartella dir.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   386
 * Se dir = "" allora il file viene estratto nella cartella corrente.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   387
 * Se la funzione fallisce cancella i file che si è tentato di estrarre.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   388
 * Restituisce i nomi assoluti dei file estratti.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   389
 *
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   390
 * La funzione fallisce se:
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   391
 * * non si riesce ad aprire l'oggetto zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   392
 * * la compressione di un file fallisce;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   393
 * * non si riesce a chiudere l'oggetto zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   394
 */
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   395
QStringList JlCompress::extractDir(QString fileCompressed, QString dir) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   396
    // Apro lo zip
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   397
    QuaZip* zip = new QuaZip(QFileInfo(fileCompressed).absoluteFilePath());
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   398
    if(!zip->open(QuaZip::mdUnzip)) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   399
        delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   400
        return QStringList();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   401
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   402
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   403
    // Estraggo i file
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   404
    QStringList lst = getFileList(fileCompressed);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   405
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   406
    QDir directory(dir);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   407
    for (int i=0; i<lst.count(); i++) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   408
        QString absFilePath = directory.absoluteFilePath(lst.at(i));
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   409
        if (!extractFile(zip, lst.at(i), absFilePath)) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   410
            delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   411
            removeFile(lst);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   412
            return QStringList();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   413
        }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   414
        lst[i] = absFilePath;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   415
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   416
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   417
    // Chiudo il file zip
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   418
    zip->close();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   419
    if(zip->getZipError()!=0) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   420
        delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   421
        removeFile(lst);
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   422
        return QStringList();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   423
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   424
    delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   425
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   426
    return lst;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   427
}
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   428
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   429
/**OK
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   430
 * Restituisce la lista dei file resenti nel file compresso fileCompressed.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   431
 * Se la funzione fallisce, restituisce un elenco vuoto.
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   432
 *
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   433
 * La funzione fallisce se:
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   434
 * * non si riesce ad aprire l'oggetto zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   435
 * * la richiesta di informazioni di un file fallisce;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   436
 * * non si riesce a chiudere l'oggetto zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   437
 */
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   438
QStringList JlCompress::getFileList(QString fileCompressed) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   439
    // Apro lo zip
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   440
    QuaZip* zip = new QuaZip(QFileInfo(fileCompressed).absoluteFilePath());
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   441
    if(!zip->open(QuaZip::mdUnzip)) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   442
        delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   443
        return QStringList();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   444
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   445
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   446
    // Estraggo i nomi dei file
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   447
    QStringList lst;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   448
    QuaZipFileInfo info;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   449
    for(bool more=zip->goToFirstFile(); more; more=zip->goToNextFile()) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   450
      if(!zip->getCurrentFileInfo(&info)) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   451
          delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   452
          return QStringList();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   453
      }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   454
      lst << info.name;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   455
      //info.name.toLocal8Bit().constData()
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   456
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   457
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   458
    // Chiudo il file zip
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   459
    zip->close();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   460
    if(zip->getZipError()!=0) {
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   461
        delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   462
        return QStringList();
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   463
    }
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   464
    delete zip;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   465
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   466
    return lst;
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   467
}
ea95ee97c805 Add QuaZIP library to build system
unc0rr
parents:
diff changeset
   468