selfextract.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * This code shows how to read a zipfile included in an app's binary.
  3. *
  4. * License: this code is public domain. I make no warranty that it is useful,
  5. * correct, harmless, or environmentally safe.
  6. *
  7. * This particular file may be used however you like, including copying it
  8. * verbatim into a closed-source project, exploiting it commercially, and
  9. * removing any trace of my name from the source (although I hope you won't
  10. * do that). I welcome enhancements and corrections to this file, but I do
  11. * not require you to send me patches if you make changes. This code has
  12. * NO WARRANTY.
  13. *
  14. * Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
  15. * Please see LICENSE.txt in the root of the source tree.
  16. *
  17. * This file was written by Ryan C. Gordon. (icculus@icculus.org).
  18. */
  19. /*
  20. * Compile this program and then attach a .zip file to the end of the
  21. * compiled binary.
  22. *
  23. * On Linux, something like this will build the final binary:
  24. * gcc -o selfextract.tmp selfextract.c -lphysfs && \
  25. * cat selfextract.tmp myzipfile.zip >> selfextract && \
  26. * chmod a+x selfextract && \
  27. * rm -f selfextract.tmp
  28. *
  29. * This may not work on all platforms, and it probably only works with
  30. * .zip files, since they are designed to be appended to another file.
  31. */
  32. #include <stdio.h>
  33. #include "physfs.h"
  34. int main(int argc, char **argv)
  35. {
  36. int rc = 0;
  37. if (!PHYSFS_init(argv[0]))
  38. {
  39. printf("PHYSFS_init() failed: %s\n", PHYSFS_getLastError());
  40. return(42);
  41. } /* if */
  42. rc = PHYSFS_addToSearchPath(argv[0], 0);
  43. if (!rc)
  44. {
  45. printf("Couldn't find self-extract data: %s\n", PHYSFS_getLastError());
  46. printf("This might mean you didn't append a zipfile to the binary.\n");
  47. return(42);
  48. } /* if */
  49. char **files = PHYSFS_enumerateFiles("/");
  50. char **i;
  51. for (i = files; *i != NULL; i++)
  52. {
  53. const char *dirorfile = PHYSFS_isDirectory(*i) ? "Directory" : "File";
  54. printf(" * %s '%s' is in root of attached data.\n", dirorfile, *i);
  55. } /* for */
  56. PHYSFS_freeList(files);
  57. return(0);
  58. } /* main */