My database is small so I don't zip it but I zip some images (mainly to bundle them) and unpack them directly to a location. You should be able to adapt the below code for your database zip file.
I created an AsyncTask that is triggered in my splash screen and will keep the splash screen open until the copy was finished.
The copy process is pretty simple:
protected Void doInBackground(String... params) {
final File dataBaseFile = new File(mDestinationFile);
if (!dataBaseFile.exists()) {
try {
copyFromAssetsToSdcard();
FileUtils.unzip(mContext.getAssets().open("images.zip"), Constants.IMAGE_CACHE_PATH + "/");
} catch (IOException ioe) {
Log.e(LOG_TAG, "Database can not be copied", ioe);
}
} else {
Log.w(LOG_TAG, "Destination database already exists");
}
return null;
}
private void copyFromAssetsToSdcard() throws IOException {
final BufferedInputStream inputStream = new BufferedInputStream(mContext.getAssets().open(mSourceFile));
final OutputStream outputStream = new FileOutputStream(mTmpDestinationFile);
copyStream(inputStream, outputStream);
outputStream.flush();
outputStream.close();
inputStream.close();
File tmpFile = new File(mTmpDestinationFile);
if (tmpFile.renameTo(new File(mDestinationFile))) {
Log.w(LOG_TAG, "Database file successfully copied!");
} else {
Log.w(LOG_TAG, "Database file couldn't be renamed!");
}
}
And my FileUtils.unzip method just unpacks into the specified location:
public static void unzip(InputStream zipInput, String location) throws IOException {
try {
File f = new File(location);
if (!f.isDirectory()) {
f.mkdirs();
}
ZipInputStream zin = new ZipInputStream(zipInput);
try {
ZipEntry ze = null;
final byte[] buffer = new byte[BUFFER_SIZE];
while ((ze = zin.getNextEntry()) != null) {
String path = location + ze.getName();
if (ze.isDirectory()) {
File unzipFile = new File(path);
if (!unzipFile.isDirectory()) {
unzipFile.mkdirs();
}
} else {
FileOutputStream fout = new FileOutputStream(path, false);
try {
int length = zin.read(buffer);
while (length > 0) {
fout.write(buffer, 0, length);
length = zin.read(buffer);
}
zin.closeEntry();
} finally {
fout.close();
}
}
}
} finally {
zin.close();
}
} catch (Exception e) {
Log.e(LOG_TAG, "Unzip exception", e);
}
}