简体   繁体   中英

Extract 7z file in android

I am working on android application that requires extracting.7z files. I am getting error at the first step. All the parameters look fine in function call. 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 -

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

I am working on android application that requires extracting.7z files. I am getting error at the first step. All the parameters look fine in function call. 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? if not, you might be able run it on simulation, but not on the device.

With the gradle build system, just add

       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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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