gpt4 book ai didi

java - 君拉尔 : Application freezes until extraction is not completed

转载 作者:行者123 更新时间:2023-12-01 13:40:58 28 4
gpt4 key购买 nike

我在 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();

}
}

最佳答案

您只需创建一个线程并将提取登录添加到 run() 方法中:

// Download Contents
Thread t = new Thread() {
@Override
public void run() {
//Add extract logic here
}
};
t.start();

关于java - 君拉尔 : Application freezes until extraction is not completed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20779760/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com