unzip_sdcard.ino 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // ZIP library example sketch
  3. //
  4. // Written by Larry Bank
  5. // June 9, 2021
  6. //
  7. // This example shows how to do the following:
  8. // - Search an SD card for ZIP files
  9. // - Step through all of the files in a ZIP archive (if found)
  10. // - Display the name and compressed/uncompressed size of each file in the zip archive
  11. // - Write the code for the open/close/read/seek callback functions to use any media type
  12. //
  13. #include <unzipLIB.h>
  14. #include <SD.h>
  15. UNZIP zip; // statically allocate the UNZIP structure (41K)
  16. void setup() {
  17. Serial.begin(115200);
  18. while (!Serial && millis() < 3000); // wait up to 3 seconds for Arduino Serial Monitor
  19. Serial.println("Search for ZIP files on the SD card");
  20. while (!SD.begin(4/*BUILTIN_SDCARD*/)) { // change this to the appropriate value for your setup
  21. Serial.println("Unable to access SD Card");
  22. delay(1000);
  23. }
  24. }
  25. // Functions to access a file on the SD card
  26. static File myfile;
  27. //
  28. // Callback functions needed by the unzipLIB to access a file system
  29. // The library has built-in code for memory-to-memory transfers, but needs
  30. // these callback functions to allow using other storage media
  31. //
  32. void * myOpen(const char *filename, int32_t *size) {
  33. myfile = SD.open(filename);
  34. *size = myfile.size();
  35. return (void *)&myfile;
  36. }
  37. void myClose(void *p) {
  38. ZIPFILE *pzf = (ZIPFILE *)p;
  39. File *f = (File *)pzf->fHandle;
  40. if (f) f->close();
  41. }
  42. int32_t myRead(void *p, uint8_t *buffer, int32_t length) {
  43. ZIPFILE *pzf = (ZIPFILE *)p;
  44. File *f = (File *)pzf->fHandle;
  45. return f->read(buffer, length);
  46. }
  47. int32_t mySeek(void *p, int32_t position, int iType) {
  48. ZIPFILE *pzf = (ZIPFILE *)p;
  49. File *f = (File *)pzf->fHandle;
  50. if (iType == SEEK_SET)
  51. return f->seek(position);
  52. else if (iType == SEEK_END) {
  53. return f->seek(position + pzf->iSize);
  54. } else { // SEEK_CUR
  55. long l = f->position();
  56. return f->seek(l + position);
  57. }
  58. }
  59. // Main loop, scan for all .PNG files on the card and display them
  60. void loop() {
  61. int rc, filecount = 0;
  62. char szComment[256], szName[256];
  63. unz_file_info fi;
  64. File dir = SD.open("/");
  65. while (true) {
  66. File entry = dir.openNextFile();
  67. if (!entry) break;
  68. if (entry.isDirectory() == false) {
  69. const char *name = entry.name();
  70. const int len = strlen(name);
  71. if (len > 3 && strcmp(name + len - 3, "ZIP") == 0) {
  72. Serial.print("File: ");
  73. Serial.println(name);
  74. rc = rc = zip.openZIP(name, myOpen, myClose, myRead, mySeek);
  75. if (rc == UNZ_OK) {
  76. Serial.print("found zip file: ");
  77. Serial.println(name);
  78. // Display the global comment and all of the filenames within
  79. rc = zip.getGlobalComment(szComment, sizeof(szComment));
  80. Serial.print("Global comment: ");
  81. Serial.println(szComment);
  82. Serial.println("Files in this archive:");
  83. zip.gotoFirstFile();
  84. rc = UNZ_OK;
  85. while (rc == UNZ_OK) { // Display all files contained in the archive
  86. rc = zip.getFileInfo(&fi, szName, sizeof(szName), NULL, 0, szComment, sizeof(szComment));
  87. if (rc == UNZ_OK) {
  88. Serial.print(szName);
  89. Serial.print(" - ");
  90. Serial.print(fi.compressed_size, DEC);
  91. Serial.print("/");
  92. Serial.println(fi.uncompressed_size, DEC);
  93. }
  94. rc = zip.gotoNextFile();
  95. } // while more files...
  96. zip.closeZIP();
  97. }
  98. filecount = filecount + 1;
  99. }
  100. }
  101. entry.close();
  102. }
  103. if (filecount == 0) {
  104. Serial.println("No .ZIP files found");
  105. }
  106. delay(2000);
  107. }