下载zlib source code,官网能直接下载,也可以本站附件下载。
解压编译安装zlib。
tar -xzvf zlib-1.2.13.tar.gz cd zlib-1.2.13 export CC=your_gcc_path/riscv64-linux-gcc ./configure --prefix=/opt --shar make
使用minizip
上面的源码目录contrib\minizip即为minizip,是zip和unzip功能最小化实现。
打开Makefile文件,我们可以看到,这个目录下最终编译出来两个工具,一个是miniunz,一个是minizip。一个是zip的解压缩工具,一个文件的zip压缩工具。
CC=your_gcc_path/riscv64-linux-gcc CFLAGS := $(CFLAGS) -O -I../.. UNZ_OBJS = miniunz.o unzip.o ioapi.o ../../libz.a ZIP_OBJS = minizip.o zip.o ioapi.o ../../libz.a .c.o: $(CC) -c $(CFLAGS) $*.c all: miniunz minizip miniunz: $(UNZ_OBJS) $(CC) $(CFLAGS) -o $@ $(UNZ_OBJS) minizip: $(ZIP_OBJS) $(CC) $(CFLAGS) -o $@ $(ZIP_OBJS) test: miniunz minizip @rm -f test.* @echo hello hello hello > test.txt ./minizip test test.txt ./miniunz -l test.zip @mv test.txt test.old ./miniunz test.zip @cmp test.txt test.old @rm -f test.* clean: /bin/rm -f *.o *~ minizip miniunz test.*
make就会生成minizip和miniunz两个程序,自行测试两个程序。
只保留解压功能
修改miniunz.c
运行验证,结果完美。☺☺☺
int unzip(const char* zipfilename)
{
    const char *filename_to_extract=NULL;
    const char *password=NULL;
    char filename_try[MAXFILENAME+16] = "";
    int ret_value=0;
    int opt_do_list=0;
    int opt_do_extract=1;
    int opt_do_extract_withoutpath=0;
    int opt_overwrite=1;
    int opt_extractdir=0;
    const char *dirname=NULL;
    unzFile uf=NULL;
    do_banner();
    if (zipfilename!=NULL)
    {
#        ifdef USEWIN32IOAPI
        zlib_filefunc64_def ffunc;
#        endif
        strncpy(filename_try, zipfilename,MAXFILENAME-1);
        /* strncpy doesnt append the trailing NULL, of the string is too long. */
        filename_try[ MAXFILENAME ] = '\0';
#        ifdef USEWIN32IOAPI
        fill_win32_filefunc64A(&ffunc);
        uf = unzOpen2_64(zipfilename,&ffunc);
#        else
        uf = unzOpen64(zipfilename);
#        endif
        if (uf==NULL)
        {
            strcat(filename_try,".zip");
#            ifdef USEWIN32IOAPI
            uf = unzOpen2_64(filename_try,&ffunc);
#            else
            uf = unzOpen64(filename_try);
#            endif
        }
    }
    if (uf==NULL)
    {
        printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
        return 1;
    }
    printf("%s opened\n",filename_try);
    if (opt_do_list==1)
        ret_value = do_list(uf);
    else if (opt_do_extract==1)
    {
#ifdef _WIN32
        if (opt_extractdir && _chdir(dirname))
#else
        if (opt_extractdir && chdir(dirname))
#endif
        {
          printf("Error changing into %s, aborting\n", dirname);
          exit(-1);
        }
        if (filename_to_extract == NULL)
            ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password);
        else
            ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password);
    }
    unzClose(uf);
    return ret_value;
} 收藏的用户(0) X 
 正在加载信息~
 推荐阅读
  最新回复 (0) 
 站点信息
 -  文章2313
 -  用户1336
 -  访客11760180
 
 每日一句 
 Pride in your steps to dreams.
为追梦的每一步而自豪。
 为追梦的每一步而自豪。
新会员