testquazipfile.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "testquazipfile.h"
  2. #include "qztest.h"
  3. #include <quazip/jlcompress.h>
  4. #include <quazip/quazipfile.h>
  5. #include <quazip/quazip.h>
  6. #include <QFile>
  7. #include <QString>
  8. #include <QStringList>
  9. #include <QtTest/QtTest>
  10. void TestQuaZipFile::zipUnzip_data()
  11. {
  12. QTest::addColumn<QString>("zipName");
  13. QTest::addColumn<QStringList>("fileNames");
  14. QTest::addColumn<QByteArray>("fileNameCodec");
  15. QTest::newRow("simple") << "simple.zip" << (
  16. QStringList() << "test0.txt" << "testdir1/test1.txt"
  17. << "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt")
  18. << QByteArray();
  19. QTest::newRow("Cyrillic") << "cyrillic.zip" << (
  20. QStringList()
  21. << QString::fromUtf8("русское имя файла с пробелами.txt"))
  22. << QByteArray("IBM866");
  23. }
  24. void TestQuaZipFile::zipUnzip()
  25. {
  26. QFETCH(QString, zipName);
  27. QFETCH(QStringList, fileNames);
  28. QFETCH(QByteArray, fileNameCodec);
  29. QFile testFile(zipName);
  30. if (testFile.exists()) {
  31. if (!testFile.remove()) {
  32. QFAIL("Couldn't remove existing archive to create a new one");
  33. }
  34. }
  35. if (!createTestFiles(fileNames)) {
  36. QFAIL("Couldn't create test files for zipping");
  37. }
  38. QuaZip testZip(&testFile);
  39. if (!fileNameCodec.isEmpty())
  40. testZip.setFileNameCodec(fileNameCodec);
  41. QVERIFY(testZip.open(QuaZip::mdCreate));
  42. QString comment = "Test comment";
  43. testZip.setComment(comment);
  44. foreach (QString fileName, fileNames) {
  45. QFile inFile("tmp/" + fileName);
  46. if (!inFile.open(QIODevice::ReadOnly)) {
  47. qDebug("File name: %s", fileName.toUtf8().constData());
  48. QFAIL("Couldn't open input file");
  49. }
  50. QuaZipFile outFile(&testZip);
  51. QVERIFY(outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(fileName,
  52. inFile.fileName())));
  53. for (qint64 pos = 0, len = inFile.size(); pos < len; ) {
  54. char buf[4096];
  55. qint64 readSize = qMin(static_cast<qint64>(4096), len - pos);
  56. qint64 l;
  57. if ((l = inFile.read(buf, readSize)) != readSize) {
  58. qDebug("Reading %ld bytes from %s at %ld returned %ld",
  59. static_cast<long>(readSize),
  60. fileName.toUtf8().constData(),
  61. static_cast<long>(pos),
  62. static_cast<long>(l));
  63. QFAIL("Read failure");
  64. }
  65. QVERIFY(outFile.write(buf, readSize));
  66. pos += readSize;
  67. }
  68. inFile.close();
  69. outFile.close();
  70. QCOMPARE(outFile.getZipError(), ZIP_OK);
  71. }
  72. testZip.close();
  73. QCOMPARE(testZip.getZipError(), ZIP_OK);
  74. // now test unzip
  75. QuaZip testUnzip(&testFile);
  76. if (!fileNameCodec.isEmpty())
  77. testUnzip.setFileNameCodec(fileNameCodec);
  78. QVERIFY(testUnzip.open(QuaZip::mdUnzip));
  79. QCOMPARE(testUnzip.getComment(), comment);
  80. QVERIFY(testUnzip.goToFirstFile());
  81. foreach (QString fileName, fileNames) {
  82. QCOMPARE(testUnzip.getCurrentFileName(), fileName);
  83. testUnzip.goToNextFile();
  84. }
  85. testUnzip.close();
  86. QCOMPARE(testUnzip.getZipError(), UNZ_OK);
  87. // clean up
  88. removeTestFiles(fileNames);
  89. testFile.remove();
  90. }
  91. void TestQuaZipFile::bytesAvailable_data()
  92. {
  93. QTest::addColumn<QString>("zipName");
  94. QTest::addColumn<QStringList>("fileNames");
  95. QTest::newRow("simple") << "test.zip" << (
  96. QStringList() << "test0.txt" << "testdir1/test1.txt"
  97. << "testdir2/test2.txt" << "testdir2/subdir/test2sub.txt");
  98. }
  99. void TestQuaZipFile::bytesAvailable()
  100. {
  101. QFETCH(QString, zipName);
  102. QFETCH(QStringList, fileNames);
  103. QDir curDir;
  104. if (!createTestFiles(fileNames)) {
  105. QFAIL("Couldn't create test files");
  106. }
  107. if (!JlCompress::compressDir(zipName, "tmp")) {
  108. QFAIL("Couldn't create test archive");
  109. }
  110. QuaZip testZip(zipName);
  111. QVERIFY(testZip.open(QuaZip::mdUnzip));
  112. foreach (QString fileName, fileNames) {
  113. QFileInfo fileInfo("tmp/" + fileName);
  114. QVERIFY(testZip.setCurrentFile(fileName));
  115. QuaZipFile zipFile(&testZip);
  116. QVERIFY(zipFile.open(QIODevice::ReadOnly));
  117. QCOMPARE(zipFile.bytesAvailable(), fileInfo.size());
  118. QCOMPARE(zipFile.read(1).size(), 1);
  119. QCOMPARE(zipFile.bytesAvailable(), fileInfo.size() - 1);
  120. QCOMPARE(zipFile.read(fileInfo.size() - 1).size(),
  121. static_cast<int>(fileInfo.size() - 1));
  122. QCOMPARE(zipFile.bytesAvailable(), (qint64) 0);
  123. }
  124. removeTestFiles(fileNames);
  125. testZip.close();
  126. curDir.remove(zipName);
  127. }
  128. void TestQuaZipFile::atEnd_data()
  129. {
  130. bytesAvailable_data();
  131. }
  132. void TestQuaZipFile::atEnd()
  133. {
  134. QFETCH(QString, zipName);
  135. QFETCH(QStringList, fileNames);
  136. QDir curDir;
  137. if (!createTestFiles(fileNames)) {
  138. QFAIL("Couldn't create test files");
  139. }
  140. if (!JlCompress::compressDir(zipName, "tmp")) {
  141. QFAIL("Couldn't create test archive");
  142. }
  143. QuaZip testZip(zipName);
  144. QVERIFY(testZip.open(QuaZip::mdUnzip));
  145. foreach (QString fileName, fileNames) {
  146. QFileInfo fileInfo("tmp/" + fileName);
  147. QVERIFY(testZip.setCurrentFile(fileName));
  148. QuaZipFile zipFile(&testZip);
  149. QVERIFY(zipFile.open(QIODevice::ReadOnly));
  150. QCOMPARE(zipFile.atEnd(), false);
  151. QCOMPARE(zipFile.read(1).size(), 1);
  152. QCOMPARE(zipFile.atEnd(), false);
  153. QCOMPARE(zipFile.read(fileInfo.size() - 1).size(),
  154. static_cast<int>(fileInfo.size() - 1));
  155. QCOMPARE(zipFile.atEnd(), true);
  156. }
  157. removeTestFiles(fileNames);
  158. testZip.close();
  159. curDir.remove(zipName);
  160. }
  161. void TestQuaZipFile::pos_data()
  162. {
  163. bytesAvailable_data();
  164. }
  165. void TestQuaZipFile::pos()
  166. {
  167. QFETCH(QString, zipName);
  168. QFETCH(QStringList, fileNames);
  169. QDir curDir;
  170. if (!createTestFiles(fileNames)) {
  171. QFAIL("Couldn't create test files");
  172. }
  173. if (!JlCompress::compressDir(zipName, "tmp")) {
  174. QFAIL("Couldn't create test archive");
  175. }
  176. QuaZip testZip(zipName);
  177. QVERIFY(testZip.open(QuaZip::mdUnzip));
  178. foreach (QString fileName, fileNames) {
  179. QFileInfo fileInfo("tmp/" + fileName);
  180. QVERIFY(testZip.setCurrentFile(fileName));
  181. QuaZipFile zipFile(&testZip);
  182. QVERIFY(zipFile.open(QIODevice::ReadOnly));
  183. QCOMPARE(zipFile.pos(), (qint64) 0);
  184. QCOMPARE(zipFile.read(1).size(), 1);
  185. QCOMPARE(zipFile.pos(), (qint64) 1);
  186. QCOMPARE(zipFile.read(fileInfo.size() - 1).size(),
  187. static_cast<int>(fileInfo.size() - 1));
  188. QCOMPARE(zipFile.pos(), fileInfo.size());
  189. }
  190. removeTestFiles(fileNames);
  191. testZip.close();
  192. curDir.remove(zipName);
  193. }