ZipFile Class Reference

#include <lib_zipfile.h>

Detailed Description

Allows to read/write ZIP files.

Private Member Functions

 ZipFile ()
 
 ~ZipFile ()
 

Alloc/Free

static ZipFileAlloc ()
 
static void Free (ZipFile *&p)
 

Static

static Bool CreateLocalFileName (const Filename &fn, String &str)
 
static Bool CreateFilename (const String &str, Filename &fn)
 
static Bool GetFileCRC (const Filename &fn, UInt32 &ulCRC)
 
static UInt32 CalcCRC32 (const void *pBuffer, Int32 lBufferLen, UInt32 ulOldCRC=0)
 

Open/Extract/Close

Bool Open (const Filename &fn, const Bool bRead, const Int32 lAppend=0)
 
Bool OpenEncrypted (const Filename &fn, const Bool bRead, const char *pchKey, Int32 lKeyLength, Int32 lBlockLength, UInt32 lAESFlags, const Int32 lAppend=0)
 
Bool SetSpanning (UInt32 ulSpan, Bool bOverwrite)
 
Bool ExtractToDirectory (const Filename &fnZip, const Filename &fnDir, Int32 lFlags=0x00000001, ExtractDirectoryCallback fn=nullptr, void *pData=nullptr, const char *pChPassword=nullptr)
 
Bool Close ()
 
Bool Close (const char *pchGlobalComment)
 
Bool Close (const String &strGlobalComment)
 

Write

Bool CreateFileInZip (const String &strName, ZipWriteInfo *pInfo, void *pExtraFieldLocal, UInt32 lExtraSizeLocal, void *pExtraFieldGlobal, UInt32 lExtraSizeGlobal, String *pstrComment, ZipMethod method, INT lLevel, const char *pchPassword=nullptr, UInt32 ulCryptingCRC=0)
 
Bool WriteInFileInZip (const void *pBuffer, const UInt32 lLen, const Int32 lExpectedSize=-1)
 
Bool CopyInFileInZip (const Filename &fn, const String &str, const char *pchPassword=nullptr)
 
Bool CopyInFileInZip (const Filename &fn, const String &str, UInt32 ulInternalAttributes, UInt32 ulExternalAttributes, const char *pchPassword=nullptr)
 
Bool CloseFileInZip ()
 
Bool CreateDirectoryInZip (const String &strName, ZipFileTime *pTime)
 

Read

Bool GetGlobalInfo (ZipFileGlobalInfo &i)
 
Bool GetGlobalComment (String &str)
 
Bool GoToFirstFile ()
 
Bool GoToNextFile ()
 
Bool LocateFile (const String &strName)
 
Bool GetCurrentFileInfo (ZipFileInfo &i)
 
Bool GetCurrentFileInfo (String *pstrName, String *pstrComment=nullptr, void *pExtraField=nullptr, UInt32 lExtraFieldSize=0)
 
Bool ExtractCurrentFile (const Filename &fnDir, BaseThread *pThread, Int32 lFlags=0x00000001, const char *pchPassword=nullptr, Filename *pfnDest=nullptr)
 
Bool ExtractCurrentFile (const Filename &fnDir, Int32 lFlags=0x00000001, const char *pchPassword=nullptr, Filename *pfnDest=nullptr)
 
Bool OpenCurrentFile (const char *pChPassword=nullptr)
 
Bool CloseCurrentFile ()
 
Int32 ReadCurrentFile (void *pBuffer, UInt32 lBufferSize)
 
Int32 GetCurrentFileReadPosition ()
 
Bool EndOfCurrentFile ()
 

Compression Level

Bool SetCompressionLevel (Int32 lLevel)
 
Int32 GetCompressionLevel () const
 

Constructor & Destructor Documentation

◆ ZipFile()

ZipFile ( )
private

◆ ~ZipFile()

~ZipFile ( )
private

Member Function Documentation

◆ Alloc()

static ZipFile* Alloc ( )
static

