Archives Manual

内容表

关于

maxon::WriteArchiveInterface and maxon::ReadArchiveInterface are generic interfaces giving access to archive files. These interfaces are typically used to read and write ZIP files.

Writer

The maxon::WriteArchiveInterface allows to create archive files and to add virtual files to the archive.

Implementations of this interface are registered at the maxon::WriteArchiveClasses registry and are also accessible as published object:

  • maxon::WriteArchiveClasses::Zip: Implementation to write ZIP archives.
// This example creates a ZIP file containing two text files.

// create WriteArchiveRef for a ZIP file const maxon::WriteArchiveRef zipArchive = maxon::WriteArchiveClasses::Zip().Create() iferr_return ;

// create ZIP file zipArchive.Open(zipFile, false ) iferr_return ; zipArchive.SetCompressionLevel(2) iferr_return ;

// define file settings const maxon::UniversalDateTime time = maxon::UniversalDateTime::GetNow (); const maxon::IOARCHIVESTOREMETHOD method = maxon::IOARCHIVESTOREMETHOD::DEFLATE ; const maxon::IOATTRIBUTES attributes = maxon::IOATTRIBUTES::NONE ;

// create a file at the top level zipArchive.CreateFile( "text.txt" _s, time, method, attributes, 0) iferr_return ; zipArchive.WriteFile(fileData) iferr_return ; zipArchive.CloseFile() iferr_return ;

// create a file in a sub-folder zipArchive.CreateFile( "exampleFolder/text.txt" _s, time, method, attributes, 0) iferr_return ; zipArchive.WriteFile(fileData) iferr_return ; zipArchive.CloseFile() iferr_return ;

// close ZIP file zipArchive.Close() iferr_return ;

Reader

The maxon::ReadArchiveInterface allows to open and read archive files.

Implementations of this interface are registered at the maxon::ReadArchiveClasses registry and are also accessible as published object:

// This example opens a ZIP file and extracts a sub-file from it.

// callback to check if the given file should be extracted auto SingleFileCallback = []( const maxon::String & inputName, maxon::Url & outputName, maxon::IOATTRIBUTES & fileAttributes) -> maxon::Result<maxon::Bool> { // check inputName or fileAttributes if needed return true ; };

// create new ReadArchiveRef for a ZIP file const maxon::ReadArchiveRef zipArchive = maxon::ReadArchiveClasses::Zip().Create() iferr_return ;

// open ZIP file zipArchive.Open(zipFile) iferr_return ;

// extract a single file; if the file will be extracted is controlled with the optional SingleFileCallback const maxon::IOARCHIVEEXTRACTFLAGS flags = maxon::IOARCHIVEEXTRACTFLAGS::OVERWRITE_EXISTING_FILE ; zipArchive.ExtractSingleFile( "text.txt" _s, targetFolder, maxon::ThreadRef (), flags, SingleFileCallback) iferr_return ;

// close ZIP file zipArchive.Close() iferr_return ;

File Formats

These maxon::FileFormatHandler allow to browse an archive file or to easily read it:

// This example browses the files stored in the given ZIP file.

// obtain IoBrowseRef for the top level const maxon::FileFormatHandler& zipDirBrowser = maxon::FileFormatHandlers::ZipDirectoryBrowser(); auto handler = zipDirBrowser.CreateHandler<maxon::IoBrowseRef>(zipFile);

// browse through all files/folders on the top level for ( auto it : handler) { const maxon::IoBrowseRef& browseIterator = (it) iferr_return ; HandleIterator(browseIterator) iferr_return ; }

// This example shows a function that recursively iterates all files. static maxon::Result<void> HandleIterator( const maxon::IoBrowseRef& browseIterator) { iferr_scope ;

// get current element const maxon::Url currentFile = browseIterator.GetCurrentPath(); const maxon::IODETECT type = currentFile.IoDetect();

// check if file if (type == maxon::IODETECT::FILE ) DiagnosticOutput ( "File: @" , currentFile);

// check if folder if (type == maxon::IODETECT::DIRECTORY ) { DiagnosticOutput ( "Folder @" , currentFile);

// browse files in that folder for ( auto subIt : currentFile.GetBrowseIterator( maxon::GETBROWSEITERATORFLAGS::NONE )) { const maxon::IoBrowseRef& subIterator = (subIt) iferr_return ; HandleIterator(subIterator) iferr_return ; } } return maxon::OK ; }

// This example extracts all files from the given ZIP file into a target folder.

// create ReadArchiveRef handler for a ZIP file const maxon::FileFormatHandler& handler = maxon::FileFormatHandlers::ZipArchiveHandler(); const maxon::ReadArchiveRef zipArchive = handler.CreateHandler<maxon::ReadArchiveRef>( maxon::Url ()) iferr_return ;

// open ZIP file zipArchive.Open(zipFile) iferr_return ;

// extract all files to the given target folder const maxon::IOARCHIVEEXTRACTFLAGS flags = maxon::IOARCHIVEEXTRACTFLAGS::OVERWRITE_EXISTING_FILE ; zipArchive.Extract(targetFolder, maxon::ThreadRef (), flags, nullptr ) iferr_return ;

// close ZIP file zipArchive.Close() iferr_return ;

延伸阅读

maxon::IOATTRIBUTES::NONE
@ NONE
maxon::IODETECT::DIRECTORY
@ DIRECTORY
Url is a directory, you can use GetBrowseIterator to iterate through the children.
maxon::String
定义: string.h:1197
maxon::OK
return OK
定义: apibase.h:2532
iferr_return
#define iferr_return
定义: resultbase.h:1434
maxon::IODETECT
IODETECT
Return value of IoDetect().
定义: ioenums.h:39
maxon::UniversalDateTime
定义: datetime.h:231
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
定义: debugdiagnostics.h:166
maxon::IOARCHIVEEXTRACTFLAGS
IOARCHIVEEXTRACTFLAGS
Extract Flags used in ReadArchiveInterface::Extract() and ReadArchiveInterface::ExtractSingleFile().
定义: ioarchivehandler.h:32
maxon::Url
定义: url.h:819
maxon::Result
定义: apibase.h:314
maxon::ThreadRefTemplate< ThreadInterface >
iferr_scope
#define iferr_scope
定义: resultbase.h:1343
maxon::IOARCHIVEEXTRACTFLAGS::OVERWRITE_EXISTING_FILE
@ OVERWRITE_EXISTING_FILE
Overwrites the file silently if it already exists in the directory.
maxon::IODETECT::FILE
@ FILE
Url is a file.
maxon::UniversalDateTime::GetNow
static UniversalDateTime GetNow()
maxon::GETBROWSEITERATORFLAGS::NONE
@ NONE
No flags specified.
maxon::IOATTRIBUTES
IOATTRIBUTES
Attributes of Url.
定义: ioenums.h:101
maxon::IOARCHIVESTOREMETHOD
IOARCHIVESTOREMETHOD
Compression methods for files in archives.
定义: ioarchivehandler.h:22
maxon::IOARCHIVESTOREMETHOD::DEFLATE
@ DEFLATE
Deflates the file in the archive.

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1