First of all allow me some comments:

Your program is not compiled and linked by Eclipse.

Compiling is done by the compiler (gcc using option -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

Linking is done by the linker (via the compiler using option -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 provides a framework helping you in managing all sources and their references as also spawing compiler and linker tasks and setting their options.

When the linker told you there where undefined references to the zip_*function during the build of your program, the cause for this was, you were missing to tell the linker (via the compiler, via Eclipse) where those zip_* functions could be found.

Those zip_* functions are located in a library, namely libzip.

So what you as the programmer need to tell the linker (via the compiler, via Eclipse) is to link those functions against what the compiler compiled from your sources.

As the result the linker is able to create a runnable program from your compiled sources together with all libraries needed. Certain libraries are know to Eclipse (and therfore to the linker) by default, for example the one containing the C standard functions, namely libc.

To get things going:

1 Remove the source files you pulled from the libzip librarie's sources from your project. Those sources had been compiled into the library libzip, which you will use in your project.

2 Tell the linker (via Eclipse) to use libzip for your project.

Do so by following the steps below:

open the project's properties

click 'C/C++ General'

click 'Path and Symbols', on the left select the 'Libraries' tab, there click 'Add' and enter zip

finally click 'OK'

3 Then try to build your program:

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

(Please note additional option -lzip!)

If the developement version of 'libzip' had been installed properly before, you should be fine.


PS: unzipper was the name I used for the Eclispe project to produce the examples.

PSS: I used Eclipse Juno SR1

Answer from alk on Stack Overflow
🌐
GitHub
gist.github.com › mobius › 1759816
using libzip to extract files - gists · GitHub
Or binary file as zip inside zip ... just need to add this before the for loop int iRetPassword = zip_set_default_password(za, "the_password"); printf("password: %i", iRetPassword); I've made a C++/Qt implementation that deals ...
🌐
Libzip
libzip.org › documentation
Documentation · libzip
A C library for reading, creating, and modifying zip archives · Current version is 1.11.4
Top answer
1 of 1
3

First of all allow me some comments:

Your program is not compiled and linked by Eclipse.

Compiling is done by the compiler (gcc using option -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

Linking is done by the linker (via the compiler using option -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 provides a framework helping you in managing all sources and their references as also spawing compiler and linker tasks and setting their options.

When the linker told you there where undefined references to the zip_*function during the build of your program, the cause for this was, you were missing to tell the linker (via the compiler, via Eclipse) where those zip_* functions could be found.

Those zip_* functions are located in a library, namely libzip.

So what you as the programmer need to tell the linker (via the compiler, via Eclipse) is to link those functions against what the compiler compiled from your sources.

As the result the linker is able to create a runnable program from your compiled sources together with all libraries needed. Certain libraries are know to Eclipse (and therfore to the linker) by default, for example the one containing the C standard functions, namely libc.

To get things going:

1 Remove the source files you pulled from the libzip librarie's sources from your project. Those sources had been compiled into the library libzip, which you will use in your project.

2 Tell the linker (via Eclipse) to use libzip for your project.

Do so by following the steps below:

open the project's properties

click 'C/C++ General'

click 'Path and Symbols', on the left select the 'Libraries' tab, there click 'Add' and enter zip

finally click 'OK'

3 Then try to build your program:

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

(Please note additional option -lzip!)

If the developement version of 'libzip' had been installed properly before, you should be fine.


PS: unzipper was the name I used for the Eclispe project to produce the examples.

PSS: I used Eclipse Juno SR1

🌐
Libzip
libzip.org › documentation › libzip.html
libzip · libzip
The zip format requires the use of forward slash (‘/’) as directory separator. Since backslash (‘\’) can be part of a valid file name on Unix systems, libzip does not automatically convert them, even on Windows.

First of all allow me some comments:

Your program is not compiled and linked by Eclipse.

Compiling is done by the compiler (gcc using option -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

Linking is done by the linker (via the compiler using option -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 provides a framework helping you in managing all sources and their references as also spawing compiler and linker tasks and setting their options.

When the linker told you there where undefined references to the zip_*function during the build of your program, the cause for this was, you were missing to tell the linker (via the compiler, via Eclipse) where those zip_* functions could be found.

Those zip_* functions are located in a library, namely libzip.

So what you as the programmer need to tell the linker (via the compiler, via Eclipse) is to link those functions against what the compiler compiled from your sources.

As the result the linker is able to create a runnable program from your compiled sources together with all libraries needed. Certain libraries are know to Eclipse (and therfore to the linker) by default, for example the one containing the C standard functions, namely libc.

To get things going:

1 Remove the source files you pulled from the libzip librarie's sources from your project. Those sources had been compiled into the library libzip, which you will use in your project.

2 Tell the linker (via Eclipse) to use libzip for your project.

Do so by following the steps below:

open the project's properties

click 'C/C++ General'

click 'Path and Symbols', on the left select the 'Libraries' tab, there click 'Add' and enter zip

finally click 'OK'

3 Then try to build your program:

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

(Please note additional option -lzip!)

If the developement version of 'libzip' had been installed properly before, you should be fine.


PS: unzipper was the name I used for the Eclispe project to produce the examples.

PSS: I used Eclipse Juno SR1

Answer from alk on Stack Overflow
🌐
Libzip
libzip.org
libzip - libzip
libzip is fully documented via man pages. HTML versions of the man pages are in the Reference section. Example source code is in the examples and src subdirectories.
🌐
Wikipedia
en.wikipedia.org › wiki › Libzip
libzip - Wikipedia
November 23, 2024 - Since version 1.1, libzip contains ziptool, a tool for modifying zip archives from the command line.
🌐
Libzip
libzip.org › documentation › zip_fopen.html
zip_fopen · libzip
libzip(3), zip_fclose(3), zip_fread(3), zip_fseek(3), zip_get_num_entries(3), zip_name_locate(3), zip_set_default_password(3)
Find elsewhere
🌐
Fossies
fossies.org › linux › libzip › examples › in-memory.c
libzip: examples/in-memory.c | Fossies
May 23, 2025 - 32 */ 33 34 #include <errno.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <sys/stat.h> 39 40 #include <zip.h> 41 42 static int 43 get_data(void **datap, size_t *sizep, const char *archive) { 44 /* example implementation that reads data from file */ 45 struct stat st; 46 FILE *fp; 47 48 if ((fp = fopen(archive, "rb")) == NULL) { 49 if (errno != ENOENT) { 50 fprintf(stderr, "can't open %s: %s\n", archive, strerror(errno)); 51 return -1; 52 } 53 54 *datap = NULL; 55 *sizep = 0; 56 57 return 0; 58 } 59 60 if (fstat(fileno(fp), &st) < 0) { 61 fprintf(stderr, "ca
Top answer
1 of 2
62

zlib handles the deflate compression/decompression algorithm, but there is more than that in a ZIP file.

You can try libzip. It is free, portable and easy to use.

UPDATE: Here I attach quick'n'dirty example of libzip, with all the error controls ommited:

Copy#include <zip.h>

int main()
{
    //Open the ZIP archive
    int err = 0;
    zip *z = zip_open("foo.zip", 0, &err);

    //Search for the file of given name
    const char *name = "file.txt";
    struct zip_stat st;
    zip_stat_init(&st);
    zip_stat(z, name, 0, &st);

    //Alloc memory for its uncompressed contents
    char *contents = new char[st.size];

    //Read the compressed file
    zip_file *f = zip_fopen(z, name, 0);
    zip_fread(f, contents, st.size);
    zip_fclose(f);

    //And close the archive
    zip_close(z);

    //Do something with the contents
    //delete allocated memory
    delete[] contents;
}
2 of 2
36

Minizip does have an example programs to demonstrate its usage - the files are called minizip.c and miniunz.c.

Update: I had a few minutes so I whipped up this quick, bare bones example for you. It's very smelly C, and I wouldn't use it without major improvements. Hopefully it's enough to get you going for now.

Copy// uzip.c - Simple example of using the minizip API.
// Do not use this code as is! It is educational only, and probably
// riddled with errors and leaks!
#include <stdio.h>
#include <string.h>

#include "unzip.h"

#define dir_delimter '/'
#define MAX_FILENAME 512
#define READ_SIZE 8192

int main( int argc, char **argv )
{
    if ( argc < 2 )
    {
        printf( "usage:\n%s {file to unzip}\n", argv[ 0 ] );
        return -1;
    }

    // Open the zip file
    unzFile *zipfile = unzOpen( argv[ 1 ] );
    if ( zipfile == NULL )
    {
        printf( "%s: not found\n" );
        return -1;
    }

    // Get info about the zip file
    unz_global_info global_info;
    if ( unzGetGlobalInfo( zipfile, &global_info ) != UNZ_OK )
    {
        printf( "could not read file global info\n" );
        unzClose( zipfile );
        return -1;
    }

    // Buffer to hold data read from the zip file.
    char read_buffer[ READ_SIZE ];

    // Loop to extract all files
    uLong i;
    for ( i = 0; i < global_info.number_entry; ++i )
    {
        // Get info about current file.
        unz_file_info file_info;
        char filename[ MAX_FILENAME ];
        if ( unzGetCurrentFileInfo(
            zipfile,
            &file_info,
            filename,
            MAX_FILENAME,
            NULL, 0, NULL, 0 ) != UNZ_OK )
        {
            printf( "could not read file info\n" );
            unzClose( zipfile );
            return -1;
        }

        // Check if this entry is a directory or file.
        const size_t filename_length = strlen( filename );
        if ( filename[ filename_length-1 ] == dir_delimter )
        {
            // Entry is a directory, so create it.
            printf( "dir:%s\n", filename );
            mkdir( filename );
        }
        else
        {
            // Entry is a file, so extract it.
            printf( "file:%s\n", filename );
            if ( unzOpenCurrentFile( zipfile ) != UNZ_OK )
            {
                printf( "could not open file\n" );
                unzClose( zipfile );
                return -1;
            }

            // Open a file to write out the data.
            FILE *out = fopen( filename, "wb" );
            if ( out == NULL )
            {
                printf( "could not open destination file\n" );
                unzCloseCurrentFile( zipfile );
                unzClose( zipfile );
                return -1;
            }

            int error = UNZ_OK;
            do    
            {
                error = unzReadCurrentFile( zipfile, read_buffer, READ_SIZE );
                if ( error < 0 )
                {
                    printf( "error %d\n", error );
                    unzCloseCurrentFile( zipfile );
                    unzClose( zipfile );
                    return -1;
                }

                // Write data to file.
                if ( error > 0 )
                {
                    fwrite( read_buffer, error, 1, out ); // You should check return of fwrite...
                }
            } while ( error > 0 );

            fclose( out );
        }

        unzCloseCurrentFile( zipfile );

        // Go the the next entry listed in the zip file.
        if ( ( i+1 ) < global_info.number_entry )
        {
            if ( unzGoToNextFile( zipfile ) != UNZ_OK )
            {
                printf( "cound not read next file\n" );
                unzClose( zipfile );
                return -1;
            }
        }
    }

    unzClose( zipfile );

    return 0;
}

I built and tested it with MinGW/MSYS on Windows like this:

Copycontrib/minizip/$ gcc -I../.. -o unzip uzip.c unzip.c ioapi.c ../../libz.a
contrib/minizip/$ ./unzip.exe /j/zlib-125.zip
🌐
Cplusplus
cplusplus.com › forum › windows › 91350
[Solved] Trying to use libzip - C++ Forum
January 31, 2013 - Hi all! I am learning C++ and decided to make a little program to train me. It will be a program that zip/unzip files and moves them to another folder. I decided to use libzip. I compiled zlib and libzip with ./configure, make and make install in Cygwin. I linked libz.a, libzip.a and libzip.dll.a ...
🌐
Example Code
example-code.com › cpp › zip.asp
Zip Examples for C++
Chilkat • HOME • Android™ • AutoIt • C • C# • C++ • Chilkat2-Python • CkPython • Classic ASP • DataFlex • Delphi DLL • Go • Java • JavaScript • Node.js • Objective-C • PHP Extension • Perl • PowerBuilder • PowerShell • PureBasic • Ruby • SQL Server • Swift • Tcl • ...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-program-to-read-and-print-all-files-from-a-zip-file
C Program to Read and Print All Files From a Zip File - GeeksforGeeks
September 5, 2022 - // C program to read and print // all files in a zip file // uses library libzip #include <stdlib.h> #include <zip.h> // this is run from the command line with the zip file // passed in example usage: ./program zipfile.zip int main(int argc, char* argv[]) { // if more or less than 2 // command line arguments, // program ends if (argc > 2 || argc < 2) return -1; // if the file provided can't // be opened/read, program // ends if (!fopen(argv[1], "r")) return -2; // stores error codes for libzip functions int errorp = 0; // initializes a pointer to a zip archive zip_t* arch = NULL; // sets that
🌐
FreshPorts
freshports.org › archivers › libzip
FreshPorts -- archivers/libzip: C library for reading, creating, and modifying ZIP archives
libzip is a C library for reading, creating, and modifying zip archives. Files can be added from data buffers, files, or compressed data copied directly from other zip archives. Changes made without closing the archive can be reverted. The API is documented by man pages.
🌐
Libzip
libzip.org › documentation › ziptool.html
ziptool · libzip
A C library for reading, creating, and modifying zip archives · Current version is 1.11.4
🌐
ManKier
mankier.com › home › libzip-devel
libzip: library for manipulating zip archives | Man Page | Library Functions | libzip | ManKier
The zip format requires the use of forward slash (‘/’) as directory separator. Since backslash (‘\’) can be part of a valid file name on Unix systems, libzip does not automatically convert them, even on Windows.
🌐
Libzip
libzip.org › documentation › zip_open.html
zip_open · libzip
zip_open() and zip_open_from_source() were added in libzip 1.0.
🌐
Stack Overflow
stackoverflow.com › questions › tagged › libzip
Newest 'libzip' Questions - Stack Overflow
I wrote a function to unzip a .zip file given its location and destination directory path. The function is correctly able to ... ... I'm trying to use libzip in a program that needs to archive several data chunks in different files. At the moment I have a code similar to the following snippet, edited from in-memory.c example ...