Allocates a ZIP file. Destroy the allocated ZIP file with Free(). Use AutoAlloc to automate the allocation and destruction based on scope.

Returns
The allocated ZIP file, or nullptr if the allocation failed.

◆ Free()

static void Free ( ZipFile *&  p)
static

Destructs ZIP file allocated with Alloc(). Use AutoAlloc to automate the allocation and destruction based on scope.

Parameters
[in,out]pThe ZIP file to destruct. If the pointer is nullptr nothing happens. The pointer is assigned nullptr afterwards.

◆ CreateLocalFileName()

static Bool CreateLocalFileName ( const Filename fn,
String str 
)
static

Creates a local file name.

Parameters
[in]fnThe filename to create from.
[out]strAssigned the local file name to create.
Returns
true if successful, otherwise false.

◆ CreateFilename()

static Bool CreateFilename ( const String str,
Filename fn 
)
static

Creates a file name.

Parameters
[in]strThe file string to create from.
[in]fnAssigned the filename to create.
Returns
true if successful, otherwise false.

◆ GetFileCRC()

static Bool GetFileCRC ( const Filename fn,
UInt32 ulCRC 
)
static

Gets a ZIP file CRC.

Parameters
[in]fnThe filename to get the CRC for.
[out]ulCRCThe calculated CRC.
Returns
true if successful, otherwise false.

◆ CalcCRC32()

static UInt32 CalcCRC32 ( const void *  pBuffer,
Int32  lBufferLen,
UInt32  ulOldCRC = 0 
)
static

Calculates the CRC from a buffer.

Parameters
[in]pBufferThe buffer to calculate the CRC from.
[in]lBufferLenThe buffer length.
[in]ulOldCRCThe optional initial CRC value.
Returns
The calculated CRC.

◆ Open()

Bool Open ( const Filename fn,
const Bool  bRead,
const Int32  lAppend = 0 
)

Opens a ZIP file.

Parameters
[in]fnThe file to open.
[in]bReadtrue to open for reading, otherwise false.
[in]lAppendThe append flags to append the ZIP file to an existing file: ZIP_APPEND
Returns
true if successful, otherwise false.

◆ OpenEncrypted()

Bool OpenEncrypted ( const Filename fn,
const Bool  bRead,
const char *  pchKey,
Int32  lKeyLength,
Int32  lBlockLength,
UInt32  lAESFlags,
const Int32  lAppend = 0 
)

Opens an AES encrypted ZIP file, see AESFile.

Parameters
[in]fnThe encrypted file to open.
[in]bReadtrue to open for reading, otherwise false.
[in]pchKeyThe decryption key.
[in]lKeyLengthThe key length. Must be 128, 192 or 256 (bits).
[in]lBlockLengthThe block size. Must be 128, 192 or 256 (bits).
[in]lAESFlagsReserved for later use. Must be set to 0.
[in]lAppendThe append flags to append the ZIP file to an existing file: ZIP_APPEND
Returns
true if successful, otherwise false.

◆ SetSpanning()

Bool SetSpanning ( UInt32  ulSpan,
Bool  bOverwrite 
)

Sets the maximum size of the ZIP file. A new file is created with the same name and a consecutive number if it exceeds this size.

Note
Call immediately after opening a ZIP file for writing.
Parameters
[in]ulSpanThe maximum size of the file.
[in]bOverwriteIf true the next file is overwritten.
Returns
true if successful, otherwise false.

◆ ExtractToDirectory()

Bool ExtractToDirectory ( const Filename fnZip,
const Filename fnDir,
Int32  lFlags = 0x00000001,
ExtractDirectoryCallback  fn = nullptr,
void *  pData = nullptr,
const char *  pChPassword = nullptr 
)

Extracts the ZIP file to the specified directory.

Parameters
[in]fnZipThe ZIP filename.
[in]fnDirThe directory to extract to.
[in]lFlagsThe extract flags: ZIP_EXTRACT
[in]fnThe extract callback.
[in]pDataThe private data for the extract callback.
[in]pChPasswordAn optional password.
Returns
true if successful, otherwise false.

