• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.6 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 
295 static bool handlePossibleHeaderBegin(const char* buffer, QIODevice *dev)
296 {
297  // we have to detect three magic tokens here:
298  // PK34 for the next local header in case there is no data descriptor
299  // PK12 for the central header in case there is no data descriptor
300  // PK78 for the data descriptor in case it is following the compressed data
301  // TODO: optimize using 32bit const data for comparison instead of byte-wise,
302  // given we run at least on 32bit CPUs
303 
304  if (buffer[0] == 'K') {
305  if (buffer[1] == 7 && buffer[2] == 8) {
306  // data descriptor token found
307  dev->seek(dev->pos() + 12); // skip the 'data_descriptor'
308  return true;
309  }
310 
311  if ((buffer[1] == 1 && buffer[2] == 2)
312  || (buffer[1] == 3 && buffer[2] == 4)) {
313  // central/local header token found
314  dev->seek(dev->pos() - 4);
315  // go back 4 bytes, so that the magic bytes can be found
316  // in the next cycle...
317  return true;
318  }
319  }
320  return false;
321 }
322 
329 static bool seekToNextHeaderToken(QIODevice *dev)
330 {
331  bool headerTokenFound = false;
332  char buffer[3];
333 
334  while (!headerTokenFound) {
335  int n = dev->read(buffer, 1);
336  if (n < 1) {
337  //qWarning() << "Invalid ZIP file. Unexpected end of file. (#2)";
338  return false;
339  }
340 
341  if (buffer[0] != 'P') {
342  continue;
343  }
344 
345  n = dev->read(buffer, 3);
346  if (n < 3) {
347  //qWarning() << "Invalid ZIP file. Unexpected end of file. (#3)";
348  return false;
349  }
350 
351  if (handlePossibleHeaderBegin(buffer, dev)) {
352  headerTokenFound = true;
353  } else {
354  for (int i = 0; i < 3; ++i) {
355  if (buffer[i] == 'P') {
356  // We have another P character so we must go back a little to check if it is a magic
357  dev->seek(dev->pos() - 3 + i);
358  break;
359  }
360  }
361  }
362  }
363  return true;
364 }
365 
369 
370 class KZip::KZipPrivate
371 {
372 public:
373  KZipPrivate()
374  : m_crc( 0 ),
375  m_currentFile( 0 ),
376  m_currentDev( 0 ),
377  m_compression( 8 ),
378  m_extraField( KZip::NoExtraField ),
379  m_offset( 0 )
380  {}
381 
382  unsigned long m_crc; // checksum
383  KZipFileEntry* m_currentFile; // file currently being written
384  QIODevice* m_currentDev; // filterdev used to write to the above file
385  QList<KZipFileEntry*> m_fileList; // flat list of all files, for the index (saves a recursive method ;)
386  int m_compression;
387  KZip::ExtraField m_extraField;
388  // m_offset holds the offset of the place in the zip,
389  // where new data can be appended. after openarchive it points to 0, when in
390  // writeonly mode, or it points to the beginning of the central directory.
391  // each call to writefile updates this value.
392  quint64 m_offset;
393 };
394 
395 KZip::KZip( const QString& fileName )
396  : KArchive( fileName ),d(new KZipPrivate)
397 {
398 }
399 
400 KZip::KZip( QIODevice * dev )
401  : KArchive( dev ),d(new KZipPrivate)
402 {
403 }
404 
405 KZip::~KZip()
406 {
407  //kDebug(7040) << this;
408  if( isOpen() )
409  close();
410  delete d;
411 }
412 
413 bool KZip::openArchive( QIODevice::OpenMode mode )
414 {
415  //kDebug(7040);
416  d->m_fileList.clear();
417 
418  if ( mode == QIODevice::WriteOnly )
419  return true;
420 
421  char buffer[47];
422 
423  // Check that it's a valid ZIP file
424  // KArchive::open() opened the underlying device already.
425 
426  quint64 offset = 0; // holds offset, where we read
427  int n;
428 
429  // contains information gathered from the local file headers
430  QHash<QByteArray, ParseFileInfo> pfi_map;
431 
432  QIODevice* dev = device();
433 
434  // We set a bool for knowing if we are allowed to skip the start of the file
435  bool startOfFile = true;
436 
437  for (;;) // repeat until 'end of entries' signature is reached
438  {
439  //kDebug(7040) << "loop starts";
440  //kDebug(7040) << "dev->pos() now : " << dev->pos();
441  n = dev->read( buffer, 4 );
442 
443  if (n < 4)
444  {
445  kWarning(7040) << "Invalid ZIP file. Unexpected end of file. (#1)";
446 
447  return false;
448  }
449 
450  if ( !memcmp( buffer, "PK\5\6", 4 ) ) // 'end of entries'
451  {
452  //kDebug(7040) << "PK56 found end of archive";
453  startOfFile = false;
454  break;
455  }
456 
457  if ( !memcmp( buffer, "PK\3\4", 4 ) ) // local file header
458  {
459  //kDebug(7040) << "PK34 found local file header";
460  startOfFile = false;
461  // can this fail ???
462  dev->seek( dev->pos() + 2 ); // skip 'version needed to extract'
463 
464  // read static header stuff
465  n = dev->read( buffer, 24 );
466  if (n < 24) {
467  kWarning(7040) << "Invalid ZIP file. Unexpected end of file. (#4)";
468  return false;
469  }
470 
471  int gpf = (uchar)buffer[0]; // "general purpose flag" not "general protection fault" ;-)
472  int compression_mode = (uchar)buffer[2] | (uchar)buffer[3] << 8;
473  time_t mtime = transformFromMsDos( buffer+4 );
474 
475  const qint64 compr_size = uint(uchar(buffer[12])) | uint(uchar(buffer[13])) << 8 |
476  uint(uchar(buffer[14])) << 16 | uint(uchar(buffer[15])) << 24;
477  const qint64 uncomp_size = uint(uchar(buffer[16])) | uint(uchar(buffer[17])) << 8 |
478  uint(uchar(buffer[18])) << 16 | uint(uchar(buffer[19])) << 24;
479  const int namelen = uint(uchar(buffer[20])) | uint(uchar(buffer[21])) << 8;
480  const int extralen = uint(uchar(buffer[22])) | uint(uchar(buffer[23])) << 8;
481 
482  /*
483  kDebug(7040) << "general purpose bit flag: " << gpf;
484  kDebug(7040) << "compressed size: " << compr_size;
485  kDebug(7040) << "uncompressed size: " << uncomp_size;
486  kDebug(7040) << "namelen: " << namelen;
487  kDebug(7040) << "extralen: " << extralen;
488  kDebug(7040) << "archive size: " << dev->size();
489  */
490 
491  // read fileName
492  Q_ASSERT( namelen > 0 );
493  QByteArray fileName = dev->read(namelen);
494  if ( fileName.size() < namelen ) {
495  kWarning(7040) << "Invalid ZIP file. Name not completely read (#2)";
496  return false;
497  }
498 
499  ParseFileInfo pfi;
500  pfi.mtime = mtime;
501 
502  // read and parse the beginning of the extra field,
503  // skip rest of extra field in case it is too long
504  unsigned int extraFieldEnd = dev->pos() + extralen;
505  pfi.extralen = extralen;
506  int handledextralen = qMin(extralen, (int)sizeof buffer);
507 
508  //if ( handledextralen )
509  // kDebug(7040) << "handledextralen: " << handledextralen;
510 
511  n = dev->read(buffer, handledextralen);
512  // no error msg necessary as we deliberately truncate the extra field
513  if (!parseExtraField(buffer, handledextralen, true, pfi))
514  {
515  kWarning(7040) << "Invalid ZIP File. Broken ExtraField.";
516  return false;
517  }
518 
519  // jump to end of extra field
520  dev->seek( extraFieldEnd );
521 
522  // we have to take care of the 'general purpose bit flag'.
523  // if bit 3 is set, the header doesn't contain the length of
524  // the file and we look for the signature 'PK\7\8'.
525  if ( gpf & 8 )
526  {
527  // here we have to read through the compressed data to find
528  // the next PKxx
529  if (!seekToNextHeaderToken(dev)) {
530  return false;
531  }
532  }
533  else
534  {
535  // here we skip the compressed data and jump to the next header
536  //kDebug(7040) << "general purpose bit flag indicates, that local file header contains valid size";
537  bool foundSignature = false;
538  // check if this could be a symbolic link
539  if (compression_mode == NoCompression
540  && uncomp_size <= max_path_len
541  && uncomp_size > 0) {
542  // read content and store it
543  // If it's not a symlink, then we'll just discard the data for now.
544  pfi.guessed_symlink = dev->read(uncomp_size);
545  if (pfi.guessed_symlink.size() < uncomp_size) {
546  kWarning(7040) << "Invalid ZIP file. Unexpected end of file. (#5)";
547  return false;
548  }
549  } else {
550 
551  if ( compr_size > dev->size() )
552  {
553  // here we cannot trust the compressed size, so scan through the compressed
554  // data to find the next header
555 
556  if (!seekToNextHeaderToken(dev)) {
557  return false;
558  }
559  foundSignature = true;
560  }
561  else
562  {
563 // kDebug(7040) << "before interesting dev->pos(): " << dev->pos();
564  bool success = dev->seek( dev->pos() + compr_size ); // can this fail ???
565  Q_UNUSED( success ); // prevent warning in release builds.
566  Q_ASSERT( success ); // let's see...
567 /* kDebug(7040) << "after interesting dev->pos(): " << dev->pos();
568  if ( success )
569  kDebug(7040) << "dev->at was successful... ";
570  else
571  kDebug(7040) << "dev->at failed... ";*/
572  }
573 
574  }
575  // test for optional data descriptor
576  if (!foundSignature) {
577 // qDebug() << "Testing for optional data descriptor";
578  // read static data descriptor
579  n = dev->read(buffer, 4);
580  if (n < 4) {
581 // qWarning() << "Invalid ZIP file. Unexpected end of file. (#1)";
582  return false;
583  }
584 
585  if (buffer[0] != 'P' || !handlePossibleHeaderBegin(buffer+1, dev)) {
586  // assume data descriptor without signature
587  dev->seek(dev->pos() + 8); // skip rest of the 'data_descriptor'
588  }
589  }
590 
591 // not needed any more
592 /* // here we calculate the length of the file in the zip
593  // with headers and jump to the next header.
594  uint skip = compr_size + namelen + extralen;
595  offset += 30 + skip;*/
596  }
597  pfi_map.insert(fileName, pfi);
598  }
599  else if ( !memcmp( buffer, "PK\1\2", 4 ) ) // central block
600  {
601  //kDebug(7040) << "PK12 found central block";
602  startOfFile = false;
603 
604  // so we reached the central header at the end of the zip file
605  // here we get all interesting data out of the central header
606  // of a file
607  offset = dev->pos() - 4;
608 
609  //set offset for appending new files
610  if ( d->m_offset == 0L ) d->m_offset = offset;
611 
612  n = dev->read( buffer + 4, 42 );
613  if (n < 42) {
614  kWarning(7040) << "Invalid ZIP file, central entry too short"; // not long enough for valid entry
615  return false;
616  }
617 
618  //int gpf = (uchar)buffer[9] << 8 | (uchar)buffer[10];
619  //kDebug() << "general purpose flag=" << gpf;
620  // length of the fileName (well, pathname indeed)
621  int namelen = (uchar)buffer[29] << 8 | (uchar)buffer[28];
622  Q_ASSERT( namelen > 0 );
623  QByteArray bufferName = dev->read( namelen );
624  if ( bufferName.size() < namelen )
625  kWarning(7040) << "Invalid ZIP file. Name not completely read";
626 
627  ParseFileInfo pfi = pfi_map.value( bufferName, ParseFileInfo() );
628 
629  QString name( QFile::decodeName(bufferName) );
630 
631  //kDebug(7040) << "name: " << name;
632  // only in central header ! see below.
633  // length of extra attributes
634  int extralen = (uchar)buffer[31] << 8 | (uchar)buffer[30];
635  // length of comment for this file
636  int commlen = (uchar)buffer[33] << 8 | (uchar)buffer[32];
637  // compression method of this file
638  int cmethod = (uchar)buffer[11] << 8 | (uchar)buffer[10];
639 
640  //kDebug(7040) << "cmethod: " << cmethod;
641  //kDebug(7040) << "extralen: " << extralen;
642 
643  // crc32 of the file
644  uint crc32 = (uchar)buffer[19] << 24 | (uchar)buffer[18] << 16 |
645  (uchar)buffer[17] << 8 | (uchar)buffer[16];
646 
647  // uncompressed file size
648  uint ucsize = (uchar)buffer[27] << 24 | (uchar)buffer[26] << 16 |
649  (uchar)buffer[25] << 8 | (uchar)buffer[24];
650  // compressed file size
651  uint csize = (uchar)buffer[23] << 24 | (uchar)buffer[22] << 16 |
652  (uchar)buffer[21] << 8 | (uchar)buffer[20];
653 
654  // offset of local header
655  uint localheaderoffset = (uchar)buffer[45] << 24 | (uchar)buffer[44] << 16 |
656  (uchar)buffer[43] << 8 | (uchar)buffer[42];
657 
658  // some clever people use different extra field lengths
659  // in the central header and in the local header... funny.
660  // so we need to get the localextralen to calculate the offset
661  // from localheaderstart to dataoffset
662  int localextralen = pfi.extralen; // FIXME: this will not work if
663  // no local header exists
664 
665  //kDebug(7040) << "localextralen: " << localextralen;
666 
667  // offset, where the real data for uncompression starts
668  uint dataoffset = localheaderoffset + 30 + localextralen + namelen; //comment only in central header
669 
670  //kDebug(7040) << "esize: " << esize;
671  //kDebug(7040) << "eoffset: " << eoffset;
672  //kDebug(7040) << "csize: " << csize;
673 
674  int os_madeby = (uchar)buffer[5];
675  bool isdir = false;
676  int access = 0100644;
677 
678  if (os_madeby == 3) { // good ole unix
679  access = (uchar)buffer[40] | (uchar)buffer[41] << 8;
680  }
681 
682  QString entryName;
683 
684  if (name.endsWith(QLatin1Char('/'))) { // Entries with a trailing slash are directories
685  isdir = true;
686  name = name.left( name.length() - 1 );
687  if (os_madeby != 3) access = S_IFDIR | 0755;
688  else Q_ASSERT(access & S_IFDIR);
689  }
690 
691  int pos = name.lastIndexOf(QLatin1Char('/'));
692  if ( pos == -1 )
693  entryName = name;
694  else
695  entryName = name.mid( pos + 1 );
696  Q_ASSERT( !entryName.isEmpty() );
697 
698  KArchiveEntry* entry;
699  if ( isdir )
700  {
701  QString path = QDir::cleanPath( name );
702  const KArchiveEntry* ent = rootDir()->entry( path );
703  if ( ent && ent->isDirectory() )
704  {
705  //kDebug(7040) << "Directory already exists, NOT going to add it again";
706  entry = 0;
707  }
708  else
709  {
710  entry = new KArchiveDirectory( this, entryName, access, (int)pfi.mtime, rootDir()->user(), rootDir()->group(), QString() );
711  //kDebug(7040) << "KArchiveDirectory created, entryName= " << entryName << ", name=" << name;
712  }
713  }
714  else
715  {
716  QString symlink;
717  if (S_ISLNK(access)) {
718  symlink = QFile::decodeName(pfi.guessed_symlink);
719  }
720  entry = new KZipFileEntry( this, entryName, access, pfi.mtime,
721  rootDir()->user(), rootDir()->group(),
722  symlink, name, dataoffset,
723  ucsize, cmethod, csize );
724  static_cast<KZipFileEntry *>(entry)->setHeaderStart( localheaderoffset );
725  static_cast<KZipFileEntry*>(entry)->setCRC32(crc32);
726  //kDebug(7040) << "KZipFileEntry created, entryName= " << entryName << ", name=" << name;
727  d->m_fileList.append( static_cast<KZipFileEntry *>( entry ) );
728  }
729 
730  if ( entry )
731  {
732  if ( pos == -1 )
733  {
734  rootDir()->addEntry(entry);
735  }
736  else
737  {
738  // In some tar files we can find dir/./file => call cleanPath
739  QString path = QDir::cleanPath( name.left( pos ) );
740  // Ensure container directory exists, create otherwise
741  KArchiveDirectory * tdir = findOrCreate( path );
742  tdir->addEntry(entry);
743  }
744  }
745 
746  //calculate offset to next entry
747  offset += 46 + commlen + extralen + namelen;
748  bool b = dev->seek(offset);
749  Q_ASSERT( b );
750  if ( !b )
751  return false;
752  }
753  else if ( startOfFile )
754  {
755  // The file does not start with any ZIP header (e.g. self-extractable ZIP files)
756  // Therefore we need to find the first PK\003\004 (local header)
757  //kDebug(7040) << "Try to skip start of file";
758  startOfFile = false;
759  bool foundSignature = false;
760 
761  while (!foundSignature)
762  {
763  n = dev->read( buffer, 1 );
764  if (n < 1)
765  {
766  kWarning(7040) << "Invalid ZIP file. Unexpected end of file. " ;
767  return false;
768  }
769 
770  if ( buffer[0] != 'P' )
771  continue;
772 
773  n = dev->read( buffer, 3 );
774  if (n < 3)
775  {
776  kWarning(7040) << "Invalid ZIP file. Unexpected end of file. " ;
777  return false;
778  }
779 
780  // We have to detect the magic token for a local header: PK\003\004
781  /*
782  * Note: we do not need to check the other magics, if the ZIP file has no
783  * local header, then it has not any files!
784  */
785  if ( buffer[0] == 'K' && buffer[1] == 3 && buffer[2] == 4 )
786  {
787  foundSignature = true;
788  dev->seek( dev->pos() - 4 ); // go back 4 bytes, so that the magic bytes can be found...
789  } else {
790  for (int i = 0; i < 3; ++i) {
791  if (buffer[i] == 'P') {
792  // We have another P character so we must go back a little to check if it is a magic
793  dev->seek(dev->pos() - 3 + i);
794  break;
795  }
796  }
797  }
798  }
799  }
800  else
801  {
802  kWarning(7040) << "Invalid ZIP file. Unrecognized header at offset " << offset;
803 
804  return false;
805  }
806  }
807  //kDebug(7040) << "*** done *** ";
808  return true;
809 }
810 
811 bool KZip::closeArchive()
812 {
813  if ( ! ( mode() & QIODevice::WriteOnly ) )
814  {
815  //kDebug(7040) << "readonly";
816  return true;
817  }
818 
819  //ReadWrite or WriteOnly
820  //write all central dir file entries
821 
822  // to be written at the end of the file...
823  char buffer[ 22 ]; // first used for 12, then for 22 at the end
824  uLong crc = crc32(0L, Z_NULL, 0);
825 
826  qint64 centraldiroffset = device()->pos();
827  //kDebug(7040) << "closearchive: centraldiroffset: " << centraldiroffset;
828  qint64 atbackup = centraldiroffset;
829  QMutableListIterator<KZipFileEntry*> it( d->m_fileList );
830 
831  while(it.hasNext())
832  { //set crc and compressed size in each local file header
833  it.next();
834  if ( !device()->seek( it.value()->headerStart() + 14 ) )
835  return false;
836  //kDebug(7040) << "closearchive setcrcandcsize: fileName:"
837  // << it.current()->path()
838  // << "encoding:" << it.current()->encoding();
839 
840  uLong mycrc = it.value()->crc32();
841  buffer[0] = char(mycrc); // crc checksum, at headerStart+14
842  buffer[1] = char(mycrc >> 8);
843  buffer[2] = char(mycrc >> 16);
844  buffer[3] = char(mycrc >> 24);
845 
846  int mysize1 = it.value()->compressedSize();
847  buffer[4] = char(mysize1); // compressed file size, at headerStart+18
848  buffer[5] = char(mysize1 >> 8);
849  buffer[6] = char(mysize1 >> 16);
850  buffer[7] = char(mysize1 >> 24);
851 
852  int myusize = it.value()->size();
853  buffer[8] = char(myusize); // uncompressed file size, at headerStart+22
854  buffer[9] = char(myusize >> 8);
855  buffer[10] = char(myusize >> 16);
856  buffer[11] = char(myusize >> 24);
857 
858  if ( device()->write( buffer, 12 ) != 12 )
859  return false;
860  }
861  device()->seek( atbackup );
862 
863  it.toFront();
864  while (it.hasNext())
865  {
866  it.next();
867  //kDebug(7040) << "fileName:" << it.current()->path()
868  // << "encoding:" << it.current()->encoding();
869 
870  QByteArray path = QFile::encodeName(it.value()->path());
871 
872  const int extra_field_len = 9;
873  int bufferSize = extra_field_len + path.length() + 46;
874  char* buffer = new char[ bufferSize ];
875 
876  memset(buffer, 0, 46); // zero is a nice default for most header fields
877 
878  const char head[] =
879  {
880  'P', 'K', 1, 2, // central file header signature
881  0x14, 3, // version made by (3 == UNIX)
882  0x14, 0 // version needed to extract
883  };
884 
885  // I do not know why memcpy is not working here
886  //memcpy(buffer, head, sizeof(head));
887  memmove(buffer, head, sizeof(head));
888 
889  buffer[ 10 ] = char(it.value()->encoding()); // compression method
890  buffer[ 11 ] = char(it.value()->encoding() >> 8);
891 
892  transformToMsDos( it.value()->datetime(), &buffer[ 12 ] );
893 
894  uLong mycrc = it.value()->crc32();
895  buffer[ 16 ] = char(mycrc); // crc checksum
896  buffer[ 17 ] = char(mycrc >> 8);
897  buffer[ 18 ] = char(mycrc >> 16);
898  buffer[ 19 ] = char(mycrc >> 24);
899 
900  int mysize1 = it.value()->compressedSize();
901  buffer[ 20 ] = char(mysize1); // compressed file size
902  buffer[ 21 ] = char(mysize1 >> 8);
903  buffer[ 22 ] = char(mysize1 >> 16);
904  buffer[ 23 ] = char(mysize1 >> 24);
905 
906  int mysize = it.value()->size();
907  buffer[ 24 ] = char(mysize); // uncompressed file size
908  buffer[ 25 ] = char(mysize >> 8);
909  buffer[ 26 ] = char(mysize >> 16);
910  buffer[ 27 ] = char(mysize >> 24);
911 
912  buffer[ 28 ] = char(path.length()); // fileName length
913  buffer[ 29 ] = char(path.length() >> 8);
914 
915  buffer[ 30 ] = char(extra_field_len);
916  buffer[ 31 ] = char(extra_field_len >> 8);
917 
918  buffer[ 40 ] = char(it.value()->permissions());
919  buffer[ 41 ] = char(it.value()->permissions() >> 8);
920 
921  int myhst = it.value()->headerStart();
922  buffer[ 42 ] = char(myhst); //relative offset of local header
923  buffer[ 43 ] = char(myhst >> 8);
924  buffer[ 44 ] = char(myhst >> 16);
925  buffer[ 45 ] = char(myhst >> 24);
926 
927  // file name
928  strncpy( buffer + 46, path, path.length() );
929  //kDebug(7040) << "closearchive length to write: " << bufferSize;
930 
931  // extra field
932  char *extfield = buffer + 46 + path.length();
933  extfield[0] = 'U';
934  extfield[1] = 'T';
935  extfield[2] = 5;
936  extfield[3] = 0;
937  extfield[4] = 1 | 2 | 4; // specify flags from local field
938  // (unless I misread the spec)
939  // provide only modification time
940  unsigned long time = (unsigned long)it.value()->date();
941  extfield[5] = char(time);
942  extfield[6] = char(time >> 8);
943  extfield[7] = char(time >> 16);
944  extfield[8] = char(time >> 24);
945 
946  crc = crc32(crc, (Bytef *)buffer, bufferSize );
947  bool ok = ( device()->write( buffer, bufferSize ) == bufferSize );
948  delete[] buffer;
949  if ( !ok )
950  return false;
951  }
952  qint64 centraldirendoffset = device()->pos();
953  //kDebug(7040) << "closearchive: centraldirendoffset: " << centraldirendoffset;
954  //kDebug(7040) << "closearchive: device()->pos(): " << device()->pos();
955 
956  //write end of central dir record.
957  buffer[ 0 ] = 'P'; //end of central dir signature
958  buffer[ 1 ] = 'K';
959  buffer[ 2 ] = 5;
960  buffer[ 3 ] = 6;
961 
962  buffer[ 4 ] = 0; // number of this disk
963  buffer[ 5 ] = 0;
964 
965  buffer[ 6 ] = 0; // number of disk with start of central dir
966  buffer[ 7 ] = 0;
967 
968  int count = d->m_fileList.count();
969  //kDebug(7040) << "number of files (count): " << count;
970 
971 
972  buffer[ 8 ] = char(count); // total number of entries in central dir of
973  buffer[ 9 ] = char(count >> 8); // this disk
974 
975  buffer[ 10 ] = buffer[ 8 ]; // total number of entries in the central dir
976  buffer[ 11 ] = buffer[ 9 ];
977 
978  int cdsize = centraldirendoffset - centraldiroffset;
979  buffer[ 12 ] = char(cdsize); // size of the central dir
980  buffer[ 13 ] = char(cdsize >> 8);
981  buffer[ 14 ] = char(cdsize >> 16);
982  buffer[ 15 ] = char(cdsize >> 24);
983 
984  //kDebug(7040) << "end : centraldiroffset: " << centraldiroffset;
985  //kDebug(7040) << "end : centraldirsize: " << cdsize;
986 
987  buffer[ 16 ] = char(centraldiroffset); // central dir offset
988  buffer[ 17 ] = char(centraldiroffset >> 8);
989  buffer[ 18 ] = char(centraldiroffset >> 16);
990  buffer[ 19 ] = char(centraldiroffset >> 24);
991 
992  buffer[ 20 ] = 0; //zipfile comment length
993  buffer[ 21 ] = 0;
994 
995  if ( device()->write( buffer, 22 ) != 22 )
996  return false;
997 
998  return true;
999 }
1000 
1001 bool KZip::doWriteDir( const QString &name, const QString &user, const QString &group,
1002  mode_t perm, time_t atime, time_t mtime, time_t ctime ) {
1003  // Zip files have no explicit directories, they are implicitly created during extraction time
1004  // when file entries have paths in them.
1005  // However, to support empty directories, we must create a dummy file entry which ends with '/'.
1006  QString dirName = name;
1007  if (!name.endsWith(QLatin1Char('/')))
1008  dirName = dirName.append(QLatin1Char('/'));
1009  return writeFile(dirName, user, group, 0, 0, perm, atime, mtime, ctime);
1010 }
1011 
1012 bool KZip::doPrepareWriting(const QString &name, const QString &user,
1013  const QString &group, qint64 /*size*/, mode_t perm,
1014  time_t atime, time_t mtime, time_t ctime) {
1015  //kDebug(7040);
1016  if ( !isOpen() )
1017  {
1018  qWarning( "KZip::writeFile: You must open the zip file before writing to it\n");
1019  return false;
1020  }
1021 
1022  if ( ! ( mode() & QIODevice::WriteOnly ) ) // accept WriteOnly and ReadWrite
1023  {
1024  qWarning( "KZip::writeFile: You must open the zip file for writing\n");
1025  return false;
1026  }
1027 
1028  Q_ASSERT( device() );
1029 
1030  // set right offset in zip.
1031  if ( !device()->seek( d->m_offset ) ) {
1032  kWarning(7040) << "doPrepareWriting: cannot seek in ZIP file. Disk full?";
1033  return false;
1034  }
1035 
1036  // Find or create parent dir
1037  KArchiveDirectory* parentDir = rootDir();
1038  QString fileName( name );
1039  int i = name.lastIndexOf(QLatin1Char('/'));
1040  if (i != -1) {
1041  QString dir = name.left( i );
1042  fileName = name.mid( i + 1 );
1043  //kDebug(7040) << "ensuring" << dir << "exists. fileName=" << fileName;
1044  parentDir = findOrCreate( dir );
1045  }
1046 
1047  // delete entries in the filelist with the same fileName as the one we want
1048  // to save, so that we don't have duplicate file entries when viewing the zip
1049  // with konqi...
1050  // CAUTION: the old file itself is still in the zip and won't be removed !!!
1051  QMutableListIterator<KZipFileEntry*> it( d->m_fileList );
1052  //kDebug(7040) << "fileName to write: " << name;
1053  while(it.hasNext())
1054  {
1055  it.next();
1056  //kDebug(7040) << "prepfileName: " << it.current()->path();
1057  if (name == it.value()->path() )
1058  {
1059  // also remove from the parentDir
1060  parentDir->removeEntry(it.value());
1061  //kDebug(7040) << "removing following entry: " << it.current()->path();
1062  delete it.value();
1063  it.remove();
1064  }
1065 
1066  }
1067 
1068  // construct a KZipFileEntry and add it to list
1069  KZipFileEntry * e = new KZipFileEntry( this, fileName, perm, mtime, user, group, QString(),
1070  name, device()->pos() + 30 + name.length(), // start
1071  0 /*size unknown yet*/, d->m_compression, 0 /*csize unknown yet*/ );
1072  e->setHeaderStart( device()->pos() );
1073  //kDebug(7040) << "wrote file start: " << e->position() << " name: " << name;
1074  parentDir->addEntry( e );
1075 
1076  d->m_currentFile = e;
1077  d->m_fileList.append( e );
1078 
1079  int extra_field_len = 0;
1080  if ( d->m_extraField == ModificationTime )
1081  extra_field_len = 17; // value also used in finishWriting()
1082 
1083  // write out zip header
1084  QByteArray encodedName = QFile::encodeName(name);
1085  int bufferSize = extra_field_len + encodedName.length() + 30;
1086  //kDebug(7040) << "bufferSize=" << bufferSize;
1087  char* buffer = new char[ bufferSize ];
1088 
1089  buffer[ 0 ] = 'P'; //local file header signature
1090  buffer[ 1 ] = 'K';
1091  buffer[ 2 ] = 3;
1092  buffer[ 3 ] = 4;
1093 
1094  buffer[ 4 ] = 0x14; // version needed to extract
1095  buffer[ 5 ] = 0;
1096 
1097  buffer[ 6 ] = 0; // general purpose bit flag
1098  buffer[ 7 ] = 0;
1099 
1100  buffer[ 8 ] = char(e->encoding()); // compression method
1101  buffer[ 9 ] = char(e->encoding() >> 8);
1102 
1103  transformToMsDos( e->datetime(), &buffer[ 10 ] );
1104 
1105  buffer[ 14 ] = 'C'; //dummy crc
1106  buffer[ 15 ] = 'R';
1107  buffer[ 16 ] = 'C';
1108  buffer[ 17 ] = 'q';
1109 
1110  buffer[ 18 ] = 'C'; //compressed file size
1111  buffer[ 19 ] = 'S';
1112  buffer[ 20 ] = 'I';
1113  buffer[ 21 ] = 'Z';
1114 
1115  buffer[ 22 ] = 'U'; //uncompressed file size
1116  buffer[ 23 ] = 'S';
1117  buffer[ 24 ] = 'I';
1118  buffer[ 25 ] = 'Z';
1119 
1120  buffer[ 26 ] = (uchar)(encodedName.length()); //fileName length
1121  buffer[ 27 ] = (uchar)(encodedName.length() >> 8);
1122 
1123  buffer[ 28 ] = (uchar)(extra_field_len); // extra field length
1124  buffer[ 29 ] = (uchar)(extra_field_len >> 8);
1125 
1126  // file name
1127  strncpy( buffer + 30, encodedName, encodedName.length() );
1128 
1129  // extra field
1130  if ( d->m_extraField == ModificationTime )
1131  {
1132  char *extfield = buffer + 30 + encodedName.length();
1133  // "Extended timestamp" header (0x5455)
1134  extfield[0] = 'U';
1135  extfield[1] = 'T';
1136  extfield[2] = 13; // data size
1137  extfield[3] = 0;
1138  extfield[4] = 1 | 2 | 4; // contains mtime, atime, ctime
1139 
1140  extfield[5] = char(mtime);
1141  extfield[6] = char(mtime >> 8);
1142  extfield[7] = char(mtime >> 16);
1143  extfield[8] = char(mtime >> 24);
1144 
1145  extfield[9] = char(atime);
1146  extfield[10] = char(atime >> 8);
1147  extfield[11] = char(atime >> 16);
1148  extfield[12] = char(atime >> 24);
1149 
1150  extfield[13] = char(ctime);
1151  extfield[14] = char(ctime >> 8);
1152  extfield[15] = char(ctime >> 16);
1153  extfield[16] = char(ctime >> 24);
1154  }
1155 
1156  // Write header
1157  bool b = (device()->write( buffer, bufferSize ) == bufferSize );
1158  d->m_crc = 0L;
1159  delete[] buffer;
1160 
1161  Q_ASSERT( b );
1162  if (!b) {
1163  return false;
1164  }
1165 
1166  // Prepare device for writing the data
1167  // Either device() if no compression, or a KFilterDev to compress
1168  if ( d->m_compression == 0 ) {
1169  d->m_currentDev = device();
1170  return true;
1171  }
1172 
1173  d->m_currentDev = KFilterDev::device( device(), QString::fromLatin1("application/x-gzip"), false );
1174  Q_ASSERT( d->m_currentDev );
1175  if ( !d->m_currentDev ) {
1176  return false; // ouch
1177  }
1178  static_cast<KFilterDev *>(d->m_currentDev)->setSkipHeaders(); // Just zlib, not gzip
1179 
1180  b = d->m_currentDev->open( QIODevice::WriteOnly );
1181  Q_ASSERT( b );
1182  return b;
1183 }
1184 
1185 bool KZip::doFinishWriting( qint64 size )
1186 {
1187  if ( d->m_currentFile->encoding() == 8 ) {
1188  // Finish
1189  (void)d->m_currentDev->write( 0, 0 );
1190  delete d->m_currentDev;
1191  }
1192  // If 0, d->m_currentDev was device() - don't delete ;)
1193  d->m_currentDev = 0L;
1194 
1195  Q_ASSERT( d->m_currentFile );
1196  //kDebug(7040) << "fileName: " << d->m_currentFile->path();
1197  //kDebug(7040) << "getpos (at): " << device()->pos();
1198  d->m_currentFile->setSize(size);
1199  int extra_field_len = 0;
1200  if ( d->m_extraField == ModificationTime )
1201  extra_field_len = 17; // value also used in finishWriting()
1202 
1203  const QByteArray encodedName = QFile::encodeName(d->m_currentFile->path());
1204  int csize = device()->pos() -
1205  d->m_currentFile->headerStart() - 30 -
1206  encodedName.length() - extra_field_len;
1207  d->m_currentFile->setCompressedSize(csize);
1208  //kDebug(7040) << "usize: " << d->m_currentFile->size();
1209  //kDebug(7040) << "csize: " << d->m_currentFile->compressedSize();
1210  //kDebug(7040) << "headerstart: " << d->m_currentFile->headerStart();
1211 
1212  //kDebug(7040) << "crc: " << d->m_crc;
1213  d->m_currentFile->setCRC32( d->m_crc );
1214 
1215  d->m_currentFile = 0L;
1216 
1217  // update saved offset for appending new files
1218  d->m_offset = device()->pos();
1219  return true;
1220 }
1221 
1222 bool KZip::doWriteSymLink(const QString &name, const QString &target,
1223  const QString &user, const QString &group,
1224  mode_t perm, time_t atime, time_t mtime, time_t ctime) {
1225  // reassure that symlink flag is set, otherwise strange things happen on
1226  // extraction
1227  perm |= S_IFLNK;
1228  Compression c = compression();
1229  setCompression(NoCompression); // link targets are never compressed
1230 
1231  if (!doPrepareWriting(name, user, group, 0, perm, atime, mtime, ctime)) {
1232  kWarning() << "prepareWriting failed";
1233  setCompression(c);
1234  return false;
1235  }
1236 
1237  QByteArray symlink_target = QFile::encodeName(target);
1238  if (!writeData(symlink_target, symlink_target.length())) {
1239  kWarning() << "writeData failed";
1240  setCompression(c);
1241  return false;
1242  }
1243 
1244  if (!finishWriting(symlink_target.length())) {
1245  kWarning() << "finishWriting failed";
1246  setCompression(c);
1247  return false;
1248  }
1249 
1250  setCompression(c);
1251  return true;
1252 }
1253 
1254 void KZip::virtual_hook( int id, void* data )
1255 {
1256  KArchive::virtual_hook( id, data );
1257 }
1258 
1259 bool KZip::writeData(const char * data, qint64 size)
1260 {
1261  Q_ASSERT( d->m_currentFile );
1262  Q_ASSERT( d->m_currentDev );
1263  if (!d->m_currentFile || !d->m_currentDev) {
1264  return false;
1265  }
1266 
1267  // crc to be calculated over uncompressed stuff...
1268  // and they didn't mention it in their docs...
1269  d->m_crc = crc32(d->m_crc, (const Bytef *) data , size);
1270 
1271  qint64 written = d->m_currentDev->write( data, size );
1272  //kDebug(7040) << "wrote" << size << "bytes.";
1273  return written == size;
1274 }
1275 
1276 void KZip::setCompression( Compression c )
1277 {
1278  d->m_compression = ( c == NoCompression ) ? 0 : 8;
1279 }
1280 
1281 KZip::Compression KZip::compression() const
1282 {
1283  return ( d->m_compression == 8 ) ? DeflateCompression : NoCompression;
1284 }
1285 
1286 void KZip::setExtraField( ExtraField ef )
1287 {
1288  d->m_extraField = ef;
1289 }
1290 
1291 KZip::ExtraField KZip::extraField() const
1292 {
1293  return d->m_extraField;
1294 }
1295 
1299 class KZipFileEntry::KZipFileEntryPrivate
1300 {
1301 public:
1302  KZipFileEntryPrivate()
1303  : crc(0),
1304  compressedSize(0),
1305  headerStart(0),
1306  encoding(0)
1307  {}
1308  unsigned long crc;
1309  qint64 compressedSize;
1310  qint64 headerStart;
1311  int encoding;
1312  QString path;
1313 };
1314 
1315 KZipFileEntry::KZipFileEntry(KZip* zip, const QString& name, int access, int date,
1316  const QString& user, const QString& group, const QString& symlink,
1317  const QString& path, qint64 start, qint64 uncompressedSize,
1318  int encoding, qint64 compressedSize)
1319  : KArchiveFile(zip, name, access, date, user, group, symlink, start, uncompressedSize ),
1320  d(new KZipFileEntryPrivate)
1321 {
1322  d->path = path;
1323  d->encoding = encoding;
1324  d->compressedSize = compressedSize;
1325 }
1326 
1327 KZipFileEntry::~KZipFileEntry()
1328 {
1329  delete d;
1330 }
1331 
1332 int KZipFileEntry::encoding() const
1333 {
1334  return d->encoding;
1335 }
1336 
1337 qint64 KZipFileEntry::compressedSize() const
1338 {
1339  return d->compressedSize;
1340 }
1341 
1342 void KZipFileEntry::setCompressedSize(qint64 compressedSize)
1343 {
1344  d->compressedSize = compressedSize;
1345 }
1346 
1347 void KZipFileEntry::setHeaderStart(qint64 headerstart)
1348 {
1349  d->headerStart = headerstart;
1350 }
1351 
1352 qint64 KZipFileEntry::headerStart() const
1353 {
1354  return d->headerStart;
1355 }
1356 
1357 unsigned long KZipFileEntry::crc32() const
1358 {
1359  return d->crc;
1360 }
1361 
1362 void KZipFileEntry::setCRC32(unsigned long crc32)
1363 {
1364  d->crc=crc32;
1365 }
1366 
1367 const QString &KZipFileEntry::path() const
1368 {
1369  return d->path;
1370 }
1371 
1372 QByteArray KZipFileEntry::data() const
1373 {
1374  QIODevice* dev = createDevice();
1375  QByteArray arr;
1376  if ( dev ) {
1377  arr = dev->readAll();
1378  delete dev;
1379  }
1380  return arr;
1381 }
1382 
1383 QIODevice* KZipFileEntry::createDevice() const
1384 {
1385  //kDebug(7040) << "creating iodevice limited to pos=" << position() << ", csize=" << compressedSize();
1386  // Limit the reading to the appropriate part of the underlying device (e.g. file)
1387  KLimitedIODevice* limitedDev = new KLimitedIODevice( archive()->device(), position(), compressedSize() );
1388  if ( encoding() == 0 || compressedSize() == 0 ) // no compression (or even no data)
1389  return limitedDev;
1390 
1391  if ( encoding() == 8 )
1392  {
1393  // On top of that, create a device that uncompresses the zlib data
1394  QIODevice* filterDev = KFilterDev::device( limitedDev, QString::fromLatin1("application/x-gzip") );
1395  if ( !filterDev )
1396  return 0L; // ouch
1397  static_cast<KFilterDev *>(filterDev)->setSkipHeaders(); // Just zlib, not gzip
1398  bool b = filterDev->open( QIODevice::ReadOnly );
1399  Q_UNUSED( b );
1400  Q_ASSERT( b );
1401  return filterDev;
1402  }
1403 
1404  kError() << "This zip file contains files compressed with method"
1405  << encoding() << ", this method is currently not supported by KZip,"
1406  << "please use a command-line tool to handle this file.";
1407  return 0L;
1408 }
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:1276
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:1362
kError
static QDebug kError(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
Definition: kdebug.h:187
QList< KZipFileEntry * >
KZipFileEntry::data
virtual QByteArray data() const
Definition: kzip.cpp:1372
KZipFileEntry::headerStart
qint64 headerStart() const
Definition: kzip.cpp:1352
QString
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:1357
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:1347
KZip::closeArchive
virtual bool closeArchive()
Closes the archive.
Definition: kzip.cpp:811
kzip.h
KZipFileEntry::compressedSize
qint64 compressedSize() const
Definition: kzip.cpp:1337
KZipFileEntry::path
const QString & path() const
Name with complete path - KArchiveFile::name() is the filename only (no path)
Definition: kzip.cpp:1367
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:1222
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:1254
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
handlePossibleHeaderBegin
static bool handlePossibleHeaderBegin(const char *buffer, QIODevice *dev)
Checks if a token for a central or local header has been found and resets the device to the begin of ...
Definition: kzip.cpp:295
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:1315
KZip::openArchive
virtual bool openArchive(QIODevice::OpenMode mode)
Opens the archive for reading.
Definition: kzip.cpp:413
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:1185
KZip::compression
Compression compression() const
The current compression mode that will be used for new files.
Definition: kzip.cpp:1281
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:395
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
QHash
Definition: ksycocafactory.h:28
seekToNextHeaderToken
static bool seekToNextHeaderToken(QIODevice *dev)
Reads the device forwards from the current pos until a token for a central or local header has been f...
Definition: kzip.cpp:329
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:1286
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:1001
KZip::writeData
virtual bool writeData(const char *data, qint64 size)
Write data to a file that has been created using prepareWriting().
Definition: kzip.cpp:1259
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:405
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:1012
KZipFileEntry::~KZipFileEntry
~KZipFileEntry()
Destructor.
Definition: kzip.cpp:1327
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:1342
KZipFileEntry::encoding
int encoding() const
Definition: kzip.cpp:1332
KZipFileEntry
A KZipFileEntry represents an file in a zip archive.
Definition: kzip.h:170
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:1291
KZipFileEntry::createDevice
virtual QIODevice * createDevice() const
This method returns a QIODevice to read the file contents.
Definition: kzip.cpp:1383
This file is part of the KDE documentation.
Documentation copyright © 1996-2015 The KDE developers.
Generated on Mon Mar 9 2015 12:55:42 by doxygen 1.8.8 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.14.6 API Reference

Skip menu "kdelibs-4.14.6 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