简体   繁体   English

解压android中的7z文件

[英]Extract 7z file in android

I am working on android application that requires extracting.7z files.我正在开发需要提取.7z 文件的 android 应用程序。 I am getting error at the first step.我在第一步遇到错误。 All the parameters look fine in function call. function 调用中的所有参数看起来都很好。 I would be very thankful if someone could share their experience working with code like this.如果有人可以分享他们使用这样的代码的经验,我将非常感激。

Code snippet -代码片段 -

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;
            }

Problem at line -在线问题 -

SevenZFile sevenZFile = new SevenZFile(inpFile);

Logs -日志 -

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

build.gradle - build.gradle -

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

I am working on android application that requires extracting.7z files.我正在开发需要提取.7z 文件的 android 应用程序。 I am getting error at the first step.我在第一步遇到错误。 All the parameters look fine in function call. function 调用中的所有参数看起来都很好。 I would be very thankful if someone could share their experience working with code like this.如果有人可以分享他们使用这样的代码的经验,我将非常感激。

Is the function to extracting.7z file compatible for the tools ver you use? function to extracting.7z 文件是否与您使用的工具版本兼容? if not, you might be able run it on simulation, but not on the device.如果没有,您可能可以在模拟上运行它,但不能在设备上运行。

With the gradle build system, just add使用 gradle 构建系统,只需添加

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

Here is a sample of code on how to use this to extract all files of a 7Zip archive:这是一个关于如何使用它来提取 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM