{
Q_OBJECT
public:
PackZip()
{
startnow
= QDir::currentPath();
}
~PackZip()
{
QDir::setCurrent(startnow
);
}
{
startdir = packfolder; /* grab all this file */
zipdir = zipoutfolder; /* save zip to this dir */
startnow
= QDir::currentPath();
Init();
}
private:
void Init()
{
canmake = false;
if (!zipdir.contains("/")) {
ResetDir();
return;
}
if (!zipdir.endsWith("/")) {
ResetDir();
return;
}
if (!startdir.contains("/")) {
ResetDir();
return;
}
if (!startdir.endsWith("/")) {
ResetDir();
return;
}
canmake = true;
char c;
char* suk;
qreal cento;
qreal percentuale;
selfname = dirname + ".zip";
zipfile = zipdir;
zipfile.append(dirname);
zipfile.append(".zip");
if (zcheck.exists()) {
zcheck.remove();
}
dirandfile.clear();
IndexDir(startdir);
if (dirandfile.size() > 0) {
QuaZip zip(zipfile);
if(!zip.open(QuaZip::mdCreate)) {
ResetDir();
return;
}
QuaZipFile outFile(&zip);
int totalf = dirandfile.size();
///////qDebug() << "### totalfile " << dirandfile.size();
for (int i = 0; i < dirandfile.size(); ++i) {
QString filepak
= dirandfile.
at(i
);
const QString goname
= filepak.
replace(startdir,
"");
inFile.setFileName(abse);
ResetDir();
return;
}
if(!outFile.
open(QIODevice::WriteOnly, QuaZipNewInfo
(goname, inFile.
fileName()))) { ResetDir();
return;
}
while(inFile.getChar(&c)&&outFile.putChar(c));
if(outFile.getZipError()!=UNZ_OK) {
ResetDir();
return;
}
outFile.close();
if(outFile.getZipError()!=UNZ_OK) {
ResetDir();
return;
}
inFile.close();
cento = 100;
percentuale = i*cento/totalf;
int status = percentuale;
emit Status(status);
fileqinfo.append(" ");
fileqinfo.append(BiteorMega(inFile.size()));
suk = fe.data();
////////// cout << "Status. " << percentuale << "% " << suk << "\n";
/////fflush ( stdin );
////////qDebug() << "### status " << status;
//////////qDebug() << "### d " << goname;
}
zip.close();
if(zip.getZipError()!=0) {
QDir::setCurrent(startnow
);
}
fr.append(" ");
fr.append(BiteorMega(re.size()));
suk = fe.data();
///////////////// cout << "End file " << suk << "\n";
///////qDebug() << "### status 100";
emit Status(100);
emit ZipEnd(zipfile);
return;
/////////////qDebug() << "### emitend " << zipfile;
}
emit ZipError();
}
void ResetDir()
{
emit ZipError();
QDir::setCurrent(startnow
);
}
{
if (dir.exists())
{
const QFileInfoList list = dir.entryInfoList();
for (int l = 0; l < list.size(); l++)
{
fi = list.at(l);
if (fi.isDir() && fi.fileName() != "." && fi.fileName() != "..") {
IndexDir(fi.absoluteFilePath());
} else if (fi.isFile() && fi.fileName() != selfname ) {
dirandfile.append(fi.absoluteFilePath());
}
}
}
}
bool canmake;
signals:
void ZipError();
void Status(int o );
public slots:
};
extern inline void gz_compress( FILE *in , gzFile out )
{
local char buf[BUFLEN];
int len;
int err;
for (;;) {
len = (int)fread(buf, 1, sizeof(buf), in);
if (ferror(in)) {
perror("fread");
exit(1);
}
if (len == 0) break;
if (gzwrite(out, buf, (unsigned)len) != len) {
return;
}
}
fclose(in);
if (gzclose(out) != Z_OK) {
return;
}
}
class PackZip : public QObject
{
Q_OBJECT
public:
PackZip()
{
startnow = QDir::currentPath();
}
~PackZip()
{
QDir::setCurrent(startnow);
}
void SetJob( const QString packfolder , const QString zipoutfolder )
{
startdir = packfolder; /* grab all this file */
zipdir = zipoutfolder; /* save zip to this dir */
startnow = QDir::currentPath();
Init();
}
private:
void Init()
{
canmake = false;
if (!zipdir.contains("/")) {
ResetDir();
return;
}
if (!zipdir.endsWith("/")) {
ResetDir();
return;
}
if (!startdir.contains("/")) {
ResetDir();
return;
}
if (!startdir.endsWith("/")) {
ResetDir();
return;
}
canmake = true;
QDir dir(startdir);
char c;
char* suk;
qreal cento;
qreal percentuale;
QString dirname = dir.dirName();
selfname = dirname + ".zip";
zipfile = zipdir;
zipfile.append(dirname);
zipfile.append(".zip");
QFile zcheck(zipfile);
if (zcheck.exists()) {
zcheck.remove();
}
dirandfile.clear();
IndexDir(startdir);
if (dirandfile.size() > 0) {
QuaZip zip(zipfile);
if(!zip.open(QuaZip::mdCreate)) {
ResetDir();
return;
}
QuaZipFile outFile(&zip);
int totalf = dirandfile.size();
///////qDebug() << "### totalfile " << dirandfile.size();
for (int i = 0; i < dirandfile.size(); ++i) {
QString filepak = dirandfile.at(i);
const QString abse = filepak;
const QString goname = filepak.replace(startdir,"");
QFile inFile(abse);
inFile.setFileName(abse);
if(!inFile.open(QIODevice::ReadOnly)) {
ResetDir();
return;
}
if(!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(goname, inFile.fileName()))) {
ResetDir();
return;
}
while(inFile.getChar(&c)&&outFile.putChar(c));
if(outFile.getZipError()!=UNZ_OK) {
ResetDir();
return;
}
outFile.close();
if(outFile.getZipError()!=UNZ_OK) {
ResetDir();
return;
}
inFile.close();
cento = 100;
percentuale = i*cento/totalf;
int status = percentuale;
emit Status(status);
QString fileqinfo = goname;
fileqinfo.append(" ");
fileqinfo.append(BiteorMega(inFile.size()));
QByteArray fe = fileqinfo.toAscii();
suk = fe.data();
////////// cout << "Status. " << percentuale << "% " << suk << "\n";
/////fflush ( stdin );
////////qDebug() << "### status " << status;
//////////qDebug() << "### d " << goname;
}
zip.close();
if(zip.getZipError()!=0) {
QDir::setCurrent(startnow);
}
QFile re(zipfile);
QString fr = re.fileName();
fr.append(" ");
fr.append(BiteorMega(re.size()));
QByteArray fe = fr.toAscii();
suk = fe.data();
///////////////// cout << "End file " << suk << "\n";
///////qDebug() << "### status 100";
emit Status(100);
emit ZipEnd(zipfile);
return;
/////////////qDebug() << "### emitend " << zipfile;
}
emit ZipError();
}
void ResetDir()
{
emit ZipError();
QDir::setCurrent(startnow);
}
void IndexDir(const QString d)
{
QDir dir(d);
if (dir.exists())
{
const QFileInfoList list = dir.entryInfoList();
QFileInfo fi;
for (int l = 0; l < list.size(); l++)
{
fi = list.at(l);
if (fi.isDir() && fi.fileName() != "." && fi.fileName() != "..") {
IndexDir(fi.absoluteFilePath());
} else if (fi.isFile() && fi.fileName() != selfname ) {
dirandfile.append(fi.absoluteFilePath());
}
}
}
}
QString startnow;
QStringList dirandfile;
QString startdir;
QString zipdir;
QString zipfile;
QString selfname;
bool canmake;
signals:
void ZipEnd( QString fileresult );
void ZipError();
void Status(int o );
public slots:
};
extern inline void gz_compress( FILE *in , gzFile out )
{
local char buf[BUFLEN];
int len;
int err;
for (;;) {
len = (int)fread(buf, 1, sizeof(buf), in);
if (ferror(in)) {
perror("fread");
exit(1);
}
if (len == 0) break;
if (gzwrite(out, buf, (unsigned)len) != len) {
return;
}
}
fclose(in);
if (gzclose(out) != Z_OK) {
return;
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks