zip.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <err.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "zip.h"
  5. #include "../util.h"
  6. #include "../archive.h"
  7. static uint8_t
  8. Zip_match(void __unused *self, char *const file)
  9. {
  10. return match_ext(file, EXT(zip)) || match_ext(file, EXT(jar));
  11. }
  12. impl(Match, Zip);
  13. static int
  14. Zip_list(void *self, char *const file)
  15. {
  16. SELF_CFG(self, cfg);
  17. char *const argv[] = { "unzip", "-Z", cfg->verbose ? "-m" : "-1",
  18. "--", file, NULL };
  19. return dryrun_or_exec(cfg->dry_run, argv);
  20. }
  21. impl(List, Zip);
  22. static int
  23. Zip_extract(void *self, char *const file)
  24. {
  25. SELF_CFG(self, cfg);
  26. char *argv[] = { "unzip", !cfg->verbose ? "-q" : NULL,
  27. "-d", cfg->out_dir,
  28. "--", file,
  29. NULL };
  30. fiter_null(WITH_LEN(argv));
  31. return dryrun_or_exec(cfg->dry_run, argv);
  32. }
  33. impl(Extract, Zip);
  34. static int
  35. Zip_create(void *self, char *const archive)
  36. {
  37. SELF_CFG(self, cfg);
  38. char *argv[3 + cfg->files_count + 1];
  39. char **ptr = (char **)argv;
  40. *(ptr++) = "zip";
  41. *(ptr++) = cfg->verbose ? "-r" : "-rq";
  42. *(ptr++) = archive;
  43. for (int i = 0; i < cfg->files_count; i++)
  44. *(ptr++) = cfg->files[i];
  45. *ptr = NULL;
  46. return dryrun_or_exec(cfg->dry_run, argv);
  47. }
  48. impl(Create, Zip);
  49. implExtern(Archive, Zip);