PDA

View Full Version : Zip function for Qt4



devilj
14th July 2007, 18:43
I am looking for a zip function for Qt4. My real need isn't file compression per se, but to create a way to archive multiple (3) files into a single downloadable file. The way I have been doing this is using a .zip archive (seems more multi-platform). I really don't need to unzip any files. Does anyone know of a way to do this and better yet in a multi-platform way?

Example:

I have 3 files:

L010001.jpg
L010001-Mask.jpg
HowTo.pdf

And I would like to combine them into one downloadable file called "L010001.zip"


If anyone knows a solution, it would be much appreciated.

marcel
14th July 2007, 19:10
Well, if you don't need compression I suggest doing the following.
1. Read about the ZIP file format(see links below ).
2. Once you get familiar with it, establish your workflow. Most likely your archive will have the structure(when no compression is done:
ZIP header
File 1
...
File n

This is just a draft, since I don't know the real format of a ZIP file, but it should be that easy.
So, basically, what you have to do is compute a header for your input files, add it to the out file and then start adding the input files one by one.

Regarding ZIP format:
http://math.ucr.edu/~mike/zipattacks.pdf
http://www.info-zip.org/pub/infozip/doc/
http://en.wikipedia.org/wiki/ZIP_(file_format)
But mostly this:
http://www.pkware.com/documents/casestudies/APPNOTE.TXT
http://www.pkware.com/index.php?option=com_content&task=view&id=64&Itemid=107


Regards

patrik08
14th July 2007, 19:41
the simple way is http://quazip.sourceforge.net/

read all file in and load to quazip qt4...

i think this sample

https://qt-webdav.svn.sourceforge.net/svnroot/qt-webdav/console_zip_unzip/

go rekursive folder....

devilj
14th July 2007, 19:56
Patrik08,
Am I wrong or is QuaZip at this time only an unzipping function or has Sergey developed the zip side as well? Some of the discussions I have read as well as some of the usage pages made it sound as if it was only for unzipping and the ability to use data stream.

marcel
14th July 2007, 20:02
Well, if you are willing to use an entire library then why not just use zlib?
I thought you didn't need any compression, therefore no library to offer complete support for zipping.

Regards

patrik08
14th July 2007, 23:44
i call this zip and unzip ....
this is my work .... not the the quazip...
https://qt-webdav.svn.sourceforge.net/svnroot/qt-webdav/console_zip_unzip/






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;
}
}

devilj
15th July 2007, 04:49
Marcel,

This may sound silly but can I get a .zip file from zlib or am I only going to get a .gz gunzip file? My end users I am afraid will not know what to do with a .gz extension and would like to use the more universally known .zip extension. Is this possible?

Thanks.

patrik08
15th July 2007, 14:37
Marcel,

This may sound silly but can I get a .zip file from zlib or am I only going to get a .gz gunzip file? My end users I am afraid will not know what to do with a .gz extension and would like to use the more universally known .zip extension. Is this possible?

Thanks.


you are deaf?, if i say the fact that you can zip and to unzip and can gz open ond close...

my client only having a normal zip programm ... not http://www.7-zip.org/ that handle all..

read https://qt-webdav.svn.sourceforge.net/svnroot/qt-webdav/console_zip_unzip/

My new CMS

http://ciz.ch/it/software/content_management_system/cms___qt_pulitzer_pc_14069775.html

shoot http://ciz.ch/data/2007/06/12/14069775/cmsqt.jpg

handle all page as zip ... inside is the page on all language image pdf attachment ,excel and entire dir as attachment...
And zip is upload on server (PHP open zip!) and open to correct path at end go on back-up to mysql to new edit... by qt4...

End user export page as back-up as a normal zip.. :) to import on other page or place..