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

KDECore

  • 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  // delete entries in the filelist with the same fileName as the one we want
1014  // to save, so that we don't have duplicate file entries when viewing the zip
1015  // with konqi...
1016  // CAUTION: the old file itself is still in the zip and won't be removed !!!
1017  QMutableListIterator<KZipFileEntry*> it( d->m_fileList );
1018  //kDebug(7040) << "fileName to write: " << name;
1019  while(it.hasNext())
1020  {
1021  it.next();
1022  //kDebug(7040) << "prepfileName: " << it.current()->path();
1023  if (name == it.value()->path() )
1024  {
1025  //kDebug(7040) << "removing following entry: " << it.current()->path();
1026  delete it.value();
1027  it.remove();
1028  }
1029 
1030  }
1031  // Find or create parent dir
1032  KArchiveDirectory* parentDir = rootDir();
1033  QString fileName( name );
1034  int i = name.lastIndexOf(QLatin1Char('/'));
1035  if (i != -1) {
1036  QString dir = name.left( i );
1037  fileName = name.mid( i + 1 );
1038  //kDebug(7040) << "ensuring" << dir << "exists. fileName=" << fileName;
1039  parentDir = findOrCreate( dir );
1040  }
1041 
1042  // construct a KZipFileEntry and add it to list
1043  KZipFileEntry * e = new KZipFileEntry( this, fileName, perm, mtime, user, group, QString(),
1044  name, device()->pos() + 30 + name.length(), // start
1045  0 /*size unknown yet*/, d->m_compression, 0 /*csize unknown yet*/ );
1046  e->setHeaderStart( device()->pos() );
1047  //kDebug(7040) << "wrote file start: " << e->position() << " name: " << name;
1048  parentDir->addEntry( e );
1049 
1050  d->m_currentFile = e;
1051  d->m_fileList.append( e );
1052 
1053  int extra_field_len = 0;
1054  if ( d->m_extraField == ModificationTime )
1055  extra_field_len = 17; // value also used in finishWriting()
1056 
1057  // write out zip header
1058  QByteArray encodedName = QFile::encodeName(name);
1059  int bufferSize = extra_field_len + encodedName.length() + 30;
1060  //kDebug(7040) << "bufferSize=" << bufferSize;
1061  char* buffer = new char[ bufferSize ];
1062 
1063  buffer[ 0 ] = 'P'; //local file header signature
1064  buffer[ 1 ] = 'K';
1065  buffer[ 2 ] = 3;
1066  buffer[ 3 ] = 4;
1067 
1068  buffer[ 4 ] = 0x14; // version needed to extract
1069  buffer[ 5 ] = 0;
1070 
1071  buffer[ 6 ] = 0; // general purpose bit flag
1072  buffer[ 7 ] = 0;
1073 
1074  buffer[ 8 ] = char(e->encoding()); // compression method
1075  buffer[ 9 ] = char(e->encoding() >> 8);
1076 
1077  transformToMsDos( e->datetime(), &buffer[ 10 ] );
1078 
1079  buffer[ 14 ] = 'C'; //dummy crc
1080  buffer[ 15 ] = 'R';
1081  buffer[ 16 ] = 'C';
1082  buffer[ 17 ] = 'q';
1083 
1084  buffer[ 18 ] = 'C'; //compressed file size
1085  buffer[ 19 ] = 'S';
1086  buffer[ 20 ] = 'I';
1087  buffer[ 21 ] = 'Z';
1088 
1089  buffer[ 22 ] = 'U'; //uncompressed file size
1090  buffer[ 23 ] = 'S';
1091  buffer[ 24 ] = 'I';
1092  buffer[ 25 ] = 'Z';
1093 
1094  buffer[ 26 ] = (uchar)(encodedName.length()); //fileName length
1095  buffer[ 27 ] = (uchar)(encodedName.length() >> 8);
1096 
1097  buffer[ 28 ] = (uchar)(extra_field_len); // extra field length
1098  buffer[ 29 ] = (uchar)(extra_field_len >> 8);
1099 
1100  // file name
1101  strncpy( buffer + 30, encodedName, encodedName.length() );
1102 
1103  // extra field
1104  if ( d->m_extraField == ModificationTime )
1105  {
1106  char *extfield = buffer + 30 + encodedName.length();
1107  // "Extended timestamp" header (0x5455)
1108  extfield[0] = 'U';
1109  extfield[1] = 'T';
1110  extfield[2] = 13; // data size
1111  extfield[3] = 0;
1112  extfield[4] = 1 | 2 | 4; // contains mtime, atime, ctime
1113 
1114  extfield[5] = char(mtime);
1115  extfield[6] = char(mtime >> 8);
1116  extfield[7] = char(mtime >> 16);
1117  extfield[8] = char(mtime >> 24);
1118 
1119  extfield[9] = char(atime);
1120  extfield[10] = char(atime >> 8);
1121  extfield[11] = char(atime >> 16);
1122  extfield[12] = char(atime >> 24);
1123 
1124  extfield[13] = char(ctime);
1125  extfield[14] = char(ctime >> 8);
1126  extfield[15] = char(ctime >> 16);
1127  extfield[16] = char(ctime >> 24);
1128  }
1129 
1130  // Write header
1131  bool b = (device()->write( buffer, bufferSize ) == bufferSize );
1132  d->m_crc = 0L;
1133  delete[] buffer;
1134 
1135  Q_ASSERT( b );
1136  if (!b) {
1137  return false;
1138  }
1139 
1140  // Prepare device for writing the data
1141  // Either device() if no compression, or a KFilterDev to compress
1142  if ( d->m_compression == 0 ) {
1143  d->m_currentDev = device();
1144  return true;
1145  }
1146 
1147  d->m_currentDev = KFilterDev::device( device(), QString::fromLatin1("application/x-gzip"), false );
1148  Q_ASSERT( d->m_currentDev );
1149  if ( !d->m_currentDev ) {
1150  return false; // ouch
1151  }
1152  static_cast<KFilterDev *>(d->m_currentDev)->setSkipHeaders(); // Just zlib, not gzip
1153 
1154  b = d->m_currentDev->open( QIODevice::WriteOnly );
1155  Q_ASSERT( b );
1156  return b;
1157 }
1158 
1159 bool KZip::doFinishWriting( qint64 size )
1160 {
1161  if ( d->m_currentFile->encoding() == 8 ) {
1162  // Finish
1163  (void)d->m_currentDev->write( 0, 0 );
1164  delete d->m_currentDev;
1165  }
1166  // If 0, d->m_currentDev was device() - don't delete ;)
1167  d->m_currentDev = 0L;
1168 
1169  Q_ASSERT( d->m_currentFile );
1170  //kDebug(7040) << "fileName: " << d->m_currentFile->path();
1171  //kDebug(7040) << "getpos (at): " << device()->pos();
1172  d->m_currentFile->setSize(size);
1173  int extra_field_len = 0;
1174  if ( d->m_extraField == ModificationTime )
1175  extra_field_len = 17; // value also used in finishWriting()
1176 
1177  const QByteArray encodedName = QFile::encodeName(d->m_currentFile->path());
1178  int csize = device()->pos() -
1179  d->m_currentFile->headerStart() - 30 -
1180  encodedName.length() - extra_field_len;
1181  d->m_currentFile->setCompressedSize(csize);
1182  //kDebug(7040) << "usize: " << d->m_currentFile->size();
1183  //kDebug(7040) << "csize: " << d->m_currentFile->compressedSize();
1184  //kDebug(7040) << "headerstart: " << d->m_currentFile->headerStart();
1185 
1186  //kDebug(7040) << "crc: " << d->m_crc;
1187  d->m_currentFile->setCRC32( d->m_crc );
1188 
1189  d->m_currentFile = 0L;
1190 
1191  // update saved offset for appending new files
1192  d->m_offset = device()->pos();
1193  return true;
1194 }
1195 
1196 bool KZip::doWriteSymLink(const QString &name, const QString &target,
1197  const QString &user, const QString &group,
1198  mode_t perm, time_t atime, time_t mtime, time_t ctime) {
1199  // reassure that symlink flag is set, otherwise strange things happen on
1200  // extraction
1201  perm |= S_IFLNK;
1202  Compression c = compression();
1203  setCompression(NoCompression); // link targets are never compressed
1204 
1205  if (!doPrepareWriting(name, user, group, 0, perm, atime, mtime, ctime)) {
1206  kWarning() << "prepareWriting failed";
1207  setCompression(c);
1208  return false;
1209  }
1210 
1211  QByteArray symlink_target = QFile::encodeName(target);
1212  if (!writeData(symlink_target, symlink_target.length())) {
1213  kWarning() << "writeData failed";
1214  setCompression(c);
1215  return false;
1216  }
1217 
1218  if (!finishWriting(symlink_target.length())) {
1219  kWarning() << "finishWriting failed";
1220  setCompression(c);
1221  return false;
1222  }
1223 
1224  setCompression(c);
1225  return true;
1226 }
1227 
1228 void KZip::virtual_hook( int id, void* data )
1229 {
1230  KArchive::virtual_hook( id, data );
1231 }
1232 
1233 bool KZip::writeData(const char * data, qint64 size)
1234 {
1235  Q_ASSERT( d->m_currentFile );
1236  Q_ASSERT( d->m_currentDev );
1237  if (!d->m_currentFile || !d->m_currentDev) {
1238  return false;
1239  }
1240 
1241  // crc to be calculated over uncompressed stuff...
1242  // and they didn't mention it in their docs...
1243  d->m_crc = crc32(d->m_crc, (const Bytef *) data , size);
1244 
1245  qint64 written = d->m_currentDev->write( data, size );
1246  //kDebug(7040) << "wrote" << size << "bytes.";
1247  return written == size;
1248 }
1249 
1250 void KZip::setCompression( Compression c )
1251 {
1252  d->m_compression = ( c == NoCompression ) ? 0 : 8;
1253 }
1254 
1255 KZip::Compression KZip::compression() const
1256 {
1257  return ( d->m_compression == 8 ) ? DeflateCompression : NoCompression;
1258 }
1259 
1260 void KZip::setExtraField( ExtraField ef )
1261 {
1262  d->m_extraField = ef;
1263 }
1264 
1265 KZip::ExtraField KZip::extraField() const
1266 {
1267  return d->m_extraField;
1268 }
1269 
1273 class KZipFileEntry::KZipFileEntryPrivate
1274 {
1275 public:
1276  KZipFileEntryPrivate()
1277  : crc(0),
1278  compressedSize(0),
1279  headerStart(0),
1280  encoding(0)
1281  {}
1282  unsigned long crc;
1283  qint64 compressedSize;
1284  qint64 headerStart;
1285  int encoding;
1286  QString path;
1287 };
1288 
1289 KZipFileEntry::KZipFileEntry(KZip* zip, const QString& name, int access, int date,
1290  const QString& user, const QString& group, const QString& symlink,
1291  const QString& path, qint64 start, qint64 uncompressedSize,
1292  int encoding, qint64 compressedSize)
1293  : KArchiveFile(zip, name, access, date, user, group, symlink, start, uncompressedSize ),
1294  d(new KZipFileEntryPrivate)
1295 {
1296  d->path = path;
1297  d->encoding = encoding;
1298  d->compressedSize = compressedSize;
1299 }
1300 
1301 KZipFileEntry::~KZipFileEntry()
1302 {
1303  delete d;
1304 }
1305 
1306 int KZipFileEntry::encoding() const
1307 {
1308  return d->encoding;
1309 }
1310 
1311 qint64 KZipFileEntry::compressedSize() const
1312 {
1313  return d->compressedSize;
1314 }
1315 
1316 void KZipFileEntry::setCompressedSize(qint64 compressedSize)
1317 {
1318  d->compressedSize = compressedSize;
1319 }
1320 
1321 void KZipFileEntry::setHeaderStart(qint64 headerstart)
1322 {
1323  d->headerStart = headerstart;
1324 }
1325 
1326 qint64 KZipFileEntry::headerStart() const
1327 {
1328  return d->headerStart;
1329 }
1330 
1331 unsigned long KZipFileEntry::crc32() const
1332 {
1333  return d->crc;
1334 }
1335 
1336 void KZipFileEntry::setCRC32(unsigned long crc32)
1337 {
1338  d->crc=crc32;
1339 }
1340 
1341 const QString &KZipFileEntry::path() const
1342 {
1343  return d->path;
1344 }
1345 
1346 QByteArray KZipFileEntry::data() const
1347 {
1348  QIODevice* dev = createDevice();
1349  QByteArray arr;
1350  if ( dev ) {
1351  arr = dev->readAll();
1352  delete dev;
1353  }
1354  return arr;
1355 }
1356 
1357 QIODevice* KZipFileEntry::createDevice() const
1358 {
1359  //kDebug(7040) << "creating iodevice limited to pos=" << position() << ", csize=" << compressedSize();
1360  // Limit the reading to the appropriate part of the underlying device (e.g. file)
1361  KLimitedIODevice* limitedDev = new KLimitedIODevice( archive()->device(), position(), compressedSize() );
1362  if ( encoding() == 0 || compressedSize() == 0 ) // no compression (or even no data)
1363  return limitedDev;
1364 
1365  if ( encoding() == 8 )
1366  {
1367  // On top of that, create a device that uncompresses the zlib data
1368  QIODevice* filterDev = KFilterDev::device( limitedDev, QString::fromLatin1("application/x-gzip") );
1369  if ( !filterDev )
1370  return 0L; // ouch
1371  static_cast<KFilterDev *>(filterDev)->setSkipHeaders(); // Just zlib, not gzip
1372  bool b = filterDev->open( QIODevice::ReadOnly );
1373  Q_UNUSED( b );
1374  Q_ASSERT( b );
1375  return filterDev;
1376  }
1377 
1378  kError() << "This zip file contains files compressed with method"
1379  << encoding() << ", this method is currently not supported by KZip,"
1380  << "please use a command-line tool to handle this file.";
1381  return 0L;
1382 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Wed Mar 20 2013 07:14:38 by doxygen 1.8.3.1 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-4.10.1 API Reference

Skip menu "kdelibs-4.10.1 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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