main.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // unzip test
  3. //
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdint.h>
  8. #include "../src/unzip.h"
  9. #include "bmp_icons.h"
  10. void * myOpen(const char *filename, int32_t *size) {
  11. printf("Attempting to open %s\n", filename);
  12. FILE *f;
  13. size_t filesize;
  14. f = fopen(filename,"r+b");
  15. if (f) {
  16. fseek(f, 0, SEEK_END);
  17. filesize = ftell(f);
  18. fseek(f, 0, SEEK_SET);
  19. *size = (int32_t) filesize;
  20. }
  21. return (void *)f;
  22. } /* myOpen() */
  23. void myClose(void *p) {
  24. ZIPFILE *pzf = (ZIPFILE *)p;
  25. if (pzf) fclose((FILE *)pzf->fHandle);
  26. } /* myClose() */
  27. int32_t myRead(void *p, uint8_t *buffer, int32_t length) {
  28. ZIPFILE *pzf = (ZIPFILE *)p;
  29. if (!pzf) return 0;
  30. return (int32_t)fread(buffer, 1, length, (FILE *)pzf->fHandle);
  31. } /* myRead() */
  32. int32_t mySeek(void *p, int32_t position, int type) {
  33. ZIPFILE *pzf = (ZIPFILE *)p;
  34. if (!pzf) return 0;
  35. return fseek((FILE *)pzf->fHandle, position, type);
  36. }
  37. int main(int argc, const char *argv[])
  38. {
  39. int rc, i;
  40. unzFile zHandle;
  41. char szTemp[1024];
  42. ZIPFILE zpf;
  43. if (argc != 3) // unzip <zip> <file to extract>
  44. {
  45. printf("Usage: unziptest <zip file> <file to unzip within the zip>\n");
  46. return 0;
  47. }
  48. printf("Starting unzip test...reading file %s from zip archive %s\n", argv[2], argv[1]);
  49. // zHandle = unzOpen(argv[1], NULL, 0, &zpf, myOpen, myRead, mySeek, myClose);
  50. zHandle = unzOpen(NULL, bmp_icons, sizeof(bmp_icons), &zpf, NULL, NULL, NULL, NULL);
  51. if (zHandle == NULL) {
  52. printf("Error opening file: %s\n", argv[1]);
  53. return -1;
  54. }
  55. // Display the global comment (if any)
  56. rc = unzGetGlobalComment(zHandle, (char *)szTemp, sizeof(szTemp));
  57. printf("Global Comment: %s\n", szTemp);
  58. // rc = unzLocateFile(zHandle, argv[2], 2);
  59. rc = unzLocateFile(zHandle, "key.bmp", 2);
  60. if (rc != UNZ_OK) /* Report the file not found */
  61. {
  62. printf("file %s not found within archive\n", argv[2]);
  63. unzClose(zHandle);
  64. return -1;
  65. }
  66. rc = unzOpenCurrentFile(zHandle); /* Try to open the file we want */
  67. if (rc != UNZ_OK) {
  68. printf("Error opening file = %d\n", rc);
  69. unzClose(zHandle);
  70. return -1;
  71. }
  72. printf("File located within archive.\n");
  73. rc = 1;
  74. i = 0;
  75. while (rc > 0) {
  76. rc = unzReadCurrentFile(zHandle, szTemp, sizeof(szTemp));
  77. if (rc >= 0) {
  78. i += rc;
  79. } else {
  80. printf("Error reading from file\n");
  81. break;
  82. }
  83. }
  84. printf("Total bytes read = %d\n", i);
  85. rc = unzCloseCurrentFile(zHandle);
  86. unzClose(zHandle);
  87. return 0;
  88. } /* main() */