| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #include <stdlib.h>
- #include <common.h>
- #include <command.h>
- #include <mapmem.h>
- #include <fat.h>
- #include <fs.h>
- #include <part.h>
- #include <unzip.h>
- #include <fs.h>
- static int do_zipread(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
- {
- const char *usb_path = "/usb"; // U 盘挂载路径
- const char *zipname = (argc > 3) ? argv[1] : "images_test.zip";// ZIP 文件名
- const char *target_file = (argc > 4) ? argv[2] : "zImage";
- unzFile zipfile;
- unz_file_info file_info;
- char filename_inzip[256];
- void *buf;
- loff_t file_size;
- int err;
- printf(">>>>>>>>>>%s,%d\n",__func__,__LINE__);
- // 挂载 U 盘(假设为第一个 USB 设备)
- if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_FAT)) {
- printf("Failed to set block device\n");
- return CMD_RET_FAILURE;
- }
- printf(">>>>>>>>>>%s,%d, %s\n",__func__,__LINE__,zipname);
- // 检查ZIP文件是否存在
- if (fs_size(argv[3], &file_size) < 0) {
- printf("ZIP file not found: %s\n", zipname);
- return CMD_RET_FAILURE;
- }
- printf(">>>>>>>>>>%s,%d,file_size 0x%x\n",__func__,__LINE__,file_size);
- return 0;
- // 打开 ZIP 文件
- char fullpath[512];
- // snprintf(fullpath, sizeof(fullpath), "%s/%s", usb_path, zipname);
- snprintf(fullpath, sizeof(fullpath), "/%s",zipname);
- printf(">>>>>>>>>>%s,%d,fullpath %s\n",__func__,__LINE__,fullpath);
- zipfile = unzOpen(fullpath);
- if (!zipfile) {
- printf("Cannot open ZIP: %s\n", fullpath);
- return CMD_RET_FAILURE;
- }
- printf(">>>>>>>>>>%s,%d\n",__func__,__LINE__);
- // 定位到目标文件
- err = unzLocateFile(zipfile, target_file, 0);
- if (err != UNZ_OK) {
- printf("File %s not found\n", target_file);
- unzClose(zipfile);
- return CMD_RET_FAILURE;
- }
- printf(">>>>>>>>>>%s,%d\n",__func__,__LINE__);
- // 获取文件信息并申请内存
- unzGetCurrentFileInfo(zipfile, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0);
- buf = malloc(file_info.uncompressed_size);
- if (!buf) {
- printf("Out of memory\n");
- unzClose(zipfile);
- return CMD_RET_FAILURE;
- }
- printf(">>>>>>>>>>%s,%d\n",__func__,__LINE__);
- // 读取文件内容
- unzOpenCurrentFile(zipfile);
- int read_size = unzReadCurrentFile(zipfile, buf, file_info.uncompressed_size);
- unzCloseCurrentFile(zipfile);
- if (read_size < 0) {
- printf("Read error: %d\n", read_size);
- free(buf);
- unzClose(zipfile);
- return CMD_RET_FAILURE;
- }
- // 打印内容(示例)
- printf("File content:\n%.*s\n", read_size, (char *)buf);
- // 清理
- free(buf);
- unzClose(zipfile);
- return CMD_RET_SUCCESS;
- }
- U_BOOT_CMD(
- zipread, 5, 1, do_zipread,
- "Read a file from ZIP on USB",
- "<zipname> <filename> - Read file from ZIP"
- );
|