Results 1 to 8 of 8

Thread: Zip function for Qt4

  1. #1
    Join Date
    Jul 2007
    Location
    Scottsdale, AZ USA
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Zip function for Qt4

    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.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Zip function for Qt4

    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?opti...=64&Itemid=107


    Regards

  3. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Zip function for Qt4

    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.ne...ole_zip_unzip/

    go rekursive folder....

  4. #4
    Join Date
    Jul 2007
    Location
    Scottsdale, AZ USA
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Zip function for Qt4

    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.

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Zip function for Qt4

    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

  6. #6
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Zip function for Qt4

    i call this zip and unzip ....
    this is my work .... not the the quazip...
    https://qt-webdav.svn.sourceforge.ne...ole_zip_unzip/

    Qt Code:
    1. class PackZip : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. PackZip()
    7. {
    8. startnow = QDir::currentPath();
    9. }
    10. ~PackZip()
    11. {
    12. QDir::setCurrent(startnow);
    13. }
    14. void SetJob( const QString packfolder , const QString zipoutfolder )
    15. {
    16.  
    17. startdir = packfolder; /* grab all this file */
    18. zipdir = zipoutfolder; /* save zip to this dir */
    19. startnow = QDir::currentPath();
    20. Init();
    21. }
    22.  
    23. private:
    24. void Init()
    25. {
    26. canmake = false;
    27. if (!zipdir.contains("/")) {
    28. ResetDir();
    29. return;
    30. }
    31. if (!zipdir.endsWith("/")) {
    32. ResetDir();
    33. return;
    34. }
    35. if (!startdir.contains("/")) {
    36. ResetDir();
    37. return;
    38. }
    39. if (!startdir.endsWith("/")) {
    40. ResetDir();
    41. return;
    42. }
    43. canmake = true;
    44. QDir dir(startdir);
    45. char c;
    46. char* suk;
    47. qreal cento;
    48. qreal percentuale;
    49. QString dirname = dir.dirName();
    50. selfname = dirname + ".zip";
    51. zipfile = zipdir;
    52. zipfile.append(dirname);
    53. zipfile.append(".zip");
    54.  
    55. QFile zcheck(zipfile);
    56. if (zcheck.exists()) {
    57. zcheck.remove();
    58. }
    59.  
    60. dirandfile.clear();
    61. IndexDir(startdir);
    62. if (dirandfile.size() > 0) {
    63.  
    64. QuaZip zip(zipfile);
    65. if(!zip.open(QuaZip::mdCreate)) {
    66. ResetDir();
    67. return;
    68. }
    69.  
    70. QuaZipFile outFile(&zip);
    71.  
    72. int totalf = dirandfile.size();
    73.  
    74. ///////qDebug() << "### totalfile " << dirandfile.size();
    75. for (int i = 0; i < dirandfile.size(); ++i) {
    76. QString filepak = dirandfile.at(i);
    77. const QString abse = filepak;
    78. const QString goname = filepak.replace(startdir,"");
    79.  
    80. QFile inFile(abse);
    81. inFile.setFileName(abse);
    82.  
    83. if(!inFile.open(QIODevice::ReadOnly)) {
    84. ResetDir();
    85. return;
    86. }
    87.  
    88. if(!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(goname, inFile.fileName()))) {
    89. ResetDir();
    90. return;
    91. }
    92.  
    93. while(inFile.getChar(&c)&&outFile.putChar(c));
    94. if(outFile.getZipError()!=UNZ_OK) {
    95. ResetDir();
    96. return;
    97. }
    98.  
    99. outFile.close();
    100. if(outFile.getZipError()!=UNZ_OK) {
    101. ResetDir();
    102. return;
    103. }
    104. inFile.close();
    105. cento = 100;
    106. percentuale = i*cento/totalf;
    107. int status = percentuale;
    108. emit Status(status);
    109.  
    110.  
    111. QString fileqinfo = goname;
    112. fileqinfo.append(" ");
    113. fileqinfo.append(BiteorMega(inFile.size()));
    114. QByteArray fe = fileqinfo.toAscii();
    115. suk = fe.data();
    116.  
    117. ////////// cout << "Status. " << percentuale << "% " << suk << "\n";
    118. /////fflush ( stdin );
    119.  
    120. ////////qDebug() << "### status " << status;
    121. //////////qDebug() << "### d " << goname;
    122. }
    123.  
    124. zip.close();
    125. if(zip.getZipError()!=0) {
    126. QDir::setCurrent(startnow);
    127. }
    128.  
    129.  
    130.  
    131.  
    132. QFile re(zipfile);
    133.  
    134. QString fr = re.fileName();
    135. fr.append(" ");
    136. fr.append(BiteorMega(re.size()));
    137.  
    138. QByteArray fe = fr.toAscii();
    139. suk = fe.data();
    140.  
    141. ///////////////// cout << "End file " << suk << "\n";
    142. ///////qDebug() << "### status 100";
    143. emit Status(100);
    144. emit ZipEnd(zipfile);
    145. return;
    146. /////////////qDebug() << "### emitend " << zipfile;
    147. }
    148. emit ZipError();
    149. }
    150. void ResetDir()
    151. {
    152. emit ZipError();
    153. QDir::setCurrent(startnow);
    154. }
    155. void IndexDir(const QString d)
    156. {
    157. QDir dir(d);
    158. if (dir.exists())
    159. {
    160. const QFileInfoList list = dir.entryInfoList();
    161. for (int l = 0; l < list.size(); l++)
    162. {
    163. fi = list.at(l);
    164. if (fi.isDir() && fi.fileName() != "." && fi.fileName() != "..") {
    165. IndexDir(fi.absoluteFilePath());
    166. } else if (fi.isFile() && fi.fileName() != selfname ) {
    167. dirandfile.append(fi.absoluteFilePath());
    168. }
    169.  
    170. }
    171. }
    172. }
    173. QString startnow;
    174. QStringList dirandfile;
    175. QString startdir;
    176. QString zipdir;
    177. QString zipfile;
    178. QString selfname;
    179. bool canmake;
    180. signals:
    181. void ZipEnd( QString fileresult );
    182. void ZipError();
    183. void Status(int o );
    184. public slots:
    185.  
    186. };
    187.  
    188.  
    189.  
    190.  
    191.  
    192. extern inline void gz_compress( FILE *in , gzFile out )
    193. {
    194. local char buf[BUFLEN];
    195. int len;
    196. int err;
    197.  
    198. for (;;) {
    199. len = (int)fread(buf, 1, sizeof(buf), in);
    200. if (ferror(in)) {
    201. perror("fread");
    202. exit(1);
    203. }
    204. if (len == 0) break;
    205.  
    206. if (gzwrite(out, buf, (unsigned)len) != len) {
    207. return;
    208. }
    209. }
    210. fclose(in);
    211. if (gzclose(out) != Z_OK) {
    212. return;
    213. }
    214. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jul 2007
    Location
    Scottsdale, AZ USA
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Zip function for Qt4

    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.

  8. #8
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Zip function for Qt4

    Quote Originally Posted by devilj View Post
    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.ne...ole_zip_unzip/

    My new CMS

    http://ciz.ch/it/software/content_ma..._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..

Similar Threads

  1. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  2. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 22:04
  3. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  4. Qt 4.1.4 plugin QPSQL
    By jcr in forum Installation and Deployment
    Replies: 4
    Last Post: 22nd June 2006, 22:55
  5. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 08:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.