モバイル開発では、インターネットなしで動作するアプリケーションを作成する必要があります。 たとえば、厳しいフィールド条件で使用される辞書や参考書。 次に、アプリケーションを機能させるために、データアーカイブを一度ダウンロードして保存する必要があります。 ネットワークにクエリを実行してこれを行うことができますが、アプリケーション内でデータアーカイブをアーカイブすることもできます。
Google Playの要件に従って、アプリケーションの
apk-ファイルは50 MB以下である必要があり
ます 。2ギガバイトの
.obbアドオン
ファイルを2つ添付することもでき
ます 。 メカニズムはシンプルですが、操作中は複雑なので、50 MB以内に維持しておくのが最善です。 これで、
Zipと
7zの 2つのアーカイブ形式全体が役立ちます。
既製のテストアプリケーション
ZipExampleの例に関する彼らの仕事を見てみましょう。
テスト用に、
sqliteデータベースtest_data.dbが作成されました。 これには、android_metadataの2つのテーブルが含まれています-伝統的に、100万行のmy_test_dataです。

結果のファイルサイズは198 MBです。
2つのアーカイブtest_data.zip(10.1 MB)およびtest_data.7z(3.05 MB)を作成します。
ご覧のとおり、
sqliteデータベースファイルは非常によく圧縮されています。 経験から言えば、ベースの構造が単純であるほど、圧縮率が高くなると言えます。 これらのファイルは両方とも資産フォルダーにあり、操作中に解凍されます。

プログラムの外観は、テキストと2つのボタンがあるウィンドウです。
zipアーカイブを解凍する方法は次のとおりです。
public void onUnzipZip(View v) throws IOException { SimpleDateFormat sdf = new SimpleDateFormat(" HH:mm:ss.SSS"); String currentDateandTime = sdf.format(new Date()); String log = mTVLog.getText().toString() + "\nStart unzip zip" + currentDateandTime; mTVLog.setText(log); InputStream is = getAssets().open("test_data.zip"); File db_path = getDatabasePath("zip.db"); if (!db_path.exists()) db_path.getParentFile().mkdirs(); OutputStream os = new FileOutputStream(db_path); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is)); ZipEntry ze; while ((ze = zis.getNextEntry()) != null) { byte[] buffer = new byte[1024]; int count; while ((count = zis.read(buffer)) > -1) { os.write(buffer, 0, count); } os.close(); zis.closeEntry(); } zis.close(); is.close(); currentDateandTime = sdf.format(new Date()); log = mTVLog.getText().toString() + "\nEnd unzip zip" + currentDateandTime; mTVLog.setText(log); }
ここで展開するクラスは
ZipInputStreamで、
java.util.zipパッケージに含まれており
、そのクラスは標準の
Android SDKに含まれているため、そのまま使用できます。 個別にアップロードする必要はありません。
7zアーカイブを解凍する方法は次のとおりです。
public void onUnzip7Zip(View v) throws IOException { SimpleDateFormat sdf = new SimpleDateFormat(" HH:mm:ss.SSS"); String currentDateandTime = sdf.format(new Date()); String log = mTVLog.getText().toString() + "\nStart unzip 7zip" + currentDateandTime; mTVLog.setText(log); File db_path = getDatabasePath("7zip.db"); if (!db_path.exists()) db_path.getParentFile().mkdirs(); SevenZFile sevenZFile = new SevenZFile(getAssetFile(this, "test_data.7z", "tmp")); SevenZArchiveEntry entry = sevenZFile.getNextEntry(); OutputStream os = new FileOutputStream(db_path); while (entry != null) { byte[] buffer = new byte[8192];
そして彼のアシスタント:
public static File getAssetFile(Context context, String asset_name, String name) throws IOException { File cacheFile = new File(context.getCacheDir(), name); try { InputStream inputStream = context.getAssets().open(asset_name); try { FileOutputStream outputStream = new FileOutputStream(cacheFile); try { byte[] buf = new byte[1024]; int len; while ((len = inputStream.read(buf)) > 0) { outputStream.write(buf, 0, len); } } finally { outputStream.close(); } } finally { inputStream.close(); } } catch (IOException e) { throw new IOException("Could not open file" + asset_name, e); } return cacheFile; }
まず、
assertsからアーカイブファイルをコピーし、次に
SevenZFileを使用して解凍し
ます 。 パッケージ
org.apache.commons.compress.archivers.sevenzにあります。 したがって、使用する前に、
build.gradleで 'org.apache.commons:commons-compress:1.8'のコンパイルを行う必要があります。
Android Stuodio自体がライブラリをダウンロードし、古くなっている場合は、更新があるかどうかを通知します。
実行中のアプリケーションの画面は次のとおりです。

アプリケーションのデバッグバージョンのサイズは
6.8 MBでした。
そして、これは開梱後のデバイス内のサイズです:

質問は、キャッシュの
ブラックボックスに誰がいるかということです。
結論として、アーカイブの解凍には長い時間がかかるため、メイン(
UI )ストリームでは実行できません。 これにより、インターフェイスがフリーズします。 これを回避するには、
AsyncTaskを使用します。
できればバックグラウンドサービスを使用できます。 ユーザーは解凍を待たずに終了することができ、エラーが発生します(ただし、
onPostExecuteメソッドに松葉杖を配置しない場合)。
コメントで建設的な批判ができればうれしいです。