Click here to Skip to main content
15,896,915 members
Articles / Programming Languages / C++

Win32 Wrapper classes for Gilles Volant's Zip/Unzip API

Rate me:
Please Sign up or sign in to vote.
4.87/5 (33 votes)
27 Jul 2003CC (ASA 2.5)3 min read 465.7K   17.6K   102  
High level wrapping of the zlib library to make easy work of zipping and unzipping files and folders
// Unzipper.h: interface for the CUnzipper class (c) daniel godson 2002
//
// CUnzipper is a simple c++ wrapper for the 'unzip' file extraction
// api written by Gilles Vollant (c) 2002, which in turn makes use of 
// 'zlib' written by Jean-loup Gailly and Mark Adler (c) 1995-2002.
//
// This software is provided 'as-is', without any express or implied
// warranty.  In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely without restriction.
//
// Notwithstanding this, you are still bound by the conditions imposed
// by the original authors of 'unzip' and 'zlib'
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_UNZIPPER_H__EBC42716_31C7_4659_8EF3_9BF8D4409709__INCLUDED_)
#define AFX_UNZIPPER_H__EBC42716_31C7_4659_8EF3_9BF8D4409709__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

const UINT MAX_COMMENT = 255;

// create our own fileinfo struct to hide the underlying implementation
struct UZ_FileInfo
{
	char szFileName[MAX_PATH + 1];
	char szComment[MAX_COMMENT + 1];
	
	DWORD dwVersion;  
	DWORD dwVersionNeeded;
	DWORD dwFlags;	 
	DWORD dwCompressionMethod; 
	DWORD dwDosDate;	
	DWORD dwCRC;   
	DWORD dwCompressedSize; 
	DWORD dwUncompressedSize;
	DWORD dwInternalAttrib; 
	DWORD dwExternalAttrib; 
	bool bFolder;
};

class CUnzipper  
{
public:
	CUnzipper(LPCTSTR szFilePath = NULL);
	virtual ~CUnzipper();
	
	// simple interface
	static bool Unzip(LPCTSTR szFileName, LPCTSTR szFolder = NULL, bool bIgnoreFilePath = FALSE);
	
	// works with prior opened zip
	bool Unzip(bool bIgnoreFilePath = FALSE); // unzips to output folder or sub folder with zip name 
	bool UnzipTo(LPCTSTR szFolder, bool bIgnoreFilePath = FALSE); // unzips to specified folder

	// extended interface
	bool OpenZip(LPCTSTR szFilePath);
	bool CloseZip(); // for multiple reuse
	bool SetOutputFolder(LPCTSTR szFolder); // will try to create
	
	// unzip by file index
	int GetFileCount();
	bool GetFileInfo(int nFile, UZ_FileInfo& info);
	bool UnzipFile(int nFile, LPCTSTR szFolder = NULL, bool bIgnoreFilePath = FALSE);
	
	// unzip current file
	bool GotoFirstFile(LPCTSTR szExt = NULL);
	bool GotoNextFile(LPCTSTR szExt = NULL);
	bool GetFileInfo(UZ_FileInfo& info);
	bool UnzipFile(LPCTSTR szFolder = NULL, bool bIgnoreFilePath = FALSE);

	// helpers
	bool GotoFile(LPCTSTR szFileName, bool bIgnoreFilePath = TRUE);
	bool GotoFile(int nFile);
	
protected:
	void* m_uzFile;
	char m_szOutputFolder[MAX_PATH + 1];

protected:
	static bool CreateFolder(LPCTSTR szFolder);
	static bool CreateFilePath(LPCTSTR szFilePath); // truncates from the last '\'
	static bool SetFileModTime(LPCTSTR szFilePath, DWORD dwDosDate);

};

#endif // !defined(AFX_UNZIPPER_H__EBC42716_31C7_4659_8EF3_9BF8D4409709__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


Written By
Software Developer Maptek
Australia Australia
.dan.g. is a naturalised Australian and has been developing commercial windows software since 1998.

Comments and Discussions