zip.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #include "zip.h"
  2. #include "transform.h"
  3. #include "stopif.h"
  4. int processContent(struct archive *archiveIn, const char *fileMask) {
  5. struct archive_entry *entryIn;
  6. while (archive_read_next_header(archiveIn, &entryIn) == ARCHIVE_OK) {
  7. const char* path = archive_entry_pathname(entryIn);
  8. int64_t size = archive_entry_size(entryIn);
  9. char buf[size];
  10. Stopif(archive_read_data(archiveIn, buf, size) != size, return 0, "Archive entry has no size (%s)!\n", path);
  11. if (strstr(path, fileMask) != NULL) {
  12. XMLBuff *slide = XMLBuffNew();
  13. *slide = (XMLBuff){.data=buf, .size=size, .name=path};
  14. Stopif(!transformXML(slide), return 0, "Can't process file (%s)!\n", path);
  15. XMLBuffFree(slide);
  16. }
  17. }
  18. return 1;
  19. }
  20. int readZIP(char const *infile, const char *fileMask) {
  21. struct archive *archiveIn;
  22. archiveIn = archive_read_new();
  23. archive_read_support_format_zip(archiveIn);
  24. Stopif(archive_read_open_filename(archiveIn, infile, 10240) != ARCHIVE_OK, return 0, "Can't read file %s!\n", infile);
  25. Stopif(!processContent(archiveIn, fileMask), return 0, "Can't process file %s!\n", infile);
  26. Stopif(archive_read_free(archiveIn) != ARCHIVE_OK, return 0, "Can't free %s!\n", infile);
  27. return 1;
  28. }