◆ Close() [1/3]

Bool Close ( )

Closes the ZIP file.

Returns
true if successful, otherwise false.

◆ Close() [2/3]

Bool Close ( const char *  pchGlobalComment)

Closes the ZIP file and writes a comment.

Note
Comments are only written if the file is saved.
Parameters
[in]pchGlobalCommentThe global comment string.
Returns
true if successful, otherwise false.

◆ Close() [3/3]

Bool Close ( const String strGlobalComment)

Closes the ZIP file and writes a comment.

Note
Comments are only written if the file is saved.
Parameters
[in]strGlobalCommentThe global comment string.
Returns
true if successful, otherwise false.

◆ CreateFileInZip()

Bool CreateFileInZip ( const String strName,
ZipWriteInfo pInfo,
void *  pExtraFieldLocal,
UInt32  lExtraSizeLocal,
void *  pExtraFieldGlobal,
UInt32  lExtraSizeGlobal,
String pstrComment,
ZipMethod  method,
INT  lLevel,
const char *  pchPassword = nullptr,
UInt32  ulCryptingCRC = 0 
)

Creates a file in the ZIP file.

Note
Directories must be separated by a slash.
Parameters
[in]strNameThe file name.
[in]pInfoThe write information.
[in]pExtraFieldLocalThe extra local field.
[in]lExtraSizeLocalThe extra local size.
[in]pExtraFieldGlobalThe extra global field.
[in]lExtraSizeGlobalThe extra global size.
[in]pstrCommentThe comment string.
[in]methodThe ZIP method: ZipMethod
[in]lLevelThe ZIP compression level, between 0 and 9.
[in]pchPasswordAn optional password.
[in]ulCryptingCRCAn optional encryption CRC.
Returns
true if successful, otherwise false.

◆ WriteInFileInZip()

Bool WriteInFileInZip ( const void *  pBuffer,
const UInt32  lLen,
const Int32  lExpectedSize = -1 
)

Writes a buffer in the ZIP file.

Parameters
[in]pBufferThe buffer to write.
[in]lLenThe buffer length.
[in]lExpectedSizeAn optional expected size.
Returns
true if successful, otherwise false.

◆ CopyInFileInZip() [1/2]

Bool CopyInFileInZip ( const Filename fn,
const String str,
const char *  pchPassword = nullptr 
)

Copies a file in the ZIP file.

Parameters
[in]fnThe file to copy.
[in]strThe destination file name.
[in]pchPasswordAn optional password.
Returns
true if successful, otherwise false.

◆ CopyInFileInZip() [2/2]

Bool CopyInFileInZip ( const Filename fn,
const String str,
UInt32  ulInternalAttributes,
UInt32  ulExternalAttributes,
const char *  pchPassword = nullptr 
)

Copies a file in the ZIP file.

Parameters
[in]fnThe file to copy.
[in]strThe destination file name.
[in]ulInternalAttributesThe internal file attributes.
[in]ulExternalAttributesThe external file attributes.
[in]pchPasswordAn optional password.
Returns
true if successful, otherwise false.

◆ CloseFileInZip()

Bool CloseFileInZip ( )

Closes a file in the ZIP file.

Returns
true if successful, otherwise false.

◆ CreateDirectoryInZip()

Bool CreateDirectoryInZip ( const String strName,
ZipFileTime pTime 
)

Creates a directory in the ZIP file. A slash is added if necessary.

Parameters
[in]strNameThe directory name.
[in]pTimeThe directory time.
Returns
true if successful, otherwise false.

◆ GetGlobalInfo()

Bool GetGlobalInfo ( ZipFileGlobalInfo i)

Retrieves the global information.

Parameters
[out]iFilled with the global information.
Returns
true if successful, otherwise false.

◆ GetGlobalComment()

Bool GetGlobalComment ( String str)

Retrieves the global comment.

Parameters
[out]strFilled with the global comment.
Returns
true if successful, otherwise false.

