MTA:Eir/FileSystem/atranslator/save

From Multi Theft Auto: Wiki
Revision as of 11:54, 1 February 2014 by The GTA (talk | contribs)
Jump to navigation Jump to search

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