123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- #include <unzipLIB.h>
- #include <SD.h>
- UNZIP zip;
- void setup() {
- Serial.begin(115200);
- while (!Serial && millis() < 3000);
- Serial.println("Search for ZIP files on the SD card");
- while (!SD.begin(4)) {
- Serial.println("Unable to access SD Card");
- delay(1000);
- }
- }
- static File myfile;
- void * myOpen(const char *filename, int32_t *size) {
- myfile = SD.open(filename);
- *size = myfile.size();
- return (void *)&myfile;
- }
- void myClose(void *p) {
- ZIPFILE *pzf = (ZIPFILE *)p;
- File *f = (File *)pzf->fHandle;
- if (f) f->close();
- }
- int32_t myRead(void *p, uint8_t *buffer, int32_t length) {
- ZIPFILE *pzf = (ZIPFILE *)p;
- File *f = (File *)pzf->fHandle;
- return f->read(buffer, length);
- }
- int32_t mySeek(void *p, int32_t position, int iType) {
- ZIPFILE *pzf = (ZIPFILE *)p;
- File *f = (File *)pzf->fHandle;
- if (iType == SEEK_SET)
- return f->seek(position);
- else if (iType == SEEK_END) {
- return f->seek(position + pzf->iSize);
- } else {
- long l = f->position();
- return f->seek(l + position);
- }
- }
- void loop() {
- int rc, filecount = 0;
- char szComment[256], szName[256];
- unz_file_info fi;
-
- File dir = SD.open("/");
- while (true) {
- File entry = dir.openNextFile();
- if (!entry) break;
- if (entry.isDirectory() == false) {
- const char *name = entry.name();
- const int len = strlen(name);
- if (len > 3 && strcmp(name + len - 3, "ZIP") == 0) {
- Serial.print("File: ");
- Serial.println(name);
- rc = rc = zip.openZIP(name, myOpen, myClose, myRead, mySeek);
- if (rc == UNZ_OK) {
- Serial.print("found zip file: ");
- Serial.println(name);
-
- rc = zip.getGlobalComment(szComment, sizeof(szComment));
- Serial.print("Global comment: ");
- Serial.println(szComment);
- Serial.println("Files in this archive:");
- zip.gotoFirstFile();
- rc = UNZ_OK;
- while (rc == UNZ_OK) {
- rc = zip.getFileInfo(&fi, szName, sizeof(szName), NULL, 0, szComment, sizeof(szComment));
- if (rc == UNZ_OK) {
- Serial.print(szName);
- Serial.print(" - ");
- Serial.print(fi.compressed_size, DEC);
- Serial.print("/");
- Serial.println(fi.uncompressed_size, DEC);
- }
- rc = zip.gotoNextFile();
- }
- zip.closeZIP();
- }
- filecount = filecount + 1;
- }
- }
- entry.close();
- }
- if (filecount == 0) {
- Serial.println("No .ZIP files found");
- }
- delay(2000);
- }
|