GZIP ファイルを解凍するジョブを実行している AsyncTask があります。AsyncTasks の進行状況を公開できることを知ったばかりで、以前に作成した他の AsyncTasks でこれをうまく機能させることができました。これを行うには、最大値と現在の進捗値という 2 つの重要な値が必要であることを理解しています。
私が抱えている問題は、入力ストリームからこれらの値を取得する方法がわからないことです。私の現在の AsyncTask は次のようになります。
package com.wizzkidd.myapp.asynctasks;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
public class DecompressGzipAsync extends AsyncTask<String, Integer, Boolean> {
ProgressBar progressBar;
private Context _context;
private View _view;
private String _compressedFilePath;
private String _uncompressedFilePath;
public DecompressGzipAsync(Context context, View view, String compressedFilePath) {
this._context = context;
this._view = view;
this._compressedFilePath = compressedFilePath;
this._uncompressedFilePath = compressedFilePath.substring(0, compressedFilePath.lastIndexOf(".")); //path + filename without gz extension
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... strings) {
Boolean isSuccess = false;
byte[] buffer = new byte[1024];
try {
GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(_compressedFilePath));
FileOutputStream out = new FileOutputStream(_uncompressedFilePath);
//FIXME: I think I need the file size here?
//progressBar.setMax(???);
int len;
while ((len = gzis.read(buffer)) > 0) {
out.write(buffer, 0, len);
//FIXME: I'm not sure what incrementing value can I put in here?
//publishProgress(???);
}
gzis.close();
out.close();
isSuccess = true;
} catch (IOException ex) {
ex.printStackTrace();
}
return isSuccess;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
int currentValue = values[0];
int maxValue = progressBar.getMax();
Log.v("TAG", "Decompressing Progress... currentValue: " + currentValue + " | maxValue: " + maxValue);
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result) {
Log.v("TAG", "Decompressing - Completed");
} else {
Log.v("TAG", "Decompressing - Failed");
}
}
}
値を見つける必要があると思われるコードにコメントしましたが、ここで助けが必要です。どんな助けでも大歓迎です。