🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
6.4 KiB

  1. // example2.c - Simple demonstration of miniz.c's ZIP archive API's.
  2. // Note this test deletes the test archive file "__mz_example2_test__.zip" in the current directory, then creates a new one with test data.
  3. // Public domain, May 15 2011, Rich Geldreich, richgel99@gmail.com. See "unlicense" statement at the end of tinfl.c.
  4. #if defined(__GNUC__)
  5. // Ensure we get the 64-bit variants of the CRT's file I/O calls
  6. #ifndef _FILE_OFFSET_BITS
  7. #define _FILE_OFFSET_BITS 64
  8. #endif
  9. #ifndef _LARGEFILE64_SOURCE
  10. #define _LARGEFILE64_SOURCE 1
  11. #endif
  12. #endif
  13. #include <stdio.h>
  14. #include "miniz.h"
  15. typedef unsigned char uint8;
  16. typedef unsigned short uint16;
  17. typedef unsigned int uint;
  18. // The string to compress.
  19. static const char *s_pTest_str =
  20. "MISSION CONTROL I wouldn't worry too much about the computer. First of all, there is still a chance that he is right, despite your tests, and" \
  21. "if it should happen again, we suggest eliminating this possibility by allowing the unit to remain in place and seeing whether or not it" \
  22. "actually fails. If the computer should turn out to be wrong, the situation is still not alarming. The type of obsessional error he may be" \
  23. "guilty of is not unknown among the latest generation of HAL 9000 computers. It has almost always revolved around a single detail, such as" \
  24. "the one you have described, and it has never interfered with the integrity or reliability of the computer's performance in other areas." \
  25. "No one is certain of the cause of this kind of malfunctioning. It may be over-programming, but it could also be any number of reasons. In any" \
  26. "event, it is somewhat analogous to human neurotic behavior. Does this answer your query? Zero-five-three-Zero, MC, transmission concluded.";
  27. static const char *s_pComment = "This is a comment";
  28. int main(int argc, char *argv[])
  29. {
  30. int i, sort_iter;
  31. mz_bool status;
  32. size_t uncomp_size;
  33. mz_zip_archive zip_archive;
  34. void *p;
  35. const int N = 50;
  36. char data[2048];
  37. char archive_filename[64];
  38. static const char *s_Test_archive_filename = "__mz_example2_test__.zip";
  39. assert((strlen(s_pTest_str) + 64) < sizeof(data));
  40. printf("miniz.c version: %s\n", MZ_VERSION);
  41. (void)argc, (void)argv;
  42. // Delete the test archive, so it doesn't keep growing as we run this test
  43. remove(s_Test_archive_filename);
  44. // Append a bunch of text files to the test archive
  45. for (i = (N - 1); i >= 0; --i)
  46. {
  47. sprintf(archive_filename, "%u.txt", i);
  48. sprintf(data, "%u %s %u", (N - 1) - i, s_pTest_str, i);
  49. // Add a new file to the archive. Note this is an IN-PLACE operation, so if it fails your archive is probably hosed (its central directory may not be complete) but it should be recoverable using zip -F or -FF. So use caution with this guy.
  50. // A more robust way to add a file to an archive would be to read it into memory, perform the operation, then write a new archive out to a temp file and then delete/rename the files.
  51. // Or, write a new archive to disk to a temp file, then delete/rename the files. For this test this API is fine.
  52. status = mz_zip_add_mem_to_archive_file_in_place(s_Test_archive_filename, archive_filename, data, strlen(data) + 1, s_pComment, (uint16)strlen(s_pComment), MZ_BEST_COMPRESSION);
  53. if (!status)
  54. {
  55. printf("mz_zip_add_mem_to_archive_file_in_place failed!\n");
  56. return EXIT_FAILURE;
  57. }
  58. }
  59. // Add a directory entry for testing
  60. status = mz_zip_add_mem_to_archive_file_in_place(s_Test_archive_filename, "directory/", NULL, 0, "no comment", (uint16)strlen("no comment"), MZ_BEST_COMPRESSION);
  61. if (!status)
  62. {
  63. printf("mz_zip_add_mem_to_archive_file_in_place failed!\n");
  64. return EXIT_FAILURE;
  65. }
  66. // Now try to open the archive.
  67. memset(&zip_archive, 0, sizeof(zip_archive));
  68. status = mz_zip_reader_init_file(&zip_archive, s_Test_archive_filename, 0);
  69. if (!status)
  70. {
  71. printf("mz_zip_reader_init_file() failed!\n");
  72. return EXIT_FAILURE;
  73. }
  74. // Get and print information about each file in the archive.
  75. for (i = 0; i < (int)mz_zip_reader_get_num_files(&zip_archive); i++)
  76. {
  77. mz_zip_archive_file_stat file_stat;
  78. if (!mz_zip_reader_file_stat(&zip_archive, i, &file_stat))
  79. {
  80. printf("mz_zip_reader_file_stat() failed!\n");
  81. mz_zip_reader_end(&zip_archive);
  82. return EXIT_FAILURE;
  83. }
  84. printf("Filename: \"%s\", Comment: \"%s\", Uncompressed size: %u, Compressed size: %u, Is Dir: %u\n", file_stat.m_filename, file_stat.m_comment, (uint)file_stat.m_uncomp_size, (uint)file_stat.m_comp_size, mz_zip_reader_is_file_a_directory(&zip_archive, i));
  85. if (!strcmp(file_stat.m_filename, "directory/"))
  86. {
  87. if (!mz_zip_reader_is_file_a_directory(&zip_archive, i))
  88. {
  89. printf("mz_zip_reader_is_file_a_directory() didn't return the expected results!\n");
  90. mz_zip_reader_end(&zip_archive);
  91. return EXIT_FAILURE;
  92. }
  93. }
  94. }
  95. // Close the archive, freeing any resources it was using
  96. mz_zip_reader_end(&zip_archive);
  97. // Now verify the compressed data
  98. for (sort_iter = 0; sort_iter < 2; sort_iter++)
  99. {
  100. memset(&zip_archive, 0, sizeof(zip_archive));
  101. status = mz_zip_reader_init_file(&zip_archive, s_Test_archive_filename, sort_iter ? MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY : 0);
  102. if (!status)
  103. {
  104. printf("mz_zip_reader_init_file() failed!\n");
  105. return EXIT_FAILURE;
  106. }
  107. for (i = 0; i < N; i++)
  108. {
  109. sprintf(archive_filename, "%u.txt", i);
  110. sprintf(data, "%u %s %u", (N - 1) - i, s_pTest_str, i);
  111. // Try to extract all the files to the heap.
  112. p = mz_zip_reader_extract_file_to_heap(&zip_archive, archive_filename, &uncomp_size, 0);
  113. if (!p)
  114. {
  115. printf("mz_zip_reader_extract_file_to_heap() failed!\n");
  116. mz_zip_reader_end(&zip_archive);
  117. return EXIT_FAILURE;
  118. }
  119. // Make sure the extraction really succeeded.
  120. if ((uncomp_size != (strlen(data) + 1)) || (memcmp(p, data, strlen(data))))
  121. {
  122. printf("mz_zip_reader_extract_file_to_heap() failed to extract the proper data\n");
  123. mz_free(p);
  124. mz_zip_reader_end(&zip_archive);
  125. return EXIT_FAILURE;
  126. }
  127. printf("Successfully extracted file \"%s\", size %u\n", archive_filename, (uint)uncomp_size);
  128. printf("File data: \"%s\"\n", (const char *)p);
  129. // We're done.
  130. mz_free(p);
  131. }
  132. // Close the archive, freeing any resources it was using
  133. mz_zip_reader_end(&zip_archive);
  134. }
  135. printf("Success.\n");
  136. return EXIT_SUCCESS;
  137. }