Java 将progressbar添加到BZip2CompressorInputStream
这是我的代码:Java 将progressbar添加到BZip2CompressorInputStream,java,android,Java,Android,这是我的代码: public void extract(String input_f, String output_f){ int buffersize = 1024; FileInputStream in; try { in = new FileInputStream(input_f); FileOutputStream out = new FileOutputStream(output_f); BZip2Compress
public void extract(String input_f, String output_f){
int buffersize = 1024;
FileInputStream in;
try {
in = new FileInputStream(input_f);
FileOutputStream out = new FileOutputStream(output_f);
BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in);
final byte[] buffer = new byte[buffersize];
int n = 0;
while (-1 != (n = bzIn.read(buffer))) {
out.write(buffer, 0, n);
}
out.close();
bzIn.close();
} catch (Exception e) {
throw new Error(e.getMessage());
}
}
如何添加进度条以提取任务,或者如何获得压缩文件大小?最好的方法是:
使用以下代码进行少量修改:
public void extract(String input_f, String output_f){
int buffersize = 1024;
FileInputStream in;
try {
in = new FileInputStream(input_f);
FileOutputStream out = new FileOutputStream(output_f);
BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in);
final byte[] buffer = new byte[buffersize];
int n = 0;
int total = 0;
while (-1 != (n = bzIn.read(buffer))) {
total += n;
final int percentage=(int)(total*100/lenghtOfFile);
YourActivity.this.runOnUiThread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
progressBar.setProgress(percentage);
}
});
out.write(buffer, 0, n);
}
out.close();
bzIn.close();
} catch (Exception e) {
throw new Error(e.getMessage());
}
}
唯一剩下的是你必须计算总文件大小“唯一剩下的是你必须计算总文件大小”我知道,但“我如何获得压缩文件大小?”什么是输入和输出?如果你有输入流,你可以使用输入流;int lengthofFile=in.available();in.available();结果是0。您能告诉我您使用的是httppost还是http getfile.xml.bz2=50mb unpacket file.xml=300mb吗。