MTA:Eir/FileSystem/atranslator/save: Difference between revisions
Jump to navigation
Jump to search
(Created page with "__NOTOC__ This function attempts to save the contents of an archive into its source file. This is only possible if the source file has been opened as writable. ==Syntax== <syntaxhighlight lang="lua">...") |
mNo edit summary |
||
(One intermediate revision by one other user not shown) | |||
Line 4: | Line 4: | ||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
bool atranslator | bool atranslator:save () | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 21: | Line 21: | ||
local zipTranslator = false; | local zipTranslator = false; | ||
if ( resRoot | if ( resRoot:exists( "archive.zip" ) ) then | ||
zipFile = resRoot | zipFile = resRoot:open( "archive.zip", "rb+" ); | ||
if ( zipFile ) then | if ( zipFile ) then | ||
Line 28: | Line 28: | ||
end | end | ||
else | else | ||
zipFile = resRoot | zipFile = resRoot:open( "archive.zip", "wb+" ); | ||
if ( zipFile ) then | if ( zipFile ) then | ||
zipTranslator = | zipTranslator = fileCreateZIP( zipFile ); | ||
end | end | ||
end | end | ||
Line 45: | Line 45: | ||
-- Clean up the file handle. | -- Clean up the file handle. | ||
zipFile | zipFile:destroy(); | ||
return false; | return false; | ||
end | end | ||
-- Write a random file. | -- Write a random file. | ||
local randFile = zipTranslator | local randFile = zipTranslator:open( tostring( math.random( 0, 1 ) ) .. ".rnd", "wb" ); | ||
randFile.write( | randFile.write( | ||
Line 59: | Line 59: | ||
-- Clean up the file handle. | -- Clean up the file handle. | ||
-- We do not actually have to do this, because all files are closed from a translator when it is destroyed. | -- We do not actually have to do this, because all files are closed from a translator when it is destroyed. | ||
randFile | randFile:destroy(); | ||
-- Save the archive and clean up the handles. | -- Save the archive and clean up the handles. | ||
zipTranslator | zipTranslator:save(); | ||
zipTranslator | zipTranslator:destroy(); | ||
zipFile | zipFile:destroy(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</section> | </section> |
Latest revision as of 00:07, 17 January 2022
This function attempts to save the contents of an archive into its source file. This is only possible if the source file has been opened as writable.
Syntax
bool atranslator:save ()
Returns
This function returns true if the contents of the archive could successfully be saved back into its source file, false otherwise.
Example
Click to collapse [-]
ClientThis snippet writes a .zip archive and saves it in the end. It handles common errors that could happen during archive management.
-- Get a generic translator to the resource instance directory. local resRoot = fileCreateTranslator( "/" ); -- Open or create some .zip archive. local zipFile = false; local zipTranslator = false; if ( resRoot:exists( "archive.zip" ) ) then zipFile = resRoot:open( "archive.zip", "rb+" ); if ( zipFile ) then zipTranslator = fileOpenArchive( zipFile ); end else zipFile = resRoot:open( "archive.zip", "wb+" ); if ( zipFile ) then zipTranslator = fileCreateZIP( zipFile ); end end -- Check that everything was created alright. if not ( zipFile ) then outputDebugString( "could not open or create the archive stream" ); return false; end if not ( zipTranslator ) then outputDebugString( "could not create the .zip translator based on the stream file" ); -- Clean up the file handle. zipFile:destroy(); return false; end -- Write a random file. local randFile = zipTranslator:open( tostring( math.random( 0, 1 ) ) .. ".rnd", "wb" ); randFile.write( [[This is a randomly generated file. Its name is random. ]] .. math.random( 0, 100 ) ); -- Clean up the file handle. -- We do not actually have to do this, because all files are closed from a translator when it is destroyed. randFile:destroy(); -- Save the archive and clean up the handles. zipTranslator:save(); zipTranslator:destroy(); zipFile:destroy();
FileSystem Translator Functions
- open
- exists
- createDir
- chdir
- delete
- copy
- rename
- size
- stat
- relPath
- relPathRoot
- absPath
- absPathRoot
- scanDir
- scanDirEx
- getDirs
- getFiles
- setOutbreakEnabled
- getOutbreakEnabled
- setPathProcessingMode
- getPathProcessingMode
FileSystem Archive Translator Functions
- save (not module)