File: qtzipper.cpp

package info (click to toggle)
beid 3.5.2.dfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 147,240 kB
  • ctags: 34,507
  • sloc: cpp: 149,944; ansic: 41,577; java: 8,927; cs: 6,528; sh: 2,426; perl: 1,866; xml: 805; python: 463; makefile: 263; lex: 92
file content (198 lines) | stat: -rw-r--r-- 6,166 bytes parent folder | download
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* ****************************************************************************

 * eID Middleware Project.
 * Copyright (C) 2008-2009 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see
 * http://www.gnu.org/licenses/.

**************************************************************************** */
#include <QtGlobal>
#include <QDirIterator>
#include <QDir>
#include <QCryptographicHash>
#include "zrclib.h"
#include "qtzipper.h"

QByteArray QtZipper::getFileBytes (QString filename) 
{
    QFile qf(filename);
	qf.open (QIODevice::ReadOnly);
    QByteArray ba;
	QDataStream in(&qf);
	ba = qf.readAll();
	qf.close();
	return ba;
}

void QtZipper::getHash(QByteArray const& ba, std::string& hash) 
{
	QString qstrHash;
	getHash(ba, qstrHash);
	hash = qstrHash.toStdString();
}
void QtZipper::getHash(QByteArray const& ba, QString& hash) 
{
	QByteArray theHash = QCryptographicHash::hash (ba,QCryptographicHash::Sha1);
	hash = theHash.toHex();
}

void QtZipper::saveByteArrayToFile(QByteArray& ba, QFile& outfile, QString& hash ) 
{
	outfile.open(QIODevice::WriteOnly);
	QDataStream dsout(&outfile);
	dsout.writeRawData(ba.data(),ba.length());
	outfile.close();
	getHash(ba,hash);
}

void QtZipper::zipFolder(QString const& folderPath, QString const& ArchiveFileName)
{
	std::cout << "[INFO] Archive file: " << ArchiveFileName.toStdString().c_str() << std::endl;

	ZrcArchive zarch(folderPath.toStdString() + "/" + ArchiveFileName.toStdString());
	zarch.CreateArchive();

	QFileInfo	 archive(ArchiveFileName);
	QFile		 tmpZip(folderPath+QString("/tmpzip.zip"));
	QDir		 qdd(folderPath,"",QDir::Name,QDir::AllEntries);
	QStringList	 filter;
	filter << "*.cat" << "*.inf" << "*.sys" << "*.dll" << "*.ini" << "*.cpl" << "*.BMP" << "*.txt";
	qdd.setNameFilters(filter); 

	QDirIterator it(qdd, QDirIterator::Subdirectories);

	//-------------------------------------------------
	// run over all items in the directory
	//-------------------------------------------------
	while (it.hasNext()) 
	{
		it.next();

		if (   it.fileInfo().isFile()				// must be a file
			&& it.fileName() != archive.fileName()	// omit the own archive file
		    ) 
		{
			QString x = it.filePath();			// get the file name

			std::cout << "[INFO] Processing file: " << x.toStdString().c_str() << std::endl;

			x.remove(0,folderPath.size()+1);
			
			try 
			{
				QByteArray baCompressed = qCompress (getFileBytes(it.filePath()));
				QFile	   tmpZip(folderPath+QString("/tmpzip.zip"));
				QString	   hash;
				saveByteArrayToFile(baCompressed,tmpZip,hash);

				QStringList qsl   = x.split("/");
				QString		qsos  = qsl[0];
				QString		qsrdr = qsl[1];
				string		fn;

				x  = x.mid((qsos+QString("/")+qsrdr+QString("/")).size());
				fn = x.toStdString();

				ifstream ifs;
				ifs.open(tmpZip.fileName().toStdString().c_str(), ios::binary);
				if (ifs.good()) 
				{
					// take size of file
					ifs.seekg(0, ios::end);
					long size = ifs.tellg();
					ifs.seekg(0, ios::beg);
					// read the content
					auto_vec<char> data(new char[size]);
					ifs.read(data.get(), size);
					ifs.close();
					// make a file header and add to archive
					string reader = qsrdr.toStdString();
					string os = qsos.toStdString();

					ZrcFileHeader zfh(reader, os, fn, size);
					zarch.AddFileHeader(&zfh);
					zarch.AddFileData(data.get());
				}
				ifs.close();
				tmpZip.remove();
			}
			catch (...) 
			{
			}
		}
	}
}

/*
void EZZipper::on_pbTestUnzip_clicked()
{
	ui.textEdit->clear();
	QFileDialog::Options options = QFileDialog::DontResolveSymlinks ;
	QString zipFile = QFileDialog::getOpenFileName(this,tr("Kies zipfile"),ui.leFolder->text(),"*.zrc",0,options);
	if (!zipFile.isEmpty()) {

		QString unzipFolder = ui.leFolder->text()+QString("/unziptestfolder/");
		string tmpUnzipfile = unzipFolder.toStdString()+"tmpUnzipfile";
		QDir qd(ui.leFolder->text());

//werktniet		qd.rmpath(unzipFolder);
		qd.mkpath(unzipFolder);


        // open weer maar voor input
		ZrcArchive zarch(zipFile.toStdString());
        zarch.OpenArchive();
        ZrcFileHeader *hdr = NULL;

        // lees de file headers
        while ((hdr = zarch.ReadFileHeader()) != NULL) {

         //   if (hdr->get_Filename().compare("f2") == 0) {
                // lees de file data van "f2"
                char* data = zarch.ReadFileData(hdr->get_Filesize());
                // en terug wegschrijven ter controle
                ofstream ofs;
                string xfn = hdr->get_Filename();

                

				//ofs.open(xfn.c_str(), ios::binary);
                ofs.open(tmpUnzipfile.c_str(), ios::binary);
                ofs.write(data, hdr->get_Filesize());
                ofs.close();

				QString extractToFileName = unzipFolder+QString(hdr->get_OS().c_str())+QString("/")+QString(hdr->get_Reader().c_str())+QString("/")+   QString(xfn.c_str());
				QString tmpFolder = extractToFileName.mid(0,extractToFileName.lastIndexOf("/"));

			    qd.mkpath(tmpFolder);

				QByteArray ba = qUncompress(getFileBytes(QString(tmpUnzipfile.c_str())));
				saveByteArrayToFile(ba,extractToFileName);
				
				appendString("Extracting "+extractToFileName.toStdString()+".",QFont::Normal);

                // schoonmaak
                delete[] data;
            // schoonmaak
            delete hdr;

        }
        // sluit archive
        zarch.CloseArchive();
	    QFile::remove(QString(tmpUnzipfile.c_str()));
	    appendString("DONE",QFont::Bold);
	}
     
}
*/