1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#include "tzipper.h"
#include "utils.h"
//including a gunzip snippet from OSDaB
//by Angius Fabrizio
bool CZipper::zip_directory (const QString &archpath, const QString &dir2pack)
{
QString archname = qstring_get_last_after (dir2pack, "/");
QString zipname (archpath); //zip name has a full path ending with .zip
zipname.append ("/").append (archname).append (".zip");
QuaZip zip (zipname);
if (! zip.open (QuaZip::mdCreate))
return false;
QDir dir (dir2pack);
QFileInfoList files = dir.entryInfoList();
QFile inFile;
QuaZipFile outFile(&zip);
foreach (QFileInfo file, files)
{
if (! file.isFile())
continue;
inFile.setFileName (file.absoluteFilePath());
if (! inFile.open (QIODevice::ReadOnly))
return false;
QString outfname (archname);
outfname.append ("/").append (file.fileName());
if (! outFile.open (QIODevice::WriteOnly, QuaZipNewInfo (outfname, inFile.fileName())))
return false;
QByteArray ba = inFile.readAll();
outFile.write (ba);
outFile.close();
if (outFile.getZipError() != UNZ_OK)
return false;
inFile.close();
emit new_iteration (file);
}
zip.close();
if (zip.getZipError() != 0)
return false;
return true;
}
bool CZipper::read_as_utf8 (const QString &archname, const QString &fname)
{
QuaZip zip (archname);
if (! zip.open (QuaZip::mdUnzip))
return false;
zip.setFileNameCodec ("IBM866");
zip.setCurrentFile (fname);
if (! zip.hasCurrentFile())
return false;
QuaZipFileInfo info;
if (! zip.getCurrentFileInfo (&info))
return false;
QuaZipFile file (&zip);
if (! file.open (QIODevice::ReadOnly))
return false;
QByteArray ba = file.readAll();
string_data = QString::fromUtf8 (ba.data());
file.close();
zip.close();
return true;
}
//a snippet from OSDaB
//by Angius Fabrizio
QByteArray gzip_deflateFile (const QString &fileName)
{
gzFile gzDoc;
char buff[4097];
int i;
QByteArray fname = fileName.toLatin1();
gzDoc = gzopen(fname.constData(), "rb");
if (! gzDoc)
return QByteArray();
QByteArray data;
while ((i = gzread (gzDoc, &buff, 4096)) > 0)
{
buff[i] = '\0';
data.append (buff);
}
gzclose(gzDoc);
return data;
}
bool CZipper::pack_prepared()
{
if (archive_fullpath.isEmpty())
return false;
QString zipname (archive_fullpath);
QuaZip zip (zipname);
if (! zip.open (QuaZip::mdCreate))
return false;
QFile inFile;
QuaZipFile outFile (&zip);
foreach (QString fi, files_list)
{
QFileInfo file (fi);
if (! file.isFile())
continue;
inFile.setFileName (file.absoluteFilePath());
if (! inFile.open (QIODevice::ReadOnly))
return false;
QString outfname (archive_name);
outfname.append ("/").append (file.fileName());
if (! outFile.open (QIODevice::WriteOnly, QuaZipNewInfo (outfname, inFile.fileName())))
return false;
QByteArray ba = inFile.readAll();
outFile.write (ba);
outFile.close();
if (outFile.getZipError() != UNZ_OK)
return false;
inFile.close();
emit new_iteration (file);
}
zip.close();
if (zip.getZipError() != 0)
return false;
return true;
}
|