【发布时间】:2015-01-18 06:25:07
【问题描述】:
我正在使用 XZ Java 库在 Android 上提取大小约为 16MB 的 .xz 文件。我将提取/解压缩代码作为AsyncTask 运行,因此我想通过onProgressUpdate(Integer ... values) 方法查看提取的百分比。
我的解压代码看起来像这样。
byte[] buf = new byte[8192];
String name = null;
try {
name = "my_archive.xz";
InputStream in = getResources().openRawResource(R.raw.my_archive);//new FileInputStream(name); //
FileOutputStream out = openFileOutput("my_archive.sqlite", Context.MODE_PRIVATE);
label = (TextView)findViewById(R.id.textLabel);
try {
in = new XZInputStream(in);
label.setText("Writing db file.");
int size;
while ((size = in.read(buf)) != -1) {
out.write(buf,0,size);
progress++;
publishProgress(progress);
}
}
catch (Exception e)
{
System.err.println("Input Stream error: "+e.getMessage());
}
finally {
// Close FileInputStream (directly or indirectly
// via LZMAInputStream, it doesn't matter).
in.close();
}
} catch (FileNotFoundException e) {
System.err.println("LZMADecDemo: Cannot open " + name + ": "
+ e.getMessage());
System.exit(1);
} catch (EOFException e) {
System.err.println("LZMADecDemo: Unexpected end of input on "
+ name);
System.exit(1);
} catch (IOException e) {
System.err.println("LZMADecDemo: Error decompressing from "
+ name + ": " + e.getMessage());
System.exit(1);
}
progress 变量实际上应该保存百分比值。如果有人使用过这个库,如果你想出了一个简单的方法来计算进度百分比,请在这里帮助我。
提前感谢您的帮助。
【问题讨论】:
标签: java android android-asynctask xz