Java JUNRAR:应用程序冻结,直到提取未完成
我对junrar有一些问题,我可以使用junrar提取rar文件,但速度很慢,我的应用程序停止响应,直到提取未完成。如果有人有junrar的解决方案或替代方案,请帮助我。我也在服务中使用了junrar,但同样的结果请帮助。。。 这是我要提取的代码Java JUNRAR:应用程序冻结,直到提取未完成,java,android,Java,Android,我对junrar有一些问题,我可以使用junrar提取rar文件,但速度很慢,我的应用程序停止响应,直到提取未完成。如果有人有junrar的解决方案或替代方案,请帮助我。我也在服务中使用了junrar,但同样的结果请帮助。。。 这是我要提取的代码 private static File createFile(FileHeader fh, File destination) { File f = null; String name = null; if (fh.isFile
private static File createFile(FileHeader fh, File destination) {
File f = null;
String name = null;
if (fh.isFileHeader() && fh.isUnicode()) {
name = fh.getFileNameW();
} else {
name = fh.getFileNameString();
}
f = new File(destination, name);
if (!f.exists()) {
try {
f = makeFile(destination, name);
} catch (IOException e) {
Log.e("error creating the new file: " ,f.getName(), e);
}
}
return f;
}
private static File makeFile(File destination, String name)
throws IOException {
String[] dirs = name.split("\\\\");
if (dirs == null) {
return null;
}
String path = "";
int size = dirs.length;
if (size == 1) {
return new File(destination, name);
} else if (size > 1) {
for (int i = 0; i < dirs.length - 1; i++) {
path = path + File.separator + dirs[i];
new File(destination, path).mkdir();
}
path = path + File.separator + dirs[dirs.length - 1];
File f = new File(destination, path);
f.createNewFile();
return f;
} else {
return null;
}
}
public void extractRar(File filerar,final String location)
{
Archive a = null;
try {
a = new Archive(new FileVolumeManager(filerar));
} catch (RarException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (a != null) {
a.getMainHeader().print();
FileHeader fh = a.nextFileHeader();
while (fh != null) {
try {
if(fh.isDirectory())
{
Log.e("directory", fh.getFileNameString());
if(fh.getFileNameString().contains("."))
{
directory=fh.getFileNameString();
Log.e("Directory name",directory);
}
}else{
file=fh.getFileNameString();
Log.e("File name",file);
File out = createFile(fh, new File(location));
Log.e("observe file",location+file.substring(0, file.indexOf("\\main")).replace("\\", "/"));
final Timer mytimer=new Timer();
mytimer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
if(new File(location+file.replace("\\", "/")).exists())
Log.e("size", size(new File(location+file.replace("\\", "/")).length()));
intent.putExtra("file_length", new File(location+file.replace("\\", "/")).length());
intent.putExtra("status", "extracting");
getApplicationContext().sendBroadcast(intent);
}
}, 0, 50);
System.out.println(out.getAbsolutePath());
FileOutputStream os = new FileOutputStream(out);
a.extractFile(fh, os);
mytimer.cancel();
os.close();}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RarException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fh = a.nextFileHeader();
}
intent.putExtra("status", "completed");
getApplicationContext().sendBroadcast(intent);
Toast.makeText(getApplicationContext(), "Successfully Extracted", Toast.LENGTH_LONG).show();
}
}
私有静态文件createFile(文件头fh,文件目标){
文件f=null;
字符串名称=null;
if(fh.isFileHeader()和&fh.isUnicode()){
name=fh.getFileNameW();
}否则{
name=fh.getFileNameString();
}
f=新文件(目的地、名称);
如果(!f.exists()){
试一试{
f=生成文件(目的地、名称);
}捕获(IOE异常){
Log.e(“创建新文件时出错:”,f.getName(),e);
}
}
返回f;
}
私有静态文件makeFile(文件目标,字符串名称)
抛出IOException{
字符串[]dirs=name.split(“\\\”);
如果(dirs==null){
返回null;
}
字符串路径=”;
int size=dirs.length;
如果(大小==1){
返回新文件(目的地、名称);
}否则,如果(大小>1){
对于(int i=0;i
调用线程内的提取或其他函数,如下所示:
Runnable runnable = new Runnable() {
public void run() {
//Call function here
}
};
Thread mythread = new Thread(runnable);
mythread.start();
您只需创建一个线程并将提取登录添加到run()方法中:
// Download Contents
Thread t = new Thread() {
@Override
public void run() {
//Add extract logic here
}
};
t.start();