About
The ZipFile class provides means to work with ZIP archives. ZIP files can be created, modified and extracted.
- Note
- To create ZIP archives with the Maxon API see Archives Manual.
Allocation/Deallocation
ZipFile objects are created with the usual tools, see Entity Creation and Destruction Manual (Cinema API).
- ZipFile::Alloc(): Allocates a ZipFile.
- ZipFile::Free(): Destructs a ZipFile previously allocated with ZipFile::Alloc().
Opening and Closing
- ZipFile::Open(): Opens a ZIP file.
- ZipFile::OpenEncrypted(): Opens an AES encrypted ZIP file, see AESFile.
- ZipFile::SetSpanning(): 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
- Needs to be called immediately after opening a ZIP file for writing.
- ZipFile::Close(): Closes the ZIP file.
Filename
- ZipFile::CreateLocalFileName(): Creates a local file name.
- ZipFile::CreateFilename(): Creates a file name.
Metadata
- ZipFile::GetGlobalInfo(): Retrieves the global information (ZipFileGlobalInfo).
- ZipFile::GetGlobalComment(): Retrieves the global comment.
Compression Level
- ZipFile::SetCompressionLevel(): Sets the compression level of the ZIP file.
- Note
- Must be called before opening and initializing the file.
- ZipFile::GetCompressionLevel(): Gets the compression level of the ZIP file.
CRC
- ZipFile::GetFileCRC(): Gets a ZIP file CRC.
- ZipFile::CalcCRC32(): Calculates the CRC from a buffer.
Extracting an Entire Archive
- ZipFile::ExtractToDirectory(): Extracts all contents of the ZIP file to the specified directory.
Filename fnZip, fnDest;
AutoAlloc<ZipFile> zf;
if (zf == nullptr)
if (!zf->ExtractToDirectory(fnZip, fnDest))
LOAD
Load.
Definition: c4d_filterdata.h:1
DIRECTORY
Folder selection dialog.
Definition: ge_prepass.h:2
ANYTHING
Any file.
Definition: ge_prepass.h:0
return OK
Definition: apibase.h:2740
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:69
Current File
Internally the ZipFile class has a pointer to the "current" file inside of the archive. The following functions provide means to change the current file pointer to another file or directory.
- ZipFile::LocateFile(): Locates a file by name and sets the current file pointer to this file.
- ZipFile::GoToFirstFile(): Sets the current file pointer to the first file or directory.
- ZipFile::GoToNextFile(): Sets the current file pointer to the next file or directory.
UInt32 fileSizeUncompressed = 0;
if (!zf->GoToFirstFile())
return PrintMsgAndCloseZipFile(zf, "Error: No files in ZIP archive.");
do
{
ZipFileInfo zfi;
String sName;
if (zf->GetCurrentFileInfo(zfi) && !(zfi.lFlags &
ZIP_FLAG_DIRECTORY) && zf->GetCurrentFileInfo(&sName) && Filename(sName).CheckSuffix(
"c4d"_s))
{
fileSizeUncompressed = zfi.lUncompressedSize;
sceneFound = true;
break;
}
} while (zf->GoToNextFile());
if (!sceneFound || fileSizeUncompressed == 0)
return PrintMsgAndCloseZipFile(zf, "Error: No C4D scene found in archive.");
#define ZIP_FLAG_DIRECTORY
Directory flag.
Definition: lib_zipfile.h:20
maxon::Bool Bool
Definition: ge_sys_math.h:46
maxon::UInt32 UInt32
Definition: ge_sys_math.h:52
The current file can be extracted from a ZIP archive:
- ZipFile::ExtractCurrentFile(): Extracts the current file to a directory (keeping its directory).
Filename fnZip, fnDest;
AutoAlloc<ZipFile> zf;
if (zf == nullptr)
if (!zf->Open(fnZip, true))
if (!zf->GoToFirstFile())
{
zf->Close();
}
do
{
ZipFileInfo zfi;
zf->ExtractCurrentFile(fnDest);
} while (zf->GoToNextFile());
zf->Close();
#define ApplicationOutput(formatString,...)
Definition: debugdiagnostics.h:204
Bool ShowInFinder(const Filename &fn, Bool open)
Comprehensive information can be retrieved for the current file:
- ZipFile::GetCurrentFileInfo(): Retrieves information for the current file (there are actually two functions providing different infos, see also ZipFileInfo and ZipFileTime).
Similar to "normal" files (e.g. BaseFile) the current file can be opened for reading:
- ZipFile::OpenCurrentFile(): Opens the current file (optionally with password protection).
- ZipFile::CloseCurrentFile(): Closes the current file.
- ZipFile::ReadCurrentFile(): Reads a buffer from the current file.
- ZipFile::GetCurrentFileReadPosition(): Gets the read position in the current file.
- ZipFile::EndOfCurrentFile(): Checks for the end of the current file.
if (!zf->OpenCurrentFile())
return PrintMsgAndCloseZipFile(zf, "Error: Failed to open current file in archive.");
return PrintMsgAndCloseZipFile(zf, "Error: Failed to open allocate buffer.");
const Int32 maxChunkSize = 0x1000;
Char*
const bufferEnd =
buffer.GetPointer() + fileSizeUncompressed;
while (!zf->EndOfCurrentFile())
{
if (chunkSize <= 0)
break;
const Int32 numBytesRead = zf->ReadCurrentFile(bufferCurrentPos, chunkSize);
if (numBytesRead < 0)
break;
bufferCurrentPos += numBytesRead;
}
if (!zf->EndOfCurrentFile() || (bufferCurrentPos != bufferEnd))
{
return PrintMsgAndCloseZipFile(zf, "Error: Something went wrong, while reading file from archive.");
}
zf->CloseCurrentFile();
zf->Close();
const char ** buffer
Definition: abstract.h:327
#define NewMemClear(T, cnt)
Definition: defaultallocator.h:216
X ClampValue(X value, X lowerLimit, X upperLimit)
Clips a value against a lower and upper limit. The new value is returned.
Definition: apibasemath.h:230
maxon::Char Char
Definition: ge_sys_math.h:47
maxon::Int32 Int32
Definition: ge_sys_math.h:51
#define iferr_ignore(...)
Definition: resultbase.h:1496
Add to an ZIP File
- ZipFile::CreateFileInZip(): Creates a file in the ZIP file.
- Parameter "method", ::ZipMethod:
- ::ZipMethodDeflate: Deflate
- ::ZipMethodStore: Store
- ZipFile::WriteInFileInZip(): Writes a buffer in the ZIP file.
- ZipFile::CopyInFileInZip(): Copies a file in the ZIP file.
- ZipFile::CloseFileInZip(): Closes a file in the ZIP file.
- ZipFile::CreateDirectoryInZip(): Creates a directory in the ZIP file. A slash is added if necessary.
Filename fnZip;
AutoAlloc<ZipFile> zf;
if (zf == nullptr)
Filename fnCopy;
{
zf->CopyInFileInZip(fnCopy, fnCopy.GetFileString());
}
zf->Close();
SAVE
::Bool Get/Set. Determines if the layer is saved with the image or not if SAVEBIT::USESELECTEDLAYERS ...
Definition: ge_prepass.h:1
#define ZIP_APPEND_CREATE
Creates a new ZIP file.
Definition: lib_zipfile.h:131
Further Reading