◆ GoToFirstFile()

Bool GoToFirstFile ( )

Goes to the first file.

Returns
true if successful, otherwise false.

◆ GoToNextFile()

Bool GoToNextFile ( )

Goes to the next file.

Returns
true if successful, otherwise false.

◆ LocateFile()

Bool LocateFile ( const String strName)

Locates a file.

Parameters
[in]strNameThe file name to locate.
Returns
true if successful, otherwise false.

◆ GetCurrentFileInfo() [1/2]

Bool GetCurrentFileInfo ( ZipFileInfo i)

Retrieves the information for the current file.

Parameters
[out]iFilled with the current file information.
Returns
true if successful, otherwise false.

◆ GetCurrentFileInfo() [2/2]

Bool GetCurrentFileInfo ( String pstrName,
String pstrComment = nullptr,
void *  pExtraField = nullptr,
UInt32  lExtraFieldSize = 0 
)

Retrieves the information for the current file.

Parameters
[out]pstrNameFilled with the current file name.
[out]pstrCommentFilled with the current file comment.
[out]pExtraFieldFilled with the current file extra field.
[out]lExtraFieldSizeFilled with the current file extra field size.
Returns
true if successful, otherwise false.

◆ ExtractCurrentFile() [1/2]

Bool ExtractCurrentFile ( const Filename fnDir,
BaseThread pThread,
Int32  lFlags = 0x00000001,
const char *  pchPassword = nullptr,
Filename pfnDest = nullptr 
)

Extracts the contents of the current archive to the directory fnDir.

Parameters
[in]fnDirThe directory to extract the files to.
[in]pThreadThe thread for extracting in an own thread.
[in]lFlagsThe extract flags: ZIP_EXTRACT
[in]pchPasswordAn optional password.
[out]pfnDestAn optional filename pointer filled with the destination.
Returns
true if successful, otherwise false.

◆ ExtractCurrentFile() [2/2]

Bool ExtractCurrentFile ( const Filename fnDir,
Int32  lFlags = 0x00000001,
const char *  pchPassword = nullptr,
Filename pfnDest = nullptr 
)

Extracts the contents of the current archive to the directory fnDir.

Parameters
[in]fnDirThe directory to extract the files to.
[in]lFlagsThe extract flags: ZIP_EXTRACT
[in]pchPasswordAn optional password.
[out]pfnDestAn optional filename pointer filled with the destination.
Returns
true if successful, otherwise false.

◆ OpenCurrentFile()

Bool OpenCurrentFile ( const char *  pChPassword = nullptr)

Opens the current file with password protection.

Parameters
[in]pChPasswordAn optional password.
Returns
true if successful, otherwise false.

◆ CloseCurrentFile()

Bool CloseCurrentFile ( )

Closes the current file.

Returns
true if successful, otherwise false.

◆ ReadCurrentFile()

Int32 ReadCurrentFile ( void *  pBuffer,
UInt32  lBufferSize 
)

Reads a buffer from the current file.

Parameters
[in,out]pBufferThe buffer to read to.
[in,out]lBufferSizeThe buffer size.
Returns
The number of bytes read, or < 0 if there was an error.

◆ GetCurrentFileReadPosition()

Int32 GetCurrentFileReadPosition ( )

Gets the read position in the current file.

Returns
The current read position.

◆ EndOfCurrentFile()

Bool EndOfCurrentFile ( )

Checks for the end of the current file.

Returns
true if end of current file, otherwise false.

◆ SetCompressionLevel()

Bool SetCompressionLevel ( Int32  lLevel)

Sets the compression level of the ZIP file.

Note
Must be called before opening and initializing the file.
Parameters
[in]lLevelThe compression level, ranges from 0 (min) to 9 (max).
Returns
true if successful, otherwise false.

◆ GetCompressionLevel()

Int32 GetCompressionLevel ( ) const

Gets the compression level of the ZIP file.

Returns
The compression level, ranges from 0 (min) to 9 (max).