zip_demo.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*================================================================
  2. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  3. Quectel Wireless Solution Proprietary and Confidential.
  4. =================================================================*/
  5. /*=================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ------------ ------- -------------------------------------------------------------------------------
  11. =================================================================*/
  12. /*===========================================================================
  13. * include files
  14. ===========================================================================*/
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include "ql_api_osi.h"
  19. #include "ql_log.h"
  20. #include "zip_demo.h"
  21. #include "ql_zip.h"
  22. #include "ql_fs.h"
  23. /*===========================================================================
  24. * Macro Definition
  25. ===========================================================================*/
  26. #define QL_ZIPDEMO_LOG_LEVEL QL_LOG_LEVEL_INFO
  27. #define QL_ZIPDEMO_LOG(msg, ...) QL_LOG(QL_ZIPDEMO_LOG_LEVEL, "ql_ZIPDEMO", msg, ##__VA_ARGS__)
  28. #define QL_ZIPDEMO_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_ZIPDEMO", msg, ##__VA_ARGS__)
  29. #define ZIP_FILE_LEN 1024
  30. /*===========================================================================
  31. * Variate
  32. ===========================================================================*/
  33. const char *unZipFilePath="UFS:test.zip";
  34. char *unZip_path=NULL;
  35. const char *TestFilePath[2]={"UFS:test01.txt","UFS:test02.txt"};
  36. const char *TestFilezipname[2]={"test01.txt","test02.txt"};
  37. int unzip_result=QL_FILE_OK;
  38. int dl_size;
  39. int upl_size;
  40. ql_task_t zip_task = NULL;
  41. /*===========================================================================
  42. * Functions
  43. ===========================================================================*/
  44. static void ql_zip_demo_thread(void *param)
  45. {
  46. int ret = 0;
  47. int run_num=0;
  48. HZIP hz=0;
  49. QFILE fd[2] = {0};
  50. unsigned char *zip_fs_space=calloc(10,ZIP_FILE_LEN);
  51. if(NULL==zip_fs_space)
  52. {
  53. QL_ZIPDEMO_LOG("malloc err\r\n");
  54. return;
  55. }
  56. ql_rtos_task_sleep_s(5);
  57. QL_ZIPDEMO_LOG("========== zip demo start ==========\r\n");
  58. QL_ZIPDEMO_LOG("========== start create Test Files:[%s][%s]==========\r\n",TestFilePath[0],TestFilePath[1]);
  59. fd[0] = ql_fopen(TestFilePath[0],"w+");
  60. fd[1] = ql_fopen(TestFilePath[1],"w+");
  61. if(fd[0] < 0||fd[1] < 0)
  62. {
  63. QL_ZIPDEMO_LOG("open file failed !\r\n");
  64. QL_ZIPDEMO_LOG("========== Create Test Files:[%s][%s] fail==========\r\n",TestFilePath[0],TestFilePath[1]);
  65. goto main_exit;
  66. }
  67. else
  68. {
  69. QL_ZIPDEMO_LOG("open file:[%d][%d] success!\r\n",fd[0],fd[1]);
  70. }
  71. memset(zip_fs_space,'a',10*ZIP_FILE_LEN);
  72. ret = ql_fwrite(zip_fs_space, 10*ZIP_FILE_LEN, 1, fd[0]);
  73. if(ret > 0)
  74. {
  75. QL_ZIPDEMO_LOG("file:test01.txt write :%d bytes data\r\n", ret);
  76. }
  77. else
  78. {
  79. ql_fclose(fd[0]);
  80. ql_fclose(fd[1]);
  81. QL_ZIPDEMO_LOG("file:test01.txt write Failed\r\n");
  82. QL_ZIPDEMO_LOG("========== Create Test Files:[%s][%s] Failed==========\r\n",TestFilePath[0],TestFilePath[1]);
  83. goto main_exit;
  84. }
  85. memset(zip_fs_space,'b',5*ZIP_FILE_LEN);
  86. ret = ql_fwrite(zip_fs_space, 5*ZIP_FILE_LEN, 1, fd[1]);
  87. if(ret > 0)
  88. {
  89. QL_ZIPDEMO_LOG("file:test02.txt write :%d bytes data\r\n", ret);
  90. }
  91. else
  92. {
  93. ql_fclose(fd[0]);
  94. ql_fclose(fd[1]);
  95. QL_ZIPDEMO_LOG("file:test02.txt write Failed\r\n");
  96. QL_ZIPDEMO_LOG("========== Create Test Files:[%s][%s] Failed==========\r\n",TestFilePath[0],TestFilePath[1]);
  97. goto main_exit;
  98. }
  99. ql_fclose(fd[0]);
  100. ql_fclose(fd[1]);
  101. QL_ZIPDEMO_LOG("========== Create Test Files:[%s][%s] success==========\r\n",TestFilePath[0],TestFilePath[1]);
  102. ql_rtos_task_sleep_s(5);
  103. for(run_num=0;run_num<50;run_num++)
  104. {
  105. QL_ZIPDEMO_LOG("========== loop_test[%d] start==========\r\n",run_num+1);
  106. QL_ZIPDEMO_LOG("========== start zip file[%d]==========\r\n",run_num+1);
  107. hz = ql_fs_zip_create(unZipFilePath,0,&ret);
  108. if (ret<0)
  109. {
  110. QL_ZIPDEMO_LOG("create empty.zip:[%s] Failed,errcode=%d\r\n",unZipFilePath,ret);
  111. goto main_exit;
  112. }
  113. else
  114. {
  115. QL_ZIPDEMO_LOG("create empty.zip:[%s] Success\r\n",unZipFilePath);
  116. }
  117. ret=ql_fs_zip_add_file(hz,TestFilezipname[0],TestFilePath[0]);
  118. if (ret!=QL_FILE_OK)
  119. {
  120. QL_ZIPDEMO_LOG("add [%s] to zip Failed\r\n",TestFilePath[0]);
  121. goto main_exit;
  122. }
  123. QL_ZIPDEMO_LOG("add [%s] to zip Success\r\n",TestFilePath[0]);
  124. ret=ql_fs_zip_add_file(hz,TestFilezipname[1],TestFilePath[1]);
  125. if (ret!=QL_FILE_OK)
  126. {
  127. QL_ZIPDEMO_LOG("add [%s] to zip Failed\r\n",TestFilePath[1]);
  128. goto main_exit;
  129. }
  130. QL_ZIPDEMO_LOG("add [%s] to zip Success\r\n",TestFilePath[1]);
  131. ql_fs_zip_close(hz);
  132. #if 0
  133. //解压到内存
  134. ql_rtos_task_sleep_s(5);
  135. ql_unzip_mem_data_s *ql_unzip_tmp = (ql_unzip_mem_data_s *)calloc(1, sizeof(ql_unzip_mem_data_s));
  136. if (ql_unzip_tmp == NULL)
  137. {
  138. QL_ZIPDEMO_LOG("malloc err");
  139. goto main_exit;
  140. }
  141. ql_unzip_tmp->data = (void *)calloc(1, 15*ZIP_FILE_LEN);
  142. if (ql_unzip_tmp->data == NULL)
  143. {
  144. QL_ZIPDEMO_LOG("malloc err");
  145. free(ql_unzip_tmp);
  146. goto main_exit;
  147. }
  148. ql_unzip_tmp->total_len = 15*ZIP_FILE_LEN;
  149. ret = ql_fs_unzip_ex(unZipFilePath, QL_UNZIP_TO_MEMERY, ql_unzip_tmp);
  150. if(ret!=QL_FILE_OK)
  151. {
  152. QL_ZIPDEMO_LOG("unzip file:[%s] Failed",unZipFilePath);
  153. free(ql_unzip_tmp->data);
  154. free(ql_unzip_tmp);
  155. goto main_exit;
  156. }
  157. QL_ZIPDEMO_LOG("used_len=%d,item_num=%d",ql_unzip_tmp->used_len, ql_unzip_tmp->item_num);
  158. int j=0;
  159. int i = 0;
  160. char *tmep = NULL;
  161. for (j=0; j<ql_unzip_tmp->item_num; j++)
  162. {
  163. QL_ZIPDEMO_LOG("file path=%s", ql_unzip_tmp->item[j].file_path);
  164. QL_ZIPDEMO_LOG("file len=%d,type=%d", ql_unzip_tmp->item[j].item_len, ql_unzip_tmp->item[j].file_type);
  165. tmep = (char *)ql_unzip_tmp->item[j].item_data;
  166. if (tmep && ql_unzip_tmp->item[j].file_type == QL_UNZIP_FILE)
  167. {
  168. if (0 == strcmp(ql_unzip_tmp->item[j].file_path, TestFilePath[0]))
  169. {
  170. for (i=0; i<ql_unzip_tmp->item[j].item_len; i++)
  171. {
  172. if (tmep[i] != 'a')
  173. {
  174. QL_ZIPDEMO_LOG("not match -----------");
  175. break;
  176. }
  177. }
  178. }
  179. if (0 == strcmp(ql_unzip_tmp->item[j].file_path, TestFilePath[1]))
  180. {
  181. for (i=0; i<ql_unzip_tmp->item[j].item_len; i++)
  182. {
  183. if (tmep[i] != 'b')
  184. {
  185. QL_ZIPDEMO_LOG("not match -----------");
  186. break;
  187. }
  188. }
  189. }
  190. }
  191. }
  192. free(ql_unzip_tmp->data);
  193. free(ql_unzip_tmp);
  194. QL_ZIPDEMO_LOG("unzip file:[%s] Success\r\n",unZipFilePath);
  195. #else
  196. //解压到文件系统
  197. QL_ZIPDEMO_LOG("========== zip file[%d] success==========\r\n",run_num+1);
  198. ql_rtos_task_sleep_s(5);
  199. QL_ZIPDEMO_LOG("========== start unzip file[%d]==========\r\n",run_num+1);
  200. ret=ql_fs_unzip(unZipFilePath,unZip_path,1);
  201. //ql_unzip_file_data_s ql_unzip_file_tmp = {0};
  202. //ql_unzip_file_tmp.mode = 1;
  203. //ql_unzip_file_tmp.path_name = unZip_path;
  204. //ret = ql_fs_unzip_ex(unZipFilePath, QL_UNZIP_TO_FILE, &ql_unzip_file_tmp);
  205. if(ret!=QL_FILE_OK)
  206. {
  207. QL_ZIPDEMO_LOG("unzip file:[%s] Failed\r\n",unZipFilePath);
  208. goto main_exit;
  209. }
  210. QL_ZIPDEMO_LOG("unzip file:[%s] Success\r\n",unZipFilePath);
  211. #endif
  212. QL_ZIPDEMO_LOG("========== unzip file[%d] success==========\r\n",run_num+1);
  213. ql_rtos_task_sleep_s(5);
  214. }
  215. main_exit:
  216. QL_ZIPDEMO_LOG("========== zip demo end ==========\r\n");
  217. ql_remove(TestFilePath[0]);
  218. ql_remove(TestFilePath[1]);
  219. if(zip_fs_space!=NULL)
  220. {
  221. free(zip_fs_space);
  222. zip_fs_space = NULL;
  223. }
  224. ql_rtos_task_delete(NULL);
  225. return;
  226. }
  227. void ql_zip_app_init(void)
  228. {
  229. QlOSStatus err = QL_OSI_SUCCESS;
  230. err = ql_rtos_task_create(&zip_task, 8*4096, APP_PRIORITY_NORMAL, "ql_zipdemo", ql_zip_demo_thread, NULL, 5);
  231. if( err != QL_OSI_SUCCESS )
  232. {
  233. QL_ZIPDEMO_LOG("zip demo task created failed");
  234. }
  235. }