我有一些junrar的问题,我能够使用junrar提取rar文件,但它很慢,我的应用程序停止响应,直到提取未完成,如果有任何人有解决方案或替代junrar请帮助我。我也使用junrar服务,但同样的结果请帮助... 这是我提取的代码
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();
}
}
答案 0 :(得分:1)
您只需创建一个Thread并将您的提取登录添加到run()方法:
// Download Contents
Thread t = new Thread() {
@Override
public void run() {
//Add extract logic here
}
};
t.start();
答案 1 :(得分:0)
在如下线程中调用提取或其他函数:
Runnable runnable = new Runnable() {
public void run() {
//Call function here
}
};
Thread mythread = new Thread(runnable);
mythread.start();