• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • sources
  • kde-4.12
  • kdelibs
  • kdecore
  • io
kzip.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000 David Faure <faure@kde.org>
3  Copyright (C) 2002 Holger Schroeder <holger-kde@holgis.net>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "kzip.h"
21 #include "kfilterdev.h"
22 #include "klimitediodevice_p.h"
23 #include <kdebug.h>
24 
25 #include <QtCore/QHash>
26 #include <QtCore/QByteArray>
27 #include <QtCore/QFile>
28 #include <QtCore/QDir>
29 #include <QtCore/QDate>
30 #include <QtCore/QList>
31 
32 #include <zlib.h>
33 #include <time.h>
34 #include <string.h>
35 
36 const int max_path_len = 4095; // maximum number of character a path may contain
37 
38 static void transformToMsDos(const QDateTime& dt, char* buffer)
39 {
40  if ( dt.isValid() )
41  {
42  const quint16 time =
43  ( dt.time().hour() << 11 ) // 5 bit hour
44  | ( dt.time().minute() << 5 ) // 6 bit minute
45  | ( dt.time().second() >> 1 ); // 5 bit double seconds
46 
47  buffer[0] = char(time);
48  buffer[1] = char(time >> 8);
49 
50  const quint16 date =
51  ( ( dt.date().year() - 1980 ) << 9 ) // 7 bit year 1980-based
52  | ( dt.date().month() << 5 ) // 4 bit month
53  | ( dt.date().day() ); // 5 bit day
54 
55  buffer[2] = char(date);
56  buffer[3] = char(date >> 8);
57  }
58  else // !dt.isValid(), assume 1980-01-01 midnight
59  {
60  buffer[0] = 0;
61  buffer[1] = 0;
62  buffer[2] = 33;
63  buffer[3] = 0;
64  }
65 }
66 
67 static time_t transformFromMsDos(const char* buffer)
68 {
69  quint16 time = (uchar)buffer[0] | ( (uchar)buffer[1] << 8 );
70  int h = time >> 11;
71  int m = ( time & 0x7ff ) >> 5;
72  int s = ( time & 0x1f ) * 2 ;
73  QTime qt(h, m, s);
74 
75  quint16 date = (uchar)buffer[2] | ( (uchar)buffer[3] << 8 );
76  int y = ( date >> 9 ) + 1980;
77  int o = ( date & 0x1ff ) >> 5;
78  int d = ( date & 0x1f );
79  QDate qd(y, o, d);
80 
81  QDateTime dt( qd, qt );
82  return dt.toTime_t();
83 }
84 
85 // == parsing routines for zip headers
86 
88 struct ParseFileInfo {
89  // file related info
90  mode_t perm; // permissions of this file
91  time_t atime; // last access time (UNIX format)
92  time_t mtime; // modification time (UNIX format)
93  time_t ctime; // creation time (UNIX format)
94  int uid; // user id (-1 if not specified)
95  int gid; // group id (-1 if not specified)
96  QByteArray guessed_symlink; // guessed symlink target
97  int extralen; // length of extra field
98 
99  // parsing related info
100  bool exttimestamp_seen; // true if extended timestamp extra field
101  // has been parsed
102  bool newinfounix_seen; // true if Info-ZIP Unix New extra field has
103  // been parsed
104 
105  ParseFileInfo() : perm(0100644), uid(-1), gid(-1), extralen(0),
106  exttimestamp_seen(false), newinfounix_seen(false) {
107  ctime = mtime = atime = time(0);
108  }
109 };
110 
119 static bool parseExtTimestamp(const char *buffer, int size, bool islocal,
120  ParseFileInfo &pfi) {
121  if (size < 1) {
122  kDebug(7040) << "premature end of extended timestamp (#1)";
123  return false;
124  }/*end if*/
125  int flags = *buffer; // read flags
126  buffer += 1;
127  size -= 1;
128 
129  if (flags & 1) { // contains modification time
130  if (size < 4) {
131  kDebug(7040) << "premature end of extended timestamp (#2)";
132  return false;
133  }/*end if*/
134  pfi.mtime = time_t((uchar)buffer[0] | (uchar)buffer[1] << 8
135  | (uchar)buffer[2] << 16 | (uchar)buffer[3] << 24);
136  buffer += 4;
137  size -= 4;
138  }/*end if*/
139  // central extended field cannot contain more than the modification time
140  // even if other flags are set
141  if (!islocal) {
142  pfi.exttimestamp_seen = true;
143  return true;
144  }/*end if*/
145 
146  if (flags & 2) { // contains last access time
147  if (size < 4) {
148  kDebug(7040) << "premature end of extended timestamp (#3)";
149  return true;
150  }/*end if*/
151  pfi.atime = time_t((uchar)buffer[0] | (uchar)buffer[1] << 8
152  | (uchar)buffer[2] << 16 | (uchar)buffer[3] << 24);
153  buffer += 4;
154  size -= 4;
155  }/*end if*/
156 
157  if (flags & 4) { // contains creation time
158  if (size < 4) {
159  kDebug(7040) << "premature end of extended timestamp (#4)";
160  return true;
161  }/*end if*/
162  pfi.ctime = time_t((uchar)buffer[0] | (uchar)buffer[1] << 8
163  | (uchar)buffer[2] << 16 | (uchar)buffer[3] << 24);
164  buffer += 4;
165  }/*end if*/
166 
167  pfi.exttimestamp_seen = true;
168  return true;
169 }
170 
179 static bool parseInfoZipUnixOld(const char *buffer, int size, bool islocal,
180  ParseFileInfo &pfi) {
181  // spec mandates to omit this field if one of the newer fields are available
182  if (pfi.exttimestamp_seen || pfi.newinfounix_seen) return true;
183 
184  if (size < 8) {
185  kDebug(7040) << "premature end of Info-ZIP unix extra field old";
186  return false;
187  }/*end if*/
188 
189  pfi.atime = time_t((uchar)buffer[0] | (uchar)buffer[1] << 8
190  | (uchar)buffer[2] << 16 | (uchar)buffer[3] << 24);
191  buffer += 4;
192  pfi.mtime = time_t((uchar)buffer[0] | (uchar)buffer[1] << 8
193  | (uchar)buffer[2] << 16 | (uchar)buffer[3] << 24);
194  buffer += 4;
195  if (islocal && size >= 12) {
196  pfi.uid = (uchar)buffer[0] | (uchar)buffer[1] << 8;
197  buffer += 2;
198  pfi.gid = (uchar)buffer[0] | (uchar)buffer[1] << 8;
199  buffer += 2;
200  }/*end if*/
201  return true;
202 }
203 
204 #if 0 // not needed yet
205 
213 static bool parseInfoZipUnixNew(const char *buffer, int size, bool islocal,
214  ParseFileInfo &pfi) {
215  if (!islocal) { // contains nothing in central field
216  pfi.newinfounix = true;
217  return true;
218  }/*end if*/
219 
220  if (size < 4) {
221  kDebug(7040) << "premature end of Info-ZIP unix extra field new";
222  return false;
223  }/*end if*/
224 
225  pfi.uid = (uchar)buffer[0] | (uchar)buffer[1] << 8;
226  buffer += 2;
227  pfi.gid = (uchar)buffer[0] | (uchar)buffer[1] << 8;
228  buffer += 2;
229 
230  pfi.newinfounix = true;
231  return true;
232 }
233 #endif
234 
243 static bool parseExtraField(const char *buffer, int size, bool islocal,
244  ParseFileInfo &pfi) {
245  // extra field in central directory doesn't contain useful data, so we
246  // don't bother parsing it
247  if (!islocal) return true;
248 
249  while (size >= 4) { // as long as a potential extra field can be read
250  int magic = (uchar)buffer[0] | (uchar)buffer[1] << 8;
251  buffer += 2;
252  int fieldsize = (uchar)buffer[0] | (uchar)buffer[1] << 8;
253  buffer += 2;
254  size -= 4;
255 
256  if (fieldsize > size) {
257  //kDebug(7040) << "fieldsize: " << fieldsize << " size: " << size;
258  kDebug(7040) << "premature end of extra fields reached";
259  break;
260  }/*end if*/
261 
262  switch (magic) {
263  case 0x5455: // extended timestamp
264  if (!parseExtTimestamp(buffer, fieldsize, islocal, pfi)) return false;
265  break;
266  case 0x5855: // old Info-ZIP unix extra field
267  if (!parseInfoZipUnixOld(buffer, fieldsize, islocal, pfi)) return false;
268  break;
269 #if 0 // not needed yet
270  case 0x7855: // new Info-ZIP unix extra field
271  if (!parseInfoZipUnixNew(buffer, fieldsize, islocal, pfi)) return false;
272  break;
273 #endif
274  default:
275  /* ignore everything else */;
276  }/*end switch*/
277 
278  buffer += fieldsize;
279  size -= fieldsize;
280  }/*wend*/
281  return true;
282 }
283 
287 
288 class KZip::KZipPrivate
289 {
290 public:
291  KZipPrivate()
292  : m_crc( 0 ),
293  m_currentFile( 0 ),
294  m_currentDev( 0 ),
295  m_compression( 8 ),
296  m_extraField( KZip::NoExtraField ),
297  m_offset( 0 )
298  {}
299 
300  unsigned long m_crc; // checksum
301  KZipFileEntry* m_currentFile; // file currently being written
302  QIODevice* m_currentDev; // filterdev used to write to the above file
303  QList<KZipFileEntry*> m_fileList; // flat list of all files, for the index (saves a recursive method ;)
304  int m_compression;
305  KZip::ExtraField m_extraField;
306  // m_offset holds the offset of the place in the zip,
307  // where new data can be appended. after openarchive it points to 0, when in
308  // writeonly mode, or it points to the beginning of the central directory.
309  // each call to writefile updates this value.
310  quint64 m_offset;
311 };
312 
313 KZip::KZip( const QString& fileName )
314  : KArchive( fileName ),d(new KZipPrivate)
315 {
316 }
317 
318 KZip::KZip( QIODevice * dev )
319  : KArchive( dev ),d(new KZipPrivate)
320 {
321 }
322 
323 KZip::~KZip()
324 {
325  //kDebug(7040) << this;
326  if( isOpen() )
327  close();
328  delete d;
329 }
330 
331 bool KZip::openArchive( QIODevice::OpenMode mode )
332 {
333  //kDebug(7040);
334  d->m_fileList.clear();
335 
336  if ( mode == QIODevice::WriteOnly )
337  return true;
338 
339  char buffer[47];
340 
341  // Check that it's a valid ZIP file
342  // KArchive::open() opened the underlying device already.
343 
344  quint64 offset = 0; // holds offset, where we read
345  int n;
346 
347  // contains information gathered from the local file headers
348  QHash<QByteArray, ParseFileInfo> pfi_map;
349 
350  QIODevice* dev = device();
351 
352  // We set a bool for knowing if we are allowed to skip the start of the file
353  bool startOfFile = true;
354 
355  for (;;) // repeat until 'end of entries' signature is reached
356  {
357  //kDebug(7040) << "loop starts";
358  //kDebug(7040) << "dev->pos() now : " << dev->pos();
359  n = dev->read( buffer, 4 );
360 
361  if (n < 4)
362  {
363  kWarning(7040) << "Invalid ZIP file. Unexpected end of file. (#1)";
364 
365  return false;
366  }
367 
368  if ( !memcmp( buffer, "PK\5\6", 4 ) ) // 'end of entries'
369  {
370  //kDebug(7040) << "PK56 found end of archive";
371  startOfFile = false;
372  break;
373  }
374 
375  if ( !memcmp( buffer, "PK\3\4", 4 ) ) // local file header
376  {
377  //kDebug(7040) << "PK34 found local file header";
378  startOfFile = false;
379  // can this fail ???
380  dev->seek( dev->pos() + 2 ); // skip 'version needed to extract'
381 
382  // read static header stuff
383  n = dev->read( buffer, 24 );
384  if (n < 24) {
385  kWarning(7040) << "Invalid ZIP file. Unexpected end of file. (#4)";
386  return false;
387  }
388 
389  int gpf = (uchar)buffer[0]; // "general purpose flag" not "general protection fault" ;-)
390  int compression_mode = (uchar)buffer[2] | (uchar)buffer[3] << 8;
391  time_t mtime = transformFromMsDos( buffer+4 );
392 
393  const qint64 compr_size = uint(uchar(buffer[12])) | uint(uchar(buffer[13])) << 8 |
394  uint(uchar(buffer[14])) << 16 | uint(uchar(buffer[15])) << 24;
395  const qint64 uncomp_size = uint(uchar(buffer[16])) | uint(uchar(buffer[17])) << 8 |
396  uint(uchar(buffer[18])) << 16 | uint(uchar(buffer[19])) << 24;
397  const int namelen = uint(uchar(buffer[20])) | uint(uchar(buffer[21])) << 8;
398  const int extralen = uint(uchar(buffer[22])) | uint(uchar(buffer[23])) << 8;
399 
400  /*
401  kDebug(7040) << "general purpose bit flag: " << gpf;
402  kDebug(7040) << "compressed size: " << compr_size;
403  kDebug(7040) << "uncompressed size: " << uncomp_size;
404  kDebug(7040) << "namelen: " << namelen;
405  kDebug(7040) << "extralen: " << extralen;
406  kDebug(7040) << "archive size: " << dev->size();
407  */
408 
409  // read fileName
410  Q_ASSERT( namelen > 0 );
411  QByteArray fileName = dev->read(namelen);
412  if ( fileName.size() < namelen ) {
413  kWarning(7040) << "Invalid ZIP file. Name not completely read (#2)";
414  return false;
415  }
416 
417  ParseFileInfo pfi;
418  pfi.mtime = mtime;
419 
420  // read and parse the beginning of the extra field,
421  // skip rest of extra field in case it is too long
422  unsigned int extraFieldEnd = dev->pos() + extralen;
423  pfi.extralen = extralen;
424  int handledextralen = qMin(extralen, (int)sizeof buffer);
425 
426  //if ( handledextralen )
427  // kDebug(7040) << "handledextralen: " << handledextralen;
428 
429  n = dev->read(buffer, handledextralen);
430  // no error msg necessary as we deliberately truncate the extra field
431  if (!parseExtraField(buffer, handledextralen, true, pfi))
432  {
433  kWarning(7040) << "Invalid ZIP File. Broken ExtraField.";
434  return false;
435  }
436 
437  // jump to end of extra field
438  dev->seek( extraFieldEnd );
439 
440  // we have to take care of the 'general purpose bit flag'.
441  // if bit 3 is set, the header doesn't contain the length of
442  // the file and we look for the signature 'PK\7\8'.
443  if ( gpf & 8 )
444  {
445  // here we have to read through the compressed data to find
446  // the next PKxx
447  //kDebug(7040) << "trying to seek for next PK78";
448  bool foundSignature = false;
449 
450  while (!foundSignature)
451  {
452  n = dev->read( buffer, 1 );
453  if (n < 1)
454  {
455  kWarning(7040) << "Invalid ZIP file. Unexpected end of file. (#2)";
456  return false;
457  }
458 
459  if ( buffer[0] != 'P' )
460  continue;
461 
462  n = dev->read( buffer, 3 );
463  if (n < 3)
464  {
465  kWarning(7040) << "Invalid ZIP file. Unexpected end of file. (#3)";
466  return false;
467  }
468 
469  // we have to detect three magic tokens here:
470  // PK34 for the next local header in case there is no data descriptor
471  // PK12 for the central header in case there is no data descriptor
472  // PK78 for the data descriptor in case it is following the compressed data
473 
474  if ( buffer[0] == 'K' && buffer[1] == 7 && buffer[2] == 8 )
475  {
476  foundSignature = true;
477  dev->seek( dev->pos() + 12 ); // skip the 'data_descriptor'
478  }
479  else if ( ( buffer[0] == 'K' && buffer[1] == 1 && buffer[2] == 2 )
480  || ( buffer[0] == 'K' && buffer[1] == 3 && buffer[2] == 4 ) )
481  {
482  foundSignature = true;
483  dev->seek( dev->pos() - 4 ); // go back 4 bytes, so that the magic bytes can be found...
484  }
485  else if ( buffer[0] == 'P' || buffer[1] == 'P' || buffer[2] == 'P' )
486  {
487  // We have another P character so we must go back a little to check if it is a magic
488  dev->seek( dev->pos() - 3 );
489  }
490 
491  }
492  }
493  else
494  {
495  // here we skip the compressed data and jump to the next header
496  //kDebug(7040) << "general purpose bit flag indicates, that local file header contains valid size";
497  // check if this could be a symbolic link
498  if (compression_mode == NoCompression
499  && uncomp_size <= max_path_len
500  && uncomp_size > 0) {
501  // read content and store it
502  // If it's not a symlink, then we'll just discard the data for now.
503  pfi.guessed_symlink = dev->read(uncomp_size);
504  if (pfi.guessed_symlink.size() < uncomp_size) {
505  kWarning(7040) << "Invalid ZIP file. Unexpected end of file. (#5)";
506  return false;
507  }
508  } else {
509 
510  if ( compr_size > dev->size() )
511  {
512  // here we cannot trust the compressed size, so scan through the compressed
513  // data to find the next header
514  bool foundSignature = false;
515 
516  while (!foundSignature)
517  {
518  n = dev->read( buffer, 1 );
519  if (n < 1)
520  {
521  kWarning(7040) << "Invalid ZIP file. Unexpected end of file. (#2)";
522  return false;
523  }
524 
525  if ( buffer[0] != 'P' )
526  continue;
527 
528  n = dev->read( buffer, 3 );
529  if (n < 3)
530  {
531  kWarning(7040) << "Invalid ZIP file. Unexpected end of file. (#3)";
532  return false;
533  }
534 
535  // we have to detect three magic tokens here:
536  // PK34 for the next local header in case there is no data descriptor
537  // PK12 for the central header in case there is no data descriptor
538  // PK78 for the data descriptor in case it is following the compressed data
539 
540  if ( buffer[0] == 'K' && buffer[1] == 7 && buffer[2] == 8 )
541  {
542  foundSignature = true;
543  dev->seek( dev->pos() + 12 ); // skip the 'data_descriptor'
544  }
545 
546  if ( ( buffer[0] == 'K' && buffer[1] == 1 && buffer[2] == 2 )
547  || ( buffer[0] == 'K' && buffer[1] == 3 && buffer[2] == 4 ) )
548  {
549  foundSignature = true;
550  dev->seek( dev->pos() - 4 );
551  // go back 4 bytes, so that the magic bytes can be found
552  // in the next cycle...
553  }
554  }
555  }
556  else
557  {
558 // kDebug(7040) << "before interesting dev->pos(): " << dev->pos();
559  bool success = dev->seek( dev->pos() + compr_size ); // can this fail ???
560  Q_UNUSED( success ); // prevent warning in release builds.
561  Q_ASSERT( success ); // let's see...
562 /* kDebug(7040) << "after interesting dev->pos(): " << dev->pos();
563  if ( success )
564  kDebug(7040) << "dev->at was successful... ";
565  else
566  kDebug(7040) << "dev->at failed... ";*/
567  }
568 
569  }
570 
571 // not needed any more
572 /* // here we calculate the length of the file in the zip
573  // with headers and jump to the next header.
574  uint skip = compr_size + namelen + extralen;
575  offset += 30 + skip;*/
576  }
577  pfi_map.insert(fileName, pfi);
578  }
579  else if ( !memcmp( buffer, "PK\1\2", 4 ) ) // central block
580  {
581  //kDebug(7040) << "PK12 found central block";
582  startOfFile = false;
583 
584  // so we reached the central header at the end of the zip file
585  // here we get all interesting data out of the central header
586  // of a file
587  offset = dev->pos() - 4;
588 
589  //set offset for appending new files
590  if ( d->m_offset == 0L ) d->m_offset = offset;
591 
592  n = dev->read( buffer + 4, 42 );
593  if (n < 42) {
594  kWarning(7040) << "Invalid ZIP file, central entry too short"; // not long enough for valid entry
595  return false;
596  }
597 
598  //int gpf = (uchar)buffer[9] << 8 | (uchar)buffer[10];
599  //kDebug() << "general purpose flag=" << gpf;
600  // length of the fileName (well, pathname indeed)
601  int namelen = (uchar)buffer[29] << 8 | (uchar)buffer[28];
602  Q_ASSERT( namelen > 0 );
603  QByteArray bufferName = dev->read( namelen );
604  if ( bufferName.size() < namelen )
605  kWarning(7040) << "Invalid ZIP file. Name not completely read";
606 
607  ParseFileInfo pfi = pfi_map.value( bufferName, ParseFileInfo() );
608 
609  QString name( QFile::decodeName(bufferName) );
610 
611  //kDebug(7040) << "name: " << name;
612  // only in central header ! see below.
613  // length of extra attributes
614  int extralen = (uchar)buffer[31] << 8 | (uchar)buffer[30];
615  // length of comment for this file
616  int commlen = (uchar)buffer[33] << 8 | (uchar)buffer[32];
617  // compression method of this file
618  int cmethod = (uchar)buffer[11] << 8 | (uchar)buffer[10];
619 
620  //kDebug(7040) << "cmethod: " << cmethod;
621  //kDebug(7040) << "extralen: " << extralen;
622 
623  // crc32 of the file
624  uint crc32 = (uchar)buffer[19] << 24 | (uchar)buffer[18] << 16 |
625  (uchar)buffer[17] << 8 | (uchar)buffer[16];
626 
627  // uncompressed file size
628  uint ucsize = (uchar)buffer[27] << 24 | (uchar)buffer[26] << 16 |
629  (uchar)buffer[25] << 8 | (uchar)buffer[24];
630  // compressed file size
631  uint csize = (uchar)buffer[23] << 24 | (uchar)buffer[22] << 16 |
632  (uchar)buffer[21] << 8 | (uchar)buffer[20];
633 
634  // offset of local header
635  uint localheaderoffset = (uchar)buffer[45] << 24 | (uchar)buffer[44] << 16 |
636  (uchar)buffer[43] << 8 | (uchar)buffer[42];
637 
638  // some clever people use different extra field lengths
639  // in the central header and in the local header... funny.
640  // so we need to get the localextralen to calculate the offset
641  // from localheaderstart to dataoffset
642  int localextralen = pfi.extralen; // FIXME: this will not work if
643  // no local header exists
644 
645  //kDebug(7040) << "localextralen: " << localextralen;
646 
647  // offset, where the real data for uncompression starts
648  uint dataoffset = localheaderoffset + 30 + localextralen + namelen; //comment only in central header
649 
650  //kDebug(7040) << "esize: " << esize;
651  //kDebug(7040) << "eoffset: " << eoffset;
652  //kDebug(7040) << "csize: " << csize;
653 
654  int os_madeby = (uchar)buffer[5];
655  bool isdir = false;
656  int access = 0100644;
657 
658  if (os_madeby == 3) { // good ole unix
659  access = (uchar)buffer[40] | (uchar)buffer[41] << 8;
660  }
661 
662  QString entryName;
663 
664  if (name.endsWith(QLatin1Char('/'))) { // Entries with a trailing slash are directories
665  isdir = true;
666  name = name.left( name.length() - 1 );
667  if (os_madeby != 3) access = S_IFDIR | 0755;
668  else Q_ASSERT(access & S_IFDIR);
669  }
670 
671  int pos = name.lastIndexOf(QLatin1Char('/'));
672  if ( pos == -1 )
673  entryName = name;
674  else
675  entryName = name.mid( pos + 1 );
676  Q_ASSERT( !entryName.isEmpty() );
677 
678  KArchiveEntry* entry;
679  if ( isdir )
680  {
681  QString path = QDir::cleanPath( name );
682  const KArchiveEntry* ent = rootDir()->entry( path );
683  if ( ent && ent->isDirectory() )
684  {
685  //kDebug(7040) << "Directory already exists, NOT going to add it again";
686  entry = 0;
687  }
688  else
689  {
690  entry = new KArchiveDirectory( this, entryName, access, (int)pfi.mtime, rootDir()->user(), rootDir()->group(), QString() );
691  //kDebug(7040) << "KArchiveDirectory created, entryName= " << entryName << ", name=" << name;
692  }
693  }
694  else
695  {
696  QString symlink;
697  if (S_ISLNK(access)) {
698  symlink = QFile::decodeName(pfi.guessed_symlink);
699  }
700  entry = new KZipFileEntry( this, entryName, access, pfi.mtime,
701  rootDir()->user(), rootDir()->group(),
702  symlink, name, dataoffset,
703  ucsize, cmethod, csize );
704  static_cast<KZipFileEntry *>(entry)->setHeaderStart( localheaderoffset );
705  static_cast<KZipFileEntry*>(entry)->setCRC32(crc32);
706  //kDebug(7040) << "KZipFileEntry created, entryName= " << entryName << ", name=" << name;
707  d->m_fileList.append( static_cast<KZipFileEntry *>( entry ) );
708  }
709 
710  if ( entry )
711  {
712  if ( pos == -1 )
713  {
714  rootDir()->addEntry(entry);
715  }
716  else
717  {
718  // In some tar files we can find dir/./file => call cleanPath
719  QString path = QDir::cleanPath( name.left( pos ) );
720  // Ensure container directory exists, create otherwise
721  KArchiveDirectory * tdir = findOrCreate( path );
722  tdir->addEntry(entry);
723  }
724  }
725 
726  //calculate offset to next entry
727  offset += 46 + commlen + extralen + namelen;
728  bool b = dev->seek(offset);
729  Q_ASSERT( b );
730  if ( !b )
731  return false;
732  }
733  else if ( startOfFile )
734  {
735  // The file does not start with any ZIP header (e.g. self-extractable ZIP files)
736  // Therefore we need to find the first PK\003\004 (local header)
737  //kDebug(7040) << "Try to skip start of file";
738  startOfFile = false;
739  bool foundSignature = false;
740 
741  while (!foundSignature)
742  {
743  n = dev->read( buffer, 1 );
744  if (n < 1)
745  {
746  kWarning(7040) << "Invalid ZIP file. Unexpected end of file. " ;
747  return false;
748  }
749 
750  if ( buffer[0] != 'P' )
751  continue;
752 
753  n = dev->read( buffer, 3 );
754  if (n < 3)
755  {
756  kWarning(7040) << "Invalid ZIP file. Unexpected end of file. " ;
757  return false;
758  }
759 
760  // We have to detect the magic token for a local header: PK\003\004
761  /*
762  * Note: we do not need to check the other magics, if the ZIP file has no
763  * local header, then it has not any files!
764  */
765  if ( buffer[0] == 'K' && buffer[1] == 3 && buffer[2] == 4 )
766  {
767  foundSignature = true;
768  dev->seek( dev->pos() - 4 ); // go back 4 bytes, so that the magic bytes can be found...
769  }
770  else if ( buffer[0] == 'P' || buffer[1] == 'P' || buffer[2] == 'P' )
771  {
772  // We have another P character so we must go back a little to check if it is a magic
773  dev->seek( dev->pos() - 3 );
774  }
775  }
776  }
777  else
778  {
779  kWarning(7040) << "Invalid ZIP file. Unrecognized header at offset " << offset;
780 
781  return false;
782  }
783  }
784  //kDebug(7040) << "*** done *** ";
785  return true;
786 }
787 
788 bool KZip::closeArchive()
789 {
790  if ( ! ( mode() & QIODevice::WriteOnly ) )
791  {
792  //kDebug(7040) << "readonly";
793  return true;
794  }
795 
796  //ReadWrite or WriteOnly
797  //write all central dir file entries
798 
799  // to be written at the end of the file...
800  char buffer[ 22 ]; // first used for 12, then for 22 at the end
801  uLong crc = crc32(0L, Z_NULL, 0);
802 
803  qint64 centraldiroffset = device()->pos();
804  //kDebug(7040) << "closearchive: centraldiroffset: " << centraldiroffset;
805  qint64 atbackup = centraldiroffset;
806  QMutableListIterator<KZipFileEntry*> it( d->m_fileList );
807 
808  while(it.hasNext())
809  { //set crc and compressed size in each local file header
810  it.next();
811  if ( !device()->seek( it.value()->headerStart() + 14 ) )
812  return false;
813  //kDebug(7040) << "closearchive setcrcandcsize: fileName:"
814  // << it.current()->path()
815  // << "encoding:" << it.current()->encoding();
816 
817  uLong mycrc = it.value()->crc32();
818  buffer[0] = char(mycrc); // crc checksum, at headerStart+14
819  buffer[1] = char(mycrc >> 8);
820  buffer[2] = char(mycrc >> 16);
821  buffer[3] = char(mycrc >> 24);
822 
823  int mysize1 = it.value()->compressedSize();
824  buffer[4] = char(mysize1); // compressed file size, at headerStart+18
825  buffer[5] = char(mysize1 >> 8);
826  buffer[6] = char(mysize1 >> 16);
827  buffer[7] = char(mysize1 >> 24);
828 
829  int myusize = it.value()->size();
830  buffer[8] = char(myusize); // uncompressed file size, at headerStart+22
831  buffer[9] = char(myusize >> 8);
832  buffer[10] = char(myusize >> 16);
833  buffer[11] = char(myusize >> 24);
834 
835  if ( device()->write( buffer, 12 ) != 12 )
836  return false;
837  }
838  device()->seek( atbackup );
839 
840  it.toFront();
841  while (it.hasNext())
842  {
843  it.next();
844  //kDebug(7040) << "fileName:" << it.current()->path()
845  // << "encoding:" << it.current()->encoding();
846 
847  QByteArray path = QFile::encodeName(it.value()->path());
848 
849  const int extra_field_len = 9;
850  int bufferSize = extra_field_len + path.length() + 46;
851  char* buffer = new char[ bufferSize ];
852 
853  memset(buffer, 0, 46); // zero is a nice default for most header fields
854 
855  const char head[] =
856  {
857  'P', 'K', 1, 2, // central file header signature
858  0x14, 3, // version made by (3 == UNIX)
859  0x14, 0 // version needed to extract
860  };
861 
862  // I do not know why memcpy is not working here
863  //memcpy(buffer, head, sizeof(head));
864  memmove(buffer, head, sizeof(head));
865 
866  buffer[ 10 ] = char(it.value()->encoding()); // compression method
867  buffer[ 11 ] = char(it.value()->encoding() >> 8);
868 
869  transformToMsDos( it.value()->datetime(), &buffer[ 12 ] );
870 
871  uLong mycrc = it.value()->crc32();
872  buffer[ 16 ] = char(mycrc); // crc checksum
873  buffer[ 17 ] = char(mycrc >> 8);
874  buffer[ 18 ] = char(mycrc >> 16);
875  buffer[ 19 ] = char(mycrc >> 24);
876 
877  int mysize1 = it.value()->compressedSize();
878  buffer[ 20 ] = char(mysize1); // compressed file size
879  buffer[ 21 ] = char(mysize1 >> 8);
880  buffer[ 22 ] = char(mysize1 >> 16);
881  buffer[ 23 ] = char(mysize1 >> 24);
882 
883  int mysize = it.value()->size();
884  buffer[ 24 ] = char(mysize); // uncompressed file size
885  buffer[ 25 ] = char(mysize >> 8);
886  buffer[ 26 ] = char(mysize >> 16);
887  buffer[ 27 ] = char(mysize >> 24);
888 
889  buffer[ 28 ] = char(path.length()); // fileName length
890  buffer[ 29 ] = char(path.length() >> 8);
891 
892  buffer[ 30 ] = char(extra_field_len);
893  buffer[ 31 ] = char(extra_field_len >> 8);
894 
895  buffer[ 40 ] = char(it.value()->permissions());
896  buffer[ 41 ] = char(it.value()->permissions() >> 8);
897 
898  int myhst = it.value()->headerStart();
899  buffer[ 42 ] = char(myhst); //relative offset of local header
900  buffer[ 43 ] = char(myhst >> 8);
901  buffer[ 44 ] = char(myhst >> 16);
902  buffer[ 45 ] = char(myhst >> 24);
903 
904  // file name
905  strncpy( buffer + 46, path, path.length() );
906  //kDebug(7040) << "closearchive length to write: " << bufferSize;
907 
908  // extra field
909  char *extfield = buffer + 46 + path.length();
910  extfield[0] = 'U';
911  extfield[1] = 'T';
912  extfield[2] = 5;
913  extfield[3] = 0;
914  extfield[4] = 1 | 2 | 4; // specify flags from local field
915  // (unless I misread the spec)
916  // provide only modification time
917  unsigned long time = (unsigned long)it.value()->date();
918  extfield[5] = char(time);
919  extfield[6] = char(time >> 8);
920  extfield[7] = char(time >> 16);
921  extfield[8] = char(time >> 24);
922 
923  crc = crc32(crc, (Bytef *)buffer, bufferSize );
924  bool ok = ( device()->write( buffer, bufferSize ) == bufferSize );
925  delete[] buffer;
926  if ( !ok )
927  return false;
928  }
929  qint64 centraldirendoffset = device()->pos();
930  //kDebug(7040) << "closearchive: centraldirendoffset: " << centraldirendoffset;
931  //kDebug(7040) << "closearchive: device()->pos(): " << device()->pos();
932 
933  //write end of central dir record.
934  buffer[ 0 ] = 'P'; //end of central dir signature
935  buffer[ 1 ] = 'K';
936  buffer[ 2 ] = 5;
937  buffer[ 3 ] = 6;
938 
939  buffer[ 4 ] = 0; // number of this disk
940  buffer[ 5 ] = 0;
941 
942  buffer[ 6 ] = 0; // number of disk with start of central dir
943  buffer[ 7 ] = 0;
944 
945  int count = d->m_fileList.count();
946  //kDebug(7040) << "number of files (count): " << count;
947 
948 
949  buffer[ 8 ] = char(count); // total number of entries in central dir of
950  buffer[ 9 ] = char(count >> 8); // this disk
951 
952  buffer[ 10 ] = buffer[ 8 ]; // total number of entries in the central dir
953  buffer[ 11 ] = buffer[ 9 ];
954 
955  int cdsize = centraldirendoffset - centraldiroffset;
956  buffer[ 12 ] = char(cdsize); // size of the central dir
957  buffer[ 13 ] = char(cdsize >> 8);
958  buffer[ 14 ] = char(cdsize >> 16);
959  buffer[ 15 ] = char(cdsize >> 24);
960 
961  //kDebug(7040) << "end : centraldiroffset: " << centraldiroffset;
962  //kDebug(7040) << "end : centraldirsize: " << cdsize;
963 
964  buffer[ 16 ] = char(centraldiroffset); // central dir offset
965  buffer[ 17 ] = char(centraldiroffset >> 8);
966  buffer[ 18 ] = char(centraldiroffset >> 16);
967  buffer[ 19 ] = char(centraldiroffset >> 24);
968 
969  buffer[ 20 ] = 0; //zipfile comment length
970  buffer[ 21 ] = 0;
971 
972  if ( device()->write( buffer, 22 ) != 22 )
973  return false;
974 
975  return true;
976 }
977 
978 bool KZip::doWriteDir( const QString &name, const QString &user, const QString &group,
979  mode_t perm, time_t atime, time_t mtime, time_t ctime ) {
980  // Zip files have no explicit directories, they are implicitly created during extraction time
981  // when file entries have paths in them.
982  // However, to support empty directories, we must create a dummy file entry which ends with '/'.
983  QString dirName = name;
984  if (!name.endsWith(QLatin1Char('/')))
985  dirName = dirName.append(QLatin1Char('/'));
986  return writeFile(dirName, user, group, 0, 0, perm, atime, mtime, ctime);
987 }
988 
989 bool KZip::doPrepareWriting(const QString &name, const QString &user,
990  const QString &group, qint64 /*size*/, mode_t perm,
991  time_t atime, time_t mtime, time_t ctime) {
992  //kDebug(7040);
993  if ( !isOpen() )
994  {
995  qWarning( "KZip::writeFile: You must open the zip file before writing to it\n");
996  return false;
997  }
998 
999  if ( ! ( mode() & QIODevice::WriteOnly ) ) // accept WriteOnly and ReadWrite
1000  {
1001  qWarning( "KZip::writeFile: You must open the zip file for writing\n");
1002  return false;
1003  }
1004 
1005  Q_ASSERT( device() );
1006 
1007  // set right offset in zip.
1008  if ( !device()->seek( d->m_offset ) ) {
1009  kWarning(7040) << "doPrepareWriting: cannot seek in ZIP file. Disk full?";
1010  return false;
1011  }
1012 
1013  // Find or create parent dir
1014  KArchiveDirectory* parentDir = rootDir();
1015  QString fileName( name );
1016  int i = name.lastIndexOf(QLatin1Char('/'));
1017  if (i != -1) {
1018  QString dir = name.left( i );
1019  fileName = name.mid( i + 1 );
1020  //kDebug(7040) << "ensuring" << dir << "exists. fileName=" << fileName;
1021  parentDir = findOrCreate( dir );
1022  }
1023 
1024  // delete entries in the filelist with the same fileName as the one we want
1025  // to save, so that we don't have duplicate file entries when viewing the zip
1026  // with konqi...
1027  // CAUTION: the old file itself is still in the zip and won't be removed !!!
1028  QMutableListIterator<KZipFileEntry*> it( d->m_fileList );
1029  //kDebug(7040) << "fileName to write: " << name;
1030  while(it.hasNext())
1031  {
1032  it.next();
1033  //kDebug(7040) << "prepfileName: " << it.current()->path();
1034  if (name == it.value()->path() )
1035  {
1036  // also remove from the parentDir
1037  parentDir->removeEntry(it.value());
1038  //kDebug(7040) << "removing following entry: " << it.current()->path();
1039  delete it.value();
1040  it.remove();
1041  }
1042 
1043  }
1044 
1045  // construct a KZipFileEntry and add it to list
1046  KZipFileEntry * e = new KZipFileEntry( this, fileName, perm, mtime, user, group, QString(),
1047  name, device()->pos() + 30 + name.length(), // start
1048  0 /*size unknown yet*/, d->m_compression, 0 /*csize unknown yet*/ );
1049  e->setHeaderStart( device()->pos() );
1050  //kDebug(7040) << "wrote file start: " << e->position() << " name: " << name;
1051  parentDir->addEntry( e );
1052 
1053  d->m_currentFile = e;
1054  d->m_fileList.append( e );
1055 
1056  int extra_field_len = 0;
1057  if ( d->m_extraField == ModificationTime )
1058  extra_field_len = 17; // value also used in finishWriting()
1059 
1060  // write out zip header
1061  QByteArray encodedName = QFile::encodeName(name);
1062  int bufferSize = extra_field_len + encodedName.length() + 30;
1063  //kDebug(7040) << "bufferSize=" << bufferSize;
1064  char* buffer = new char[ bufferSize ];
1065 
1066  buffer[ 0 ] = 'P'; //local file header signature
1067  buffer[ 1 ] = 'K';
1068  buffer[ 2 ] = 3;
1069  buffer[ 3 ] = 4;
1070 
1071  buffer[ 4 ] = 0x14; // version needed to extract
1072  buffer[ 5 ] = 0;
1073 
1074  buffer[ 6 ] = 0; // general purpose bit flag
1075  buffer[ 7 ] = 0;
1076 
1077  buffer[ 8 ] = char(e->encoding()); // compression method
1078  buffer[ 9 ] = char(e->encoding() >> 8);
1079 
1080  transformToMsDos( e->datetime(), &buffer[ 10 ] );
1081 
1082  buffer[ 14 ] = 'C'; //dummy crc
1083  buffer[ 15 ] = 'R';
1084  buffer[ 16 ] = 'C';
1085  buffer[ 17 ] = 'q';
1086 
1087  buffer[ 18 ] = 'C'; //compressed file size
1088  buffer[ 19 ] = 'S';
1089  buffer[ 20 ] = 'I';
1090  buffer[ 21 ] = 'Z';
1091 
1092  buffer[ 22 ] = 'U'; //uncompressed file size
1093  buffer[ 23 ] = 'S';
1094  buffer[ 24 ] = 'I';
1095  buffer[ 25 ] = 'Z';
1096 
1097  buffer[ 26 ] = (uchar)(encodedName.length()); //fileName length
1098  buffer[ 27 ] = (uchar)(encodedName.length() >> 8);
1099 
1100  buffer[ 28 ] = (uchar)(extra_field_len); // extra field length
1101  buffer[ 29 ] = (uchar)(extra_field_len >> 8);
1102 
1103  // file name
1104  strncpy( buffer + 30, encodedName, encodedName.length() );
1105 
1106  // extra field
1107  if ( d->m_extraField == ModificationTime )
1108  {
1109  char *extfield = buffer + 30 + encodedName.length();
1110  // "Extended timestamp" header (0x5455)
1111  extfield[0] = 'U';
1112  extfield[1] = 'T';
1113  extfield[2] = 13; // data size
1114  extfield[3] = 0;
1115  extfield[4] = 1 | 2 | 4; // contains mtime, atime, ctime
1116 
1117  extfield[5] = char(mtime);
1118  extfield[6] = char(mtime >> 8);
1119  extfield[7] = char(mtime >> 16);
1120  extfield[8] = char(mtime >> 24);
1121 
1122  extfield[9] = char(atime);
1123  extfield[10] = char(atime >> 8);
1124  extfield[11] = char(atime >> 16);
1125  extfield[12] = char(atime >> 24);
1126 
1127  extfield[13] = char(ctime);
1128  extfield[14] = char(ctime >> 8);
1129  extfield[15] = char(ctime >> 16);
1130  extfield[16] = char(ctime >> 24);
1131  }
1132 
1133  // Write header
1134  bool b = (device()->write( buffer, bufferSize ) == bufferSize );
1135  d->m_crc = 0L;
1136  delete[] buffer;
1137 
1138  Q_ASSERT( b );
1139  if (!b) {
1140  return false;
1141  }
1142 
1143  // Prepare device for writing the data
1144  // Either device() if no compression, or a KFilterDev to compress
1145  if ( d->m_compression == 0 ) {
1146  d->m_currentDev = device();
1147  return true;
1148  }
1149 
1150  d->m_currentDev = KFilterDev::device( device(), QString::fromLatin1("application/x-gzip"), false );
1151  Q_ASSERT( d->m_currentDev );
1152  if ( !d->m_currentDev ) {
1153  return false; // ouch
1154  }
1155  static_cast<KFilterDev *>(d->m_currentDev)->setSkipHeaders(); // Just zlib, not gzip
1156 
1157  b = d->m_currentDev->open( QIODevice::WriteOnly );
1158  Q_ASSERT( b );
1159  return b;
1160 }
1161 
1162 bool KZip::doFinishWriting( qint64 size )
1163 {
1164  if ( d->m_currentFile->encoding() == 8 ) {
1165  // Finish
1166  (void)d->m_currentDev->write( 0, 0 );
1167  delete d->m_currentDev;
1168  }
1169  // If 0, d->m_currentDev was device() - don't delete ;)
1170  d->m_currentDev = 0L;
1171 
1172  Q_ASSERT( d->m_currentFile );
1173  //kDebug(7040) << "fileName: " << d->m_currentFile->path();
1174  //kDebug(7040) << "getpos (at): " << device()->pos();
1175  d->m_currentFile->setSize(size);
1176  int extra_field_len = 0;
1177  if ( d->m_extraField == ModificationTime )
1178  extra_field_len = 17; // value also used in finishWriting()
1179 
1180  const QByteArray encodedName = QFile::encodeName(d->m_currentFile->path());
1181  int csize = device()->pos() -
1182  d->m_currentFile->headerStart() - 30 -
1183  encodedName.length() - extra_field_len;
1184  d->m_currentFile->setCompressedSize(csize);
1185  //kDebug(7040) << "usize: " << d->m_currentFile->size();
1186  //kDebug(7040) << "csize: " << d->m_currentFile->compressedSize();
1187  //kDebug(7040) << "headerstart: " << d->m_currentFile->headerStart();
1188 
1189  //kDebug(7040) << "crc: " << d->m_crc;
1190  d->m_currentFile->setCRC32( d->m_crc );
1191 
1192  d->m_currentFile = 0L;
1193 
1194  // update saved offset for appending new files
1195  d->m_offset = device()->pos();
1196  return true;
1197 }
1198 
1199 bool KZip::doWriteSymLink(const QString &name, const QString &target,
1200  const QString &user, const QString &group,
1201  mode_t perm, time_t atime, time_t mtime, time_t ctime) {
1202  // reassure that symlink flag is set, otherwise strange things happen on
1203  // extraction
1204  perm |= S_IFLNK;
1205  Compression c = compression();
1206  setCompression(NoCompression); // link targets are never compressed
1207 
1208  if (!doPrepareWriting(name, user, group, 0, perm, atime, mtime, ctime)) {
1209  kWarning() << "prepareWriting failed";
1210  setCompression(c);
1211  return false;
1212  }
1213 
1214  QByteArray symlink_target = QFile::encodeName(target);
1215  if (!writeData(symlink_target, symlink_target.length())) {
1216  kWarning() << "writeData failed";
1217  setCompression(c);
1218  return false;
1219  }
1220 
1221  if (!finishWriting(symlink_target.length())) {
1222  kWarning() << "finishWriting failed";
1223  setCompression(c);
1224  return false;
1225  }
1226 
1227  setCompression(c);
1228  return true;
1229 }
1230 
1231 void KZip::virtual_hook( int id, void* data )
1232 {
1233  KArchive::virtual_hook( id, data );
1234 }
1235 
1236 bool KZip::writeData(const char * data, qint64 size)
1237 {
1238  Q_ASSERT( d->m_currentFile );
1239  Q_ASSERT( d->m_currentDev );
1240  if (!d->m_currentFile || !d->m_currentDev) {
1241  return false;
1242  }
1243 
1244  // crc to be calculated over uncompressed stuff...
1245  // and they didn't mention it in their docs...
1246  d->m_crc = crc32(d->m_crc, (const Bytef *) data , size);
1247 
1248  qint64 written = d->m_currentDev->write( data, size );
1249  //kDebug(7040) << "wrote" << size << "bytes.";
1250  return written == size;
1251 }
1252 
1253 void KZip::setCompression( Compression c )
1254 {
1255  d->m_compression = ( c == NoCompression ) ? 0 : 8;
1256 }
1257 
1258 KZip::Compression KZip::compression() const
1259 {
1260  return ( d->m_compression == 8 ) ? DeflateCompression : NoCompression;
1261 }
1262 
1263 void KZip::setExtraField( ExtraField ef )
1264 {
1265  d->m_extraField = ef;
1266 }
1267 
1268 KZip::ExtraField KZip::extraField() const
1269 {
1270  return d->m_extraField;
1271 }
1272 
1276 class KZipFileEntry::KZipFileEntryPrivate
1277 {
1278 public:
1279  KZipFileEntryPrivate()
1280  : crc(0),
1281  compressedSize(0),
1282  headerStart(0),
1283  encoding(0)
1284  {}
1285  unsigned long crc;
1286  qint64 compressedSize;
1287  qint64 headerStart;
1288  int encoding;
1289  QString path;
1290 };
1291 
1292 KZipFileEntry::KZipFileEntry(KZip* zip, const QString& name, int access, int date,
1293  const QString& user, const QString& group, const QString& symlink,
1294  const QString& path, qint64 start, qint64 uncompressedSize,
1295  int encoding, qint64 compressedSize)
1296  : KArchiveFile(zip, name, access, date, user, group, symlink, start, uncompressedSize ),
1297  d(new KZipFileEntryPrivate)
1298 {
1299  d->path = path;
1300  d->encoding = encoding;
1301  d->compressedSize = compressedSize;
1302 }
1303 
1304 KZipFileEntry::~KZipFileEntry()
1305 {
1306  delete d;
1307 }
1308 
1309 int KZipFileEntry::encoding() const
1310 {
1311  return d->encoding;
1312 }
1313 
1314 qint64 KZipFileEntry::compressedSize() const
1315 {
1316  return d->compressedSize;
1317 }
1318 
1319 void KZipFileEntry::setCompressedSize(qint64 compressedSize)
1320 {
1321  d->compressedSize = compressedSize;
1322 }
1323 
1324 void KZipFileEntry::setHeaderStart(qint64 headerstart)
1325 {
1326  d->headerStart = headerstart;
1327 }
1328 
1329 qint64 KZipFileEntry::headerStart() const
1330 {
1331  return d->headerStart;
1332 }
1333 
1334 unsigned long KZipFileEntry::crc32() const
1335 {
1336  return d->crc;
1337 }
1338 
1339 void KZipFileEntry::setCRC32(unsigned long crc32)
1340 {
1341  d->crc=crc32;
1342 }
1343 
1344 const QString &KZipFileEntry::path() const
1345 {
1346  return d->path;
1347 }
1348 
1349 QByteArray KZipFileEntry::data() const
1350 {
1351  QIODevice* dev = createDevice();
1352  QByteArray arr;
1353  if ( dev ) {
1354  arr = dev->readAll();
1355  delete dev;
1356  }
1357  return arr;
1358 }
1359 
1360 QIODevice* KZipFileEntry::createDevice() const
1361 {
1362  //kDebug(7040) << "creating iodevice limited to pos=" << position() << ", csize=" << compressedSize();
1363  // Limit the reading to the appropriate part of the underlying device (e.g. file)
1364  KLimitedIODevice* limitedDev = new KLimitedIODevice( archive()->device(), position(), compressedSize() );
1365  if ( encoding() == 0 || compressedSize() == 0 ) // no compression (or even no data)
1366  return limitedDev;
1367 
1368  if ( encoding() == 8 )
1369  {
1370  // On top of that, create a device that uncompresses the zlib data
1371  QIODevice* filterDev = KFilterDev::device( limitedDev, QString::fromLatin1("application/x-gzip") );
1372  if ( !filterDev )
1373  return 0L; // ouch
1374  static_cast<KFilterDev *>(filterDev)->setSkipHeaders(); // Just zlib, not gzip
1375  bool b = filterDev->open( QIODevice::ReadOnly );
1376  Q_UNUSED( b );
1377  Q_ASSERT( b );
1378  return filterDev;
1379  }
1380 
1381  kError() << "This zip file contains files compressed with method"
1382  << encoding() << ", this method is currently not supported by KZip,"
1383  << "please use a command-line tool to handle this file.";
1384  return 0L;
1385 }
KZip::ExtraField
ExtraField
Describes the contents of the "extra field" for a given file in the Zip archive.
Definition: kzip.h:74
KArchiveDirectory::removeEntry
void removeEntry(KArchiveEntry *)
Definition: karchive.cpp:770
qint64
KZip::setCompression
void setCompression(Compression c)
Call this before writeFile or prepareWriting, to define whether the next files to be written should b...
Definition: kzip.cpp:1253
KArchive::virtual_hook
virtual void virtual_hook(int id, void *data)
Definition: karchive.cpp:860
kdebug.h
KArchive::rootDir
virtual KArchiveDirectory * rootDir()
Retrieves or create the root directory.
Definition: karchive.cpp:391
KArchiveEntry::isDirectory
virtual bool isDirectory() const
Checks whether the entry is a directory.
Definition: karchive.cpp:581
kfilterdev.h
KArchive::device
QIODevice * device() const
The underlying device.
Definition: karchive.cpp:475
KMacroExpander::group
Definition: kmacroexpander_unix.cpp:34
KArchive
KArchive is a base class for reading and writing archives.
Definition: karchive.h:43
KZipFileEntry::setCRC32
void setCRC32(unsigned long crc32)
Definition: kzip.cpp:1339
kError
static QDebug kError(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
Definition: kdebug.h:187
KZipFileEntry::data
virtual QByteArray data() const
Definition: kzip.cpp:1349
KZipFileEntry::headerStart
qint64 headerStart() const
Definition: kzip.cpp:1329
QString
QHash
Definition: ksycocafactory.h:28
transformFromMsDos
static time_t transformFromMsDos(const char *buffer)
Definition: kzip.cpp:67
KZipFileEntry::crc32
unsigned long crc32() const
CRC: only used when writing.
Definition: kzip.cpp:1334
transformToMsDos
static void transformToMsDos(const QDateTime &dt, char *buffer)
Definition: kzip.cpp:38
KArchive::close
virtual bool close()
Closes the archive.
Definition: karchive.cpp:164
KArchive::mode
QIODevice::OpenMode mode() const
Returns the mode in which the archive was opened.
Definition: karchive.cpp:470
KArchive::writeFile
virtual bool writeFile(const QString &name, const QString &user, const QString &group, const char *data, qint64 size, mode_t perm=0100644, time_t atime=UnknownTime, time_t mtime=UnknownTime, time_t ctime=UnknownTime)
If an archive is opened for writing then you can add a new file using this function.
Definition: karchive.cpp:319
KArchiveFile::position
qint64 position() const
Position of the data in the [uncompressed] archive.
Definition: karchive.cpp:620
KZipFileEntry::setHeaderStart
void setHeaderStart(qint64 headerstart)
Header start: only used when writing.
Definition: kzip.cpp:1324
KZip::closeArchive
virtual bool closeArchive()
Closes the archive.
Definition: kzip.cpp:788
kzip.h
KZipFileEntry::compressedSize
qint64 compressedSize() const
Definition: kzip.cpp:1314
KZipFileEntry::path
const QString & path() const
Name with complete path - KArchiveFile::name() is the filename only (no path)
Definition: kzip.cpp:1344
KZip::doWriteSymLink
virtual bool doWriteSymLink(const QString &name, const QString &target, const QString &user, const QString &group, mode_t perm, time_t atime, time_t mtime, time_t ctime)
Reimplemented from KArchive.
Definition: kzip.cpp:1199
parseExtraField
static bool parseExtraField(const char *buffer, int size, bool islocal, ParseFileInfo &pfi)
parses the extra field
Definition: kzip.cpp:243
KArchive::fileName
QString fileName() const
The name of the archive file, as passed to the constructor that takes a fileName, or an empty string ...
Definition: karchive.cpp:485
KFilterDev
A class for reading and writing compressed data onto a device (e.g.
Definition: kfilterdev.h:36
KZip::virtual_hook
virtual void virtual_hook(int id, void *data)
Definition: kzip.cpp:1231
klimitediodevice_p.h
KArchiveEntry
A base class for entries in an KArchive.
Definition: karchive.h:369
KArchive::finishWriting
virtual bool finishWriting(qint64 size)
Call finishWriting after writing the data.
Definition: karchive.cpp:386
KZipFileEntry::KZipFileEntry
KZipFileEntry(KZip *zip, const QString &name, int access, int date, const QString &user, const QString &group, const QString &symlink, const QString &path, qint64 start, qint64 uncompressedSize, int encoding, qint64 compressedSize)
Creates a new zip file entry.
Definition: kzip.cpp:1292
KZip::openArchive
virtual bool openArchive(QIODevice::OpenMode mode)
Opens the archive for reading.
Definition: kzip.cpp:331
KLimitedIODevice
A readonly device that reads from an underlying device from a given point to another (e...
Definition: klimitediodevice_p.h:31
KZip
A class for reading / writing zip archives.
Definition: kzip.h:45
max_path_len
const int max_path_len
Definition: kzip.cpp:36
KZip::DeflateCompression
Deflate compression method.
Definition: kzip.h:98
KZip::doFinishWriting
virtual bool doFinishWriting(qint64 size)
Write data to a file that has been created using prepareWriting().
Definition: kzip.cpp:1162
KZip::compression
Compression compression() const
The current compression mode that will be used for new files.
Definition: kzip.cpp:1258
KArchive::isOpen
bool isOpen() const
Checks whether the archive is open.
Definition: karchive.cpp:480
KArchiveDirectory::entry
const KArchiveEntry * entry(const QString &name) const
Returns the entry with the given name.
Definition: karchive.cpp:721
kWarning
#define kWarning
Definition: kdebug.h:322
KArchiveDirectory::addEntry
void addEntry(KArchiveEntry *)
Definition: karchive.cpp:757
KZip::Compression
Compression
Describes the compression type for a given file in the Zip archive.
Definition: kzip.h:97
QDateTime
KZip::KZip
KZip(const QString &filename)
Creates an instance that operates on the given filename.
Definition: kzip.cpp:313
KFilterDev::device
static QIODevice * device(QIODevice *inDevice, const QString &mimetype, bool autoDeleteInDevice=true)
Creates an i/o device that is able to read from the QIODevice inDevice, whether the data is compresse...
Definition: kfilterdev.cpp:84
KZip::NoExtraField
No extra field.
Definition: kzip.h:74
KArchive::findOrCreate
KArchiveDirectory * findOrCreate(const QString &path)
Ensures that path exists, create otherwise.
Definition: karchive.cpp:406
KZip::NoCompression
Uncompressed.
Definition: kzip.h:97
KArchiveDirectory
Represents a directory entry in a KArchive.
Definition: karchive.h:542
KDE::access
int access(const QString &path, int mode)
Definition: kde_file_win.cpp:123
quint64
KZip::setExtraField
void setExtraField(ExtraField ef)
Call this before writeFile or prepareWriting, to define what the next file to be written should have ...
Definition: kzip.cpp:1263
parseExtTimestamp
static bool parseExtTimestamp(const char *buffer, int size, bool islocal, ParseFileInfo &pfi)
updates the parse information with the given extended timestamp extra field.
Definition: kzip.cpp:119
KZip::doWriteDir
virtual bool doWriteDir(const QString &name, const QString &user, const QString &group, mode_t perm, time_t atime, time_t mtime, time_t ctime)
Reimplemented from KArchive.
Definition: kzip.cpp:978
KZip::writeData
virtual bool writeData(const char *data, qint64 size)
Write data to a file that has been created using prepareWriting().
Definition: kzip.cpp:1236
KArchiveFile
Represents a file entry in a KArchive.
Definition: karchive.h:457
KZip::~KZip
virtual ~KZip()
If the zip file is still opened, then it will be closed automatically by the destructor.
Definition: kzip.cpp:323
kDebug
#define kDebug
Definition: kdebug.h:316
KZip::doPrepareWriting
virtual bool doPrepareWriting(const QString &name, const QString &user, const QString &group, qint64 size, mode_t perm, time_t atime, time_t mtime, time_t ctime)
Reimplemented from KArchive.
Definition: kzip.cpp:989
KZipFileEntry::~KZipFileEntry
~KZipFileEntry()
Destructor.
Definition: kzip.cpp:1304
QIODevice
parseInfoZipUnixOld
static bool parseInfoZipUnixOld(const char *buffer, int size, bool islocal, ParseFileInfo &pfi)
updates the parse information with the given Info-ZIP Unix old extra field.
Definition: kzip.cpp:179
KArchiveEntry::datetime
QDateTime datetime() const
Creation date of the file.
Definition: karchive.cpp:539
KZipFileEntry::setCompressedSize
void setCompressedSize(qint64 compressedSize)
Only used when writing.
Definition: kzip.cpp:1319
KZipFileEntry::encoding
int encoding() const
Definition: kzip.cpp:1309
KZipFileEntry
A KZipFileEntry represents an file in a zip archive.
Definition: kzip.h:170
QList< KZipFileEntry * >
KArchiveEntry::archive
KArchive * archive() const
Definition: karchive.cpp:586
KZip::ModificationTime
Modification time ("extended timestamp" header)
Definition: kzip.h:75
KZip::extraField
ExtraField extraField() const
The current type of "extra field" that will be used for new files.
Definition: kzip.cpp:1268
KZipFileEntry::createDevice
virtual QIODevice * createDevice() const
This method returns a QIODevice to read the file contents.
Definition: kzip.cpp:1360
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:47:09 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal