在android中提取7z文件



我正在开发需要提取。7z文件的android应用程序。第一步就出现了错误。在函数调用中,所有参数看起来都很好。如果有人能分享他们使用这种代码的经验,我将非常感激。

代码片段-

public void extractFile(String outputFolder, File inpFile) throws IOException {
Log.i("check4","Check4");
SevenZFile sevenZFile = new SevenZFile(inpFile);
Log.i("check","Check1");
SevenZArchiveEntry entry = sevenZFile.getNextEntry();
while (entry  != null){
if (entry.isDirectory()){
continue;
}

line -

SevenZFile sevenZFile = new SevenZFile(inpFile);

日志——

02-18 13:40:39.181 8452-8452/com.example.A7zextract E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.A7zextract, PID: 8452
java.lang.NoSuchMethodError: No virtual method toPath()Ljava/nio/file/Path; in class Ljava/io/File; or its super classes (declaration of 'java.io.File' appears in /system/framework/core-libart.jar)
at org.apache.commons.compress.archivers.sevenz.SevenZFile.<init>(SevenZFile.java:129)
at org.apache.commons.compress.archivers.sevenz.SevenZFile.<init>(SevenZFile.java:370)
at org.apache.commons.compress.archivers.sevenz.SevenZFile.<init>(SevenZFile.java:358)
at com.example.A7zextract.MainActivity.extractFile(MainActivity.java:65)
at com.example.A7zextract.MainActivity.onActivityResult(MainActivity.java:52)
at android.app.Activity.dispatchActivityResult(Activity.java:6428)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-18 13:40:43.155 8452-8452/com.example.A7zextract I/Process: Sending signal. PID: 8452 SIG: 9

构建。gradle——

dependencies {
implementation group: 'org.apache.commons', name: 'commons-compress', version: '1.20'

}

我正在工作的android应用程序,需要提取。7z文件。第一步就出现了错误。所有的参数看起来在函数调用中很好。如果有人能分享,我会非常感激他们处理代码的经验是这样的

提取。7z文件的功能是否与您使用的工具兼容?如果没有,您可以在模拟中运行它,但不能在设备上运行。

使用gradle构建系统,只需添加

compile 'org.apache.commons:commons-compress:1.8'

下面是一个示例代码,说明如何使用它提取7Zip存档中的所有文件:

String myPath = getContentStorage() + "/myArchivePath/";

ZIPFile sevenZFile = new ZIPFile(new File(myPath + "archive.7z"));
ZIPArchiveEntry entry = ZIPFile.getNextEntry();
while(entry!=null){
System.out.println(entry.getName());
FileOutputStream out = new FileOutputStream(myPath + entry.getName());
byte[] content = new byte[(int) entry.getSize()];
ZIPFile.read(content, 0, content.length);
out.write(content);
out.close();
entry = ZIPFile.getNextEntry();
}
ZIPFile.close();

最新更新