zipglobal.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /****************************************************************************
  2. ** Filename: zipglobal.cpp
  3. ** Last updated [dd/mm/yyyy]: 06/02/2011
  4. **
  5. ** pkzip 2.0 file compression.
  6. **
  7. ** Some of the code has been inspired by other open source projects,
  8. ** (mainly Info-Zip and Gilles Vollant's minizip).
  9. ** Compression and decompression actually uses the zlib library.
  10. **
  11. ** Copyright (C) 2007-2012 Angius Fabrizio. All rights reserved.
  12. **
  13. ** This file is part of the OSDaB project (http://osdab.42cows.org/).
  14. **
  15. ** This file may be distributed and/or modified under the terms of the
  16. ** GNU General Public License version 2 as published by the Free Software
  17. ** Foundation and appearing in the file LICENSE.GPL included in the
  18. ** packaging of this file.
  19. **
  20. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  21. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  22. **
  23. ** See the file LICENSE.GPL that came with this software distribution or
  24. ** visit http://www.gnu.org/copyleft/gpl.html for GPL licensing information.
  25. **
  26. **********************************************************************/
  27. #include "zipglobal.h"
  28. #if defined(Q_OS_WIN) || defined(Q_OS_WINCE) || defined(Q_OS_LINUX) || defined (Q_OS_MACX)
  29. #define OSDAB_ZIP_HAS_UTC
  30. #include <ctime>
  31. #else
  32. #undef OSDAB_ZIP_HAS_UTC
  33. #endif
  34. #if defined(Q_OS_WIN)
  35. #include <QtCore/qt_windows.h>
  36. #elif defined(Q_OS_LINUX) || defined(Q_OS_MACX)
  37. #include <utime.h>
  38. #endif
  39. OSDAB_BEGIN_NAMESPACE(Zip)
  40. /*! Returns the current UTC offset in seconds unless OSDAB_ZIP_NO_UTC is defined
  41. and method is implemented for the current platform and 0 otherwise.
  42. */
  43. int OSDAB_ZIP_MANGLE(currentUtcOffset)()
  44. {
  45. #if !(!defined OSDAB_ZIP_NO_UTC && defined OSDAB_ZIP_HAS_UTC)
  46. return 0;
  47. #else
  48. time_t curr_time_t;
  49. time(&curr_time_t);
  50. #if defined Q_OS_WIN
  51. struct tm _tm_struct;
  52. struct tm* tm_struct = &_tm_struct;
  53. #else
  54. struct tm* tm_struct = 0;
  55. #endif
  56. #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
  57. // use the reentrant version of localtime() where available
  58. tzset();
  59. tm res;
  60. tm_struct = gmtime_r(&curr_time_t, &res);
  61. #elif defined Q_OS_WIN && !defined Q_CC_MINGW
  62. if (gmtime_s(tm_struct, &curr_time_t))
  63. return 0;
  64. #else
  65. tm_struct = gmtime(&curr_time_t);
  66. #endif
  67. if (!tm_struct)
  68. return 0;
  69. const time_t global_time_t = mktime(tm_struct);
  70. #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
  71. // use the reentrant version of localtime() where available
  72. tm_struct = localtime_r(&curr_time_t, &res);
  73. #elif defined Q_OS_WIN && !defined Q_CC_MINGW
  74. if (localtime_s(tm_struct, &curr_time_t))
  75. return 0;
  76. #else
  77. tm_struct = localtime(&curr_time_t);
  78. #endif
  79. if (!tm_struct)
  80. return 0;
  81. const time_t local_time_t = mktime(tm_struct);
  82. const int utcOffset = - qRound(difftime(global_time_t, local_time_t));
  83. return tm_struct->tm_isdst > 0 ? utcOffset + 3600 : utcOffset;
  84. #endif // No UTC
  85. return 0;
  86. }
  87. QDateTime OSDAB_ZIP_MANGLE(fromFileTimestamp)(const QDateTime& dateTime)
  88. {
  89. #if !defined OSDAB_ZIP_NO_UTC && defined OSDAB_ZIP_HAS_UTC
  90. const int utc = OSDAB_ZIP_MANGLE(currentUtcOffset)();
  91. return dateTime.toUTC().addSecs(utc);
  92. #else
  93. return dateTime;
  94. #endif // OSDAB_ZIP_NO_UTC
  95. }
  96. bool OSDAB_ZIP_MANGLE(setFileTimestamp)(const QString& fileName, const QDateTime& dateTime)
  97. {
  98. if (fileName.isEmpty())
  99. return true;
  100. #ifdef Q_OS_WIN
  101. HANDLE hFile = CreateFile(fileName.toStdWString().c_str(),
  102. GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
  103. if (hFile == INVALID_HANDLE_VALUE) {
  104. return false;
  105. }
  106. SYSTEMTIME st;
  107. FILETIME ft, ftLastMod;
  108. const QDate date = dateTime.date();
  109. const QTime time = dateTime.time();
  110. st.wYear = date.year();
  111. st.wMonth = date.month();
  112. st.wDay = date.day();
  113. st.wHour = time.hour();
  114. st.wMinute = time.minute();
  115. st.wSecond = time.second();
  116. st.wMilliseconds = time.msec();
  117. SystemTimeToFileTime(&st, &ft);
  118. LocalFileTimeToFileTime(&ft, &ftLastMod);
  119. const bool success = SetFileTime(hFile, NULL, NULL, &ftLastMod);
  120. CloseHandle(hFile);
  121. return success;
  122. #elif defined(Q_OS_LINUX) || defined(Q_OS_MACX)
  123. struct utimbuf t_buffer;
  124. t_buffer.actime = t_buffer.modtime = dateTime.toTime_t();
  125. return utime(fileName.toLocal8Bit().constData(), &t_buffer) == 0;
  126. #endif
  127. return true;
  128. }
  129. OSDAB_END_NAMESPACE