language-extensions/R/src/RLibrarySession.cpp
//--------------------------------------------------------------------------------------------------
// Name: RLibrarySession::GetFileExtension
//
// Description:
//  Gets the correct file extension by looking up the first two magic bytes from the file header
//  and if they are equal to 0x1f and 0x8b, returns '.tar.gz.' Otherwise returns '.zip'.
//
// Returns:
//  The correct extension either .tar.gz or .zip
//
:GetFileExtension
Description:
Gets the correct file extension by looking up the first two magic bytes from the file header
and if they are equal to 0x1f and 0x8b, returns '.tar.gz.' Otherwise returns '.zip'.

The correct extension either .tar.gz or .zip
: GetFileExtension Description : Gets the correct file extension by looking up the first two magic bytes from the file header and if they are equal to 0x1f and 0x8b returns ' . tar . gz . ' Otherwise returns ' . zip ' . The correct extension either . tar . gz or . zip
string RLibrarySession::GetFileExtension(const string &libFilePathStr)
{
	bool isGzip = false;
	ifstream file(libFilePathStr, (ios::binary | ios::in));
	if (file.is_open())
	{
		std::byte header[x_NumberOfMagicBytes] = { static_cast<std::byte>(0) };
		file.read(reinterpret_cast<char *>(header), x_NumberOfMagicBytes);
		 gzip headers start with bytes 0x1f, 0x8b
		isGzip = ((header[0] == x_FirstMagicByte) && (header[1] == x_SecondMagicByte));
	}
	string extension = (isGzip ? sm_TarGzExt : sm_ZipExt);
	return extension;
}
string RLibrarySession :: GetFileExtension ( const string & libFilePathStr ) { bool isGzip = false ; ifstream file ( libFilePathStr , ( ios :: binary | ios :: in ) ) ; if ( file . is_open ( ) ) { std :: byte header [ x_NumberOfMagicBytes ] = { static_cast < std :: byte > ( 0 ) } ; file . read ( reinterpret_cast < char * > ( header ) , x_NumberOfMagicBytes ) ; isGzip = ( ( header [ 0 ] == x_FirstMagicByte ) && ( header [ 1 ] == x_SecondMagicByte ) ) ; } string extension = ( isGzip ? sm_TarGzExt : sm_ZipExt ) ; return extension ; }
Name: RLibrarySession::GetFileExtension
Description:
Gets the correct file extension by looking up the first two magic bytes from the file header
and if they are equal to 0x1f and 0x8b, returns '.tar.gz.'
Name : RLibrarySession :: GetFileExtension Description : Gets the correct file extension by looking up the first two magic bytes from the file header and if they are equal to 0x1f and 0x8b returns ' . tar . gz . '
// File extensions are not stored by CREATE EXTERNAL LIBRARY. // ".tar.gz" source packages will have ".zip" extension // the code below examines the header of the ".zip" files and // renames the file with the proper extension // // gzip headers start with bytes 0x1f, 0x8b //