cmd_minizip.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <stdlib.h>
  2. #include <common.h>
  3. #include <command.h>
  4. #include <mapmem.h>
  5. #include <fat.h>
  6. #include <fs.h>
  7. #include <part.h>
  8. #include <unzip.h>
  9. #include <fs.h>
  10. static int do_zipread(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  11. {
  12. const char *usb_path = "/usb"; // U 盘挂载路径
  13. const char *zipname = (argc > 3) ? argv[1] : "images_test.zip";// ZIP 文件名
  14. const char *target_file = (argc > 4) ? argv[2] : "zImage";
  15. unzFile zipfile;
  16. unz_file_info file_info;
  17. char filename_inzip[256];
  18. void *buf;
  19. loff_t file_size;
  20. int err;
  21. printf(">>>>>>>>>>%s,%d\n",__func__,__LINE__);
  22. // 挂载 U 盘(假设为第一个 USB 设备)
  23. if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_FAT)) {
  24. printf("Failed to set block device\n");
  25. return CMD_RET_FAILURE;
  26. }
  27. printf(">>>>>>>>>>%s,%d, %s\n",__func__,__LINE__,zipname);
  28. // 检查ZIP文件是否存在
  29. if (fs_size(argv[3], &file_size) < 0) {
  30. printf("ZIP file not found: %s\n", zipname);
  31. return CMD_RET_FAILURE;
  32. }
  33. printf(">>>>>>>>>>%s,%d,file_size 0x%x\n",__func__,__LINE__,file_size);
  34. return 0;
  35. // 打开 ZIP 文件
  36. char fullpath[512];
  37. // snprintf(fullpath, sizeof(fullpath), "%s/%s", usb_path, zipname);
  38. snprintf(fullpath, sizeof(fullpath), "/%s",zipname);
  39. printf(">>>>>>>>>>%s,%d,fullpath %s\n",__func__,__LINE__,fullpath);
  40. zipfile = unzOpen(fullpath);
  41. if (!zipfile) {
  42. printf("Cannot open ZIP: %s\n", fullpath);
  43. return CMD_RET_FAILURE;
  44. }
  45. printf(">>>>>>>>>>%s,%d\n",__func__,__LINE__);
  46. // 定位到目标文件
  47. err = unzLocateFile(zipfile, target_file, 0);
  48. if (err != UNZ_OK) {
  49. printf("File %s not found\n", target_file);
  50. unzClose(zipfile);
  51. return CMD_RET_FAILURE;
  52. }
  53. printf(">>>>>>>>>>%s,%d\n",__func__,__LINE__);
  54. // 获取文件信息并申请内存
  55. unzGetCurrentFileInfo(zipfile, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0);
  56. buf = malloc(file_info.uncompressed_size);
  57. if (!buf) {
  58. printf("Out of memory\n");
  59. unzClose(zipfile);
  60. return CMD_RET_FAILURE;
  61. }
  62. printf(">>>>>>>>>>%s,%d\n",__func__,__LINE__);
  63. // 读取文件内容
  64. unzOpenCurrentFile(zipfile);
  65. int read_size = unzReadCurrentFile(zipfile, buf, file_info.uncompressed_size);
  66. unzCloseCurrentFile(zipfile);
  67. if (read_size < 0) {
  68. printf("Read error: %d\n", read_size);
  69. free(buf);
  70. unzClose(zipfile);
  71. return CMD_RET_FAILURE;
  72. }
  73. // 打印内容(示例)
  74. printf("File content:\n%.*s\n", read_size, (char *)buf);
  75. // 清理
  76. free(buf);
  77. unzClose(zipfile);
  78. return CMD_RET_SUCCESS;
  79. }
  80. U_BOOT_CMD(
  81. zipread, 5, 1, do_zipread,
  82. "Read a file from ZIP on USB",
  83. "<zipname> <filename> - Read file from ZIP"
  84. );