CZipFile: a Lite Zip Archive File Viewer

Environment: VC6 SP4

Introduction

CZipFile is a lite library that allows you to get information about a zip archive. It is not able to decompress the files, but just retrieves the contents—the file name, file size, and so on.

I developed it because I need a lite way to list the zip file's contents on a list view. But all the code I found on the Internet was really huge, allowing me to decode the files. I needed a very small application, so I made my own class. The downloadable code was compiled for Windows 98 and 2000. I think that it should be compiled by all Windows platforms.

Overview of a ZIP File

The information was retrieved in the PKWARE's appnote.txt file; that can be found on the Internet. The information we need is contained in the [central directory] structure. It contains, file by file, all the information about the file features (file name, compressed and uncompressed file size, date and time, ...) and the compression options (compression method, password protection, ...)

So, in order to retrieve the file's attribute, the following passes must be performed:

  1. Be sure that the file is a valid ZIP archive
  2. Look for the [central directory] header
  3. Decode all the information for each file

They were implemented, as in the following:

  1. The ZIP file correctiveness can be tested (but not necessarily) by reading the first four bytes of the zipped file. They are the "local file header signature" and must be the value 0x04034b50.
  2. The [central directory] header is signed by a 4-byte code called "central file header signature" with the value 0x02014b50. It can be found looking for such code in the stream.
  3. The [central directory] has the following structure:
  4.       [file header 1]
    
          .
          .
          .
    
          [file header n]
    
          [digital signature]
    
    We need to decode just the [file header x] blocks until no other [file header] is found.

Each [file header] starts with a signature (0x02014b50), so it is really simple to find it in the stream.

Each file header data was encapsulated in a structure. An array stores all the file headers.

struct FileHeader
{
  DWORD signature;
  WORD version_made_by;
  WORD version_needed;
  WORD bitflags;
  WORD comp_method;
  WORD lastModFileTime;
  WORD lastModFileDate;
  DWORD crc_32;
  DWORD comp_size;
  DWORD uncompr_size;
  WORD fname_len;
  WORD extra_field_len;
  WORD fcomment_len;
  WORD disk_num_start;
  WORD internal_fattribute;
  DWORD external_fattribute;
  DWORD relative_offset;
  char* file_name;
  char* extra_field;
  char* file_comment;
};

The original application uses just the file_name attribute to fill a list view. However, the demo application uses the comp_size (compressed size in bytes), the uncompr_size (uncompressed size in bytes), the lastModFileDate, and the lastModFileTime for the file's date and time, and the bit 0 of the bitflags, indicating the password protection flag.

Class Description

The core of the paper is the CZipFile class that encapsulates all the operations. It is defined as the following:

class CZipFile
{
// Attributes
private:
  CString   m_FileName;
  CPtrArray m_FileAttributes;

//interface
public:
  CZipFile();
  CZipFile(CString);
  virtual ~CZipFile();
  void    ResetContent(void);

  CString      GetFileName(void);
  void         SetFileName(CString);
  BOOL         OpenFile(void);
  int          GetFilesNumber(void);
  FileHeader * GetFileAttributes(int);

// inner operations
private:
  void    ReadCentralDirectory(BYTE * data,long len);
  int     ReadFileHeader(BYTE * data, FileHeader * hdr);
  DWORD   ReadValue(unsigned char * buf, int nbits);
};

The public interface allows us to set and to retrieve the ZIP file name (respectively, SetFileName and GetFileName), to open the file (OpenFile, that reads all the data), to reset the class content information (ResetContent), to retrieve the number of files inserted in the zip archive (GetFilesNumber), and to retrieve all the information of each file (GetFileAttributes).

All the other variables/functions are hidden. They are used for storing the ZIP information and reading the Central Directory structure.

Using the Class

To use the CZipFile class in your project, you need to add the following files:

zipstruct.hcontains the FileHeader structure
ZipFile.hcontains the CZipFile class definition
ZipFile.cppcontains the CZipFile class implementation

The Demo Project

The included application is just a demo on how to use the class. It allows you to open a zipped archive and list its contents (the file name) in a list control. Each file's attributes can be displayed by simply clicking it in the list control.

Downloads

Download demo project - 105 Kb
Download source - 15 Kb


Comments

  • version_made_by not in zip format specs??

    Posted by Legacy on 02/06/2004 12:00am

    Originally posted by: mike

    from the pkware page:

    local file header signature 4 bytes (0x04034b50)
    version needed to extract 2 bytes
    general purpose bit flag 2 bytes
    compression method 2 bytes
    last mod file time 2 bytes
    last mod file date 2 bytes
    crc-32 4 bytes
    compressed size 4 bytes
    uncompressed size 4 bytes
    file name length 2 bytes
    extra field length 2 bytes
    file name (variable size)
    extra field (variable size)

    Reply
  • pointer not deleted

    Posted by Legacy on 09/02/2003 12:00am

    Originally posted by: youssef

    there is a pointer not deleted "fn=new (char[hdr->fname_len+1]);"

    Reply
  • how do i unzip the files

    Posted by Legacy on 03/17/2003 12:00am

    Originally posted by: Simon Soosai

    I didn't look your comment that your code cannot unzip. I already implemented your code and now trying to unzip. Please help how do i unzip.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds