Page 1 of 1

Extract APK Files

Posted: Sun Dec 31, 2017 8:56 pm
by LunaRebirth
I'm trying to extract files in the APK and move them to the internal path.

Here's my code:

Code: Select all

void androidExtractFiles()
{
#ifdef _IRR_ANDROID_PLATFORM_
    ANativeActivity* nativeActivity = Game::state->activity;
    Game::setAndroidInternalPath((std::string)nativeActivity->internalDataPath + "/");
 
    for ( u32 i=0; i < Game::device->getFileSystem()->getFileArchiveCount(); ++i )
    {
        IFileArchive* archive = Game::device->getFileSystem()->getFileArchive(i);
        if ( archive->getType() == EFAT_ANDROID_ASSET )
        {
            archive->addDirectoryToFileList("Files/"); // I do this for all folders and subfolders
            // ...
 
            IFileList* files = (IFileList*)archive->getFileList();
            for (int x = 0; x < files->getFileCount(); x++)
            {
                Game::log("FILE: " + (std::string)files->getFullFileName(x).c_str());
                std::string fName = Game::getAndroidInternalPath() + files->getFullFileName(x).c_str();
                if (!files->isDirectory(x) && Operations::readFile(fName) == 0)
                {
                    Game::log("EXTRACTING.\n");
                    IReadFile* f = archive->createAndOpenFile(files->getFullFileName(x));
                    int len = f->getSize();
                    char* buf = new char[len+1];
                    f->read(buf, len);
                    f->drop();
                    buf[len] = '\0';
                    
                    Operations::createDirectories(fName); // creates any folders that don't exist for the file
                    FILE* fi = fopen(fName.c_str(), "wb");
                    fwrite(buf, 1, len, fi);
                    fclose(fi);
 
                    delete [] buf;
 
                    if (Operations::readFile(fName) != 0)
                        Game::log("Extracted OK.");
                }
            }
            break;
        }
    }
 
#endif // _IRR_ANDROID_PLATFORM_
}
This seems to work for most files, but some of them don't extract correctly, although all the files print "EXTRACTED OK."
When I hash the file and check it against the hash of a file on the server (which should be the exact same files), they are different and I cannot see how.
I know the hashing is working fine, as once the server sends the file to the client and I check the hash again, they are the same. They are not the same only when extracted in the above code.

Assume that Operations::readFile() is correct.

I can't seem to figure out what I'm doing wrong, any suggestions?

Re: Extract APK Files

Posted: Sun Dec 31, 2017 11:05 pm
by CuteAlien
Not really looking wrong. A few lines are a little strange, but should still work

The slightly strange stuff: You don't need to append '0' as those are binary files, so you also don't need len+1 size.. but it should still work as you never write that byte. Also fwrite it's better to use parameter order (buf, len, 1, fi) so you write everything in a single block instead of writing len times a single byte.

Maybe check if your resulting files are same size? (they should be)
Also you can read them after writing and compare byte-by-byte.

Re: Extract APK Files

Posted: Mon Jan 01, 2018 5:27 am
by LunaRebirth
Ah, nevermind.
I found the issue is that Android (and Linux alike) are case-sensitive on files, so the issue is that they weren't hashing correctly due to not opening correctly. Thanks