El Sushiboi
El Sushiboi

Reputation: 464

Create Database from inside a Zip File - Android

I need to create a SQLiteDatabase object from a .db file that is inside a .zip file and store it in a variable WITHOUT extracting the files.

The .zip file will always have the same name and always have only 1 entry, namely the database file.

When I try to create a database file from the .zip file, it gives me back a ZipEntry which cannot be cast to File, which is making things complicated.

Alternatively, when I try to feed the .zip file itself by openDatabase("path-to-zip", null, SQLiteDatabase.OPEN_READONLY)

I get the following error:

file is encrypted or is not a database (code 26)

I've done a lot of research, but I just can't find a solution anywhere. Does anyone know how to do this? Below is my setup:

class DatabaseHelper(private val mContext: Context, factory: SQLiteDatabase.CursorFactory?) :
        SQLiteOpenHelper(mContext, DB_NAME, factory, 1) {

    private lateinit var mDatabase: SQLiteDatabase

    companion object {
        private val PATH: String = "../myZipFile.zip"
        private val DB_NAME = ZipFile(PATH).entries().nextElement().name
    }

    init {
        try {
            mDatabase = SQLiteDatabase.openDatabase(PATH, null, SQLiteDatabase.OPEN_READONLY)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

}

Upvotes: 2

Views: 546

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007534

I need to create a SQLiteDatabase object from a .db file that is inside a .zip file

That is not possible, sorry. SQLite requires a file on the filesystem, not an entry in a ZIP file. Either extract the file, or do not ZIP it in the first place.

Upvotes: 2

Related Questions