MTA:Eir/FileSystem/atranslator/save: Difference between revisions

From Multi Theft Auto: Wiki
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.save ()
bool atranslator:save ()
</syntaxhighlight>
</syntaxhighlight>


Line 21: Line 21:
local zipTranslator = false;
local zipTranslator = false;


if ( resRoot.exists( "archive.zip" ) ) then
if ( resRoot:exists( "archive.zip" ) ) then
     zipFile = resRoot.open( "archive.zip", "rb+" );
     zipFile = resRoot:open( "archive.zip", "rb+" );


     if ( zipFile ) then
     if ( zipFile ) then
Line 28: Line 28:
     end
     end
else
else
     zipFile = resRoot.open( "archive.zip", "wb+" );
     zipFile = resRoot:open( "archive.zip", "wb+" );


     if ( zipFile ) then
     if ( zipFile ) then
         zipTranslator = fileCreateArchive( zipFile );
         zipTranslator = fileCreateZIP( zipFile );
     end
     end
end
end
Line 45: Line 45:


     -- Clean up the file handle.
     -- Clean up the file handle.
     zipFile.destroy();
     zipFile:destroy();
     return false;
     return false;
end
end


-- Write a random file.
-- Write a random file.
local randFile = zipTranslator.open( tostring( math.random( 0, 1 ) ) .. ".rnd", "wb" );
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.destroy();
randFile:destroy();


-- Save the archive and clean up the handles.
-- Save the archive and clean up the handles.
zipTranslator.save();
zipTranslator:save();
zipTranslator.destroy();
zipTranslator:destroy();
zipFile.destroy();
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 [-]
Client

This 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

FileSystem Archive Translator Functions

FileSystem File Functions