用libzip解压缩一个文件

时间:2012-10-17 17:57:26

标签: c eclipse extract

我需要创建一个应用程序来从zip存档中提取一个文件,之后我想为Android编译它。

我正在使用Ubuntu,预先安装了libzip-0.10.1。 我在Eclipse中创建了C项目,添加了包含路径并找到了用于提取文件的简单脚本。不幸的是我无法获得以下内容,我可以使用一些建议。

// zip.c file
#include <stdio.h>
#include <stdlib.h>
#include <zip.h>

int main(int argc, char **argv) {
struct zip *zip_file;
struct zip_file *file_in_zip;
int err;
int files_total;
int file_number;
int r;
char buffer[10000];

if (argc < 3) {
    fprintf(stderr,"usage: %s <zipfile> <fileindex>\n",argv[0]);
    return -1;
};

zip_file = zip_open(argv[1], 0, &err);
if (!zip_file) {
    fprintf(stderr,"Error: can't open file %s\n",argv[1]);
    return -1;
};

file_number = atoi(argv[2]);
files_total = zip_get_num_files(zip_file);
if (file_number > files_total) {
    printf("Error: we have only %d files in ZIP\n",files_total);
    return -1;
};

file_in_zip = zip_fopen_index(zip_file, file_number, 0);
if (file_in_zip) {
    while ( (r = zip_fread(file_in_zip, buffer, sizeof(buffer))) > 0) {
        printf("%s",buffer);
    };
    zip_fclose(file_in_zip);
} else {
    fprintf(stderr,"Error: can't open file %d in zip\n",file_number);
};

zip_close(zip_file);

return 0;
};

此外,我添加了几个.h文件以包含我的项目中的目录和几个.c文件到zip.c文件目录。之后所有依赖都很好,但我有一个错误:

‘struct zip’ has no member named ‘default_password’ in file zip_fopen_index.c

文件zip_fopen_index.c是:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

#include "zipint.h"


ZIP_EXTERN struct zip_file *
zip_fopen_index(struct zip *za, zip_uint64_t fileno, int flags)
{
    return zip_fopen_index_encrypted(za, fileno, flags, za->default_password); // error here
}

1 个答案:

答案 0 :(得分:3)

首先请允许我发表一些意见:

Eclipse的程序未编译和链接

编译由编译器完成(gcc使用选项-c):

make all 
Building file: ../zip.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"zip.d" -MT"zip.d" -o "zip.o" "../zip.c"
Finished building: ../zip.c

链接由链接器完成(通过编译器使用选项-o):

Invoking: GCC C Linker
gcc  -o "unzipper"  ./zip.o   
./main.o: In function `zip':
/home/alk/workspace/unzipper/Debug/../zip.c:20: undefined reference to `zip_open'
/home/alk/workspace/unzipper/Debug/../zip.c:27: undefined reference to `zip_get_num_files'
/home/alk/workspace/unzipper/Debug/../zip.c:33: undefined reference to `zip_fopen_index'
/home/alk/workspace/unzipper/Debug/../zip.c:35: undefined reference to `zip_fread'
/home/alk/workspace/unzipper/Debug/../zip.c:38: undefined reference to `zip_fclose'
/home/alk/workspace/unzipper/Debug/../zip.c:43: undefined reference to `zip_close'
collect2: ld returned 1 exit status

Eclipse提供了一个框架,可帮助您管理所有源及其引用,同时还可以编译编译器和链接器任务并设置其选项。

当链接器在程序构建过程中告诉你undefined reference函数zip_*到哪里时,原因是,你错过了告诉链接器(通过编译器,通过Eclipse)可以找到那些zip_*函数。

这些zip_*函数位于库中,即libzip

那么你作为程序员需要告诉链接器(通过编译器,通过Eclipse)将这些函数与编译器从源代码编译的内容链接起来。

结果,链接器能够从已编译的源创建一个可运行的程序以及所需的所有库。默认情况下,Eclipse(以及链接器)知道某些库,例如包含C标准函数的库,即libc

为了让事情顺利进行:

1从项目中删除从libzip图书馆来源中提取的源文件。这些来源已编译到库libzip中,您将在项目中使用它。

2告诉链接器(通过Eclipse)将libzip用于您的项目。

按照以下步骤执行此操作:

打开项目的属性

点击'C / C ++ General'

点击“路径和符号”,在左侧选择“图书馆”标签,点击“添加”并输入zip

最后点击'确定'

3然后尝试构建您的程序:

Building target: unzipper
Invoking: GCC C Linker
gcc  -o "unzipper"  ./zip.o   -lzip
Finished building target: unzipper

(请注意附加选项-lzip!)

如果之前已经正确安装了'libzip'的开发版本,那么你应该没问题。


PS:unzipper是我用于制作示例的Eclispe项目的名称。

PSS:我使用的是Eclipse Juno SR1

相关问题