bookunzipper.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "bookunzipper.h"
  2. #include <QDir>
  3. #include "quazip.h"
  4. #include "quazipfile.h"
  5. #include "quazip_global.h"
  6. #include <QImageReader>
  7. #include "bookitemlist.h"
  8. BookUnzipper::BookUnzipper()
  9. {
  10. m_cancel = false;
  11. }
  12. void BookUnzipper::run()
  13. {
  14. int count = 1;
  15. QString text("Opening page %1");
  16. BookItemList::ErrorCodes code = BookItemList::NoError;
  17. QDir path(PRIVATE_BOOKDIR);
  18. path.rmpath(PRIVATE_BOOKDIR);
  19. if(path.mkpath(PRIVATE_BOOKDIR)){
  20. QuaZip zip(m_archiveName);
  21. if(zip.open(QuaZip::mdUnzip)){
  22. int total = zip.getEntriesCount();
  23. QuaZipFile file(&zip);
  24. bool running = zip.goToFirstFile();
  25. while(running){
  26. if(file.open(QIODevice::ReadOnly)){
  27. QFile outfile(PRIVATE_BOOKDIR_EXTRACTED+"/"+zip.getCurrentFileName());
  28. int end = outfile.fileName().lastIndexOf('/');
  29. if(end >0){
  30. QString dname = outfile.fileName().mid(0,end);
  31. QDir d(dname);
  32. d.mkpath(dname);
  33. }
  34. if(outfile.open(QIODevice::ReadWrite)){
  35. qint64 totalbytes = file.bytesAvailable();
  36. qint64 amount = 0;
  37. int block = 4096;
  38. while(amount<totalbytes){
  39. QByteArray arr;
  40. arr = file.read(block);
  41. if(arr.length()>0){
  42. amount+=arr.length();
  43. outfile.write(arr);
  44. outfile.flush();
  45. }
  46. }
  47. }else{
  48. if(QFile::exists(outfile.fileName()) && outfile.size() > 0 )
  49. code = BookItemList::NoError;
  50. else
  51. code = BookItemList::ZipBroken;
  52. }
  53. outfile.close();
  54. }
  55. file.close();
  56. emit this->openedZipPage(count,total,text);
  57. count++;
  58. running = zip.goToNextFile();
  59. if(running && m_cancel){
  60. running = !m_cancel;
  61. }
  62. }
  63. zip.close();
  64. }else{
  65. code = BookItemList::ZipBroken;
  66. }
  67. }else{
  68. code = BookItemList::NotEnoughSpace;
  69. }
  70. emit this->extractFinished(code);
  71. }
  72. void BookUnzipper::setArchiveName(QString text)
  73. {
  74. m_archiveName = text;
  75. }
  76. void BookUnzipper::cancelRun()
  77. {
  78. m_cancel = true;
  79. }