VOOZH about

URL: https://www.geeksforgeeks.org/c/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


  • Courses
  • Tutorials
  • Interview Prep

C Program to Read and Print All Files From a Zip File

Last Updated : 5 Sep, 2022

To understand how to write a C program for reading and printing zip files, it's important to know what exactly a zip file is.

  • At its core, a zip file contains one or more files compressed using specific compression algorithms.
  • Including the compressed data of the files, the zip file contains meta and header information about all files inside the zip file. These contain file names, modification dates, signatures, compression methods, etc.
  • A zip file can also be called a zip archive because of its organization and structure of multiple files.

With the above information in mind we only need two things to programmatically read and print the contents of a zip file:

  1. A decompression method that will decompress the data so that we can read it.
  2. A library for interacting with zip files, to make things way easier

We will be using two libraries that also provide decompression functions to write this program.

Library 1:

Library 2:

Both of these libraries are prerequisites for running the code. Libzip is a higher-level library that already utilizes parts of zlib. Zlib is lower level and therefore more technical to use. There will be code examples, one that uses libzip, and one that uses libzip for zip file interaction while utilizing zlib for the decompression.

  • For help installing libzip: check here
  • For help installing zlib: check here

If these libraries are installed, you can successfully compile them with gcc, just pass compiler flags -lz for method 1 and -lz -lzip for method 2.

Method 1: Reading and Printing All Files from a Zip File using libzip

Example output where my zip file contained this C file

👁 Image
method 1 output

Method 2: Using  Zlib Implementation

Zlib cannot directly access zip files, which is why we will use libzip to open the zip file. Zlib directly is used for decompression of the file contents in this method.

Here is the output of  the above code using a zip file with 2 files contained

👁 Image
Method 2 output
Comment
Article Tags: