7-Zip-JBinding-4Android icon indicating copy to clipboard operation
7-Zip-JBinding-4Android copied to clipboard

Specific questions

Open Arashvscode opened this issue 2 years ago • 9 comments

Hello, I use your library, the code compiles well, but the input and output are not clear. Can you help me? I really need your help. me use in code

package com.myapp;

import android.content.Context;
import android.util.Log;

import android.widget.Toast;
import net.sf.sevenzipjbinding.ArchiveFormat;
import net.sf.sevenzipjbinding.ExtractAskMode;
import net.sf.sevenzipjbinding.ExtractOperationResult;
import net.sf.sevenzipjbinding.IArchiveExtractCallback;
import net.sf.sevenzipjbinding.IArchiveOpenCallback;
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.ISequentialOutStream;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.SevenZipException;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class TestExtract {
    private static final String TAG = "TestExtract";
	private static Context context;

    public void testExtract(File file ,Context context) {
        try {
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
            RandomAccessFileInStream inStream = new RandomAccessFileInStream(randomAccessFile);
            ArchiveOpenCallback callback = new ArchiveOpenCallback();
            IInArchive inArchive = SevenZip.openInArchive(ArchiveFormat.ZIP, inStream, callback);
			

            ArchiveExtractCallback extractCallback = new ArchiveExtractCallback();
            inArchive.extract(null, false, extractCallback);

            inArchive.close();
            inStream.close();
        } catch (FileNotFoundException e) {
            Log.e(TAG, e.getMessage());
        } catch (SevenZipException e) {
            Log.e(TAG, e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
        }
    }


    private class ArchiveOpenCallback implements IArchiveOpenCallback {
        @Override
        public void setTotal(Long files, Long bytes) {
			Toast.makeText(context,"Reding",2).show();
            Log.i(TAG, "Archive open, total work: " + files + " files, " + bytes + " bytes");
        }

        @Override
        public void setCompleted(Long files, Long bytes) {
			Toast.makeText(context,"Done",2).show();
            Log.i(TAG, "Archive open, completed: " + files + " files, " + bytes + " bytes");
        }
    }

    private class ArchiveExtractCallback implements IArchiveExtractCallback {
        @Override
        public ISequentialOutStream getStream(int index, ExtractAskMode extractAskMode) throws SevenZipException {
            Log.i(TAG, "Extract archive, get stream: " + index + " to: " + extractAskMode);
            SequentialOutStream stream = new SequentialOutStream();
            return stream;
        }

        @Override
        public void prepareOperation(ExtractAskMode extractAskMode) throws SevenZipException {
            Log.i(TAG, "Extract archive, prepare to: " + extractAskMode);
        }

        @Override
        public void setOperationResult(ExtractOperationResult extractOperationResult) throws SevenZipException {
            Log.i(TAG, "Extract archive, completed with: " + extractOperationResult);
            if (extractOperationResult != ExtractOperationResult.OK) {
                throw new SevenZipException(extractOperationResult.toString());
            }
        }

        @Override
        public void setTotal(long total) throws SevenZipException {
            Log.i(TAG, "Extract archive, work planned: " + total);
        }

        @Override
        public void setCompleted(long complete) throws SevenZipException {
            Log.i(TAG, "Extract archive, work completed: " + complete);
        }
    }

    private class SequentialOutStream implements ISequentialOutStream {
        @Override
        public int write(byte[] data) throws SevenZipException {
            if (data == null || data.length == 0) {
                throw new SevenZipException("null data");
            }
            Log.i(TAG, "Data to write: " + data.length);
            return data.length;
        }
    }
}

And the code I called it

TestExtract testExtract = new TestExtract();
	   testExtract.testExtract(file,MainActivity.this);

But there is no action and no file is extracted. I will be happy for my help, thanks

Arashvscode avatar Nov 22 '22 19:11 Arashvscode

You will need to store the extracted data somehow, for example by writing to file with FileOutputStream. See changes to example below. (This does not take into account folders in the archive.)

package com.myapp;

import android.content.Context;
import android.util.Log;

import android.widget.Toast;
import net.sf.sevenzipjbinding.ArchiveFormat;
import net.sf.sevenzipjbinding.ExtractAskMode;
import net.sf.sevenzipjbinding.ExtractOperationResult;
import net.sf.sevenzipjbinding.IArchiveExtractCallback;
import net.sf.sevenzipjbinding.IArchiveOpenCallback;
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.ISequentialOutStream;
import net.sf.sevenzipjbinding.PropID;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.SevenZipException;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

public class TestExtract {
    private static final String TAG = "TestExtract";
    private Context context;
    private IInArchive inArchive;

    public void testExtract(File file ,Context context) {
        this.context = context;
        try {
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
            RandomAccessFileInStream inStream = new RandomAccessFileInStream(randomAccessFile);
            ArchiveOpenCallback callback = new ArchiveOpenCallback();
            inArchive = SevenZip.openInArchive(ArchiveFormat.ZIP, inStream, callback);


            ArchiveExtractCallback extractCallback = new ArchiveExtractCallback();
            inArchive.extract(null, false, extractCallback);

            inArchive.close();
            inStream.close();
        } catch (FileNotFoundException e) {
            Log.e(TAG, e.getMessage());
        } catch (SevenZipException e) {
            Log.e(TAG, e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
        }
    }


    private class ArchiveOpenCallback implements IArchiveOpenCallback {
        @Override
        public void setTotal(Long files, Long bytes) {
            Toast.makeText(context,"Reding",Toast.LENGTH_LONG).show();
            Log.i(TAG, "Archive open, total work: " + files + " files, " + bytes + " bytes");
        }

        @Override
        public void setCompleted(Long files, Long bytes) {
            Toast.makeText(context,"Done",Toast.LENGTH_LONG).show();
            Log.i(TAG, "Archive open, completed: " + files + " files, " + bytes + " bytes");
        }
    }

    private class ArchiveExtractCallback implements IArchiveExtractCallback {
        @Override
        public ISequentialOutStream getStream(int index, ExtractAskMode extractAskMode) throws SevenZipException {
            Log.i(TAG, "Extract archive, get stream: " + index + " to: " + extractAskMode);
            String path = inArchive.getStringProperty(index, PropID.PATH);
            SequentialOutStream stream = new SequentialOutStream(path);
            return stream;
        }

        @Override
        public void prepareOperation(ExtractAskMode extractAskMode) throws SevenZipException {
            Log.i(TAG, "Extract archive, prepare to: " + extractAskMode);
        }

        @Override
        public void setOperationResult(ExtractOperationResult extractOperationResult) throws SevenZipException {
            Log.i(TAG, "Extract archive, completed with: " + extractOperationResult);
            if (extractOperationResult != ExtractOperationResult.OK) {
                throw new SevenZipException(extractOperationResult.toString());
            }
        }

        @Override
        public void setTotal(long total) throws SevenZipException {
            Log.i(TAG, "Extract archive, work planned: " + total);
        }

        @Override
        public void setCompleted(long complete) throws SevenZipException {
            Log.i(TAG, "Extract archive, work completed: " + complete);
        }
    }

    private class SequentialOutStream implements ISequentialOutStream {
        private final String path;

        SequentialOutStream(String path) {
            this.path = path;
        }

        @Override
        public int write(byte[] data) throws SevenZipException {
            if (data == null || data.length == 0) {
                throw new SevenZipException("null data");
            }
            Log.i(TAG, "Data to write: " + data.length);

            File f = new File(context.getFilesDir(), path);
            try {
                FileOutputStream os = new FileOutputStream(f);
                os.write(data, 0, data.length);
            } catch (FileNotFoundException e) {
                Log.e(TAG, "write: FileNotFoundException: " + e.getMessage());
            } catch (IOException e) {
                Log.e(TAG, "write: IOException: " + e.getMessage());
            }

            return data.length;
        }
    }
}

omicronapps avatar Nov 29 '22 00:11 omicronapps

Well I used your code


    rd = findViewById(R.id.rd);
		rd.setOnClickListener(v -> {
			TestExtract testExtract = new TestExtract();
			testExtract.testExtract(new File("/storage/emulated/0/Android/cd/apk-debug.zip"), this);
		});


I also used this code to call, but it didn't work

Arashvscode avatar Nov 29 '22 09:11 Arashvscode

For testing this, you can place your zip-file in the 'assets' folder of your project:

  • MyApp\app\src\main\assets\apk-debug.zip

And then run TestExtract.testExtract().

package com.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        File f = fileFromAssets("apk-debug.zip");
        TestExtract testExtract = new TestExtract();
        testExtract.testExtract(f, getApplicationContext());
    }

    private File fileFromAssets(String fileName) {
        File f = new File(getFilesDir() + File.separator + fileName);
        try {
            InputStream is = getAssets().open(fileName);
            int size = is.available();
            byte[] buffer = new byte[size];
            int i = is.read(buffer);
            if (i != size) {
                Log.e("MainActivity", "failed to read file: " + fileName);
            }
            is.close();
            FileOutputStream fos = new FileOutputStream(f);
            fos.write(buffer);
            fos.close();
        } catch (IOException e) {
            Log.e("MainActivity", e.getMessage());
        }
        return f;
    }
}

omicronapps avatar Nov 30 '22 01:11 omicronapps

For testing this, you can place your zip-file in the 'assets' folder of your project:

* MyApp\app\src\main\assets\apk-debug.zip

And then run TestExtract.testExtract().

package com.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        File f = fileFromAssets("apk-debug.zip");
        TestExtract testExtract = new TestExtract();
        testExtract.testExtract(f, getApplicationContext());
    }

    private File fileFromAssets(String fileName) {
        File f = new File(getFilesDir() + File.separator + fileName);
        try {
            InputStream is = getAssets().open(fileName);
            int size = is.available();
            byte[] buffer = new byte[size];
            int i = is.read(buffer);
            if (i != size) {
                Log.e("MainActivity", "failed to read file: " + fileName);
            }
            is.close();
            FileOutputStream fos = new FileOutputStream(f);
            fos.write(buffer);
            fos.close();
        } catch (IOException e) {
            Log.e("MainActivity", e.getMessage());
        }
        return f;
    }
}

I want the vector to be extracted from the personal file path, this code does not work for me

Arashvscode avatar Dec 01 '22 08:12 Arashvscode

No problem. Then just read the file from getFilesDir() or wherever you have stored it. Example below.

Do not use hardcoded paths, such as "/storage/emulated/0/Android/cd/apk-debug.zip". These are not guaranteed to work across different devices.

You can also find more information about how to access files from an application here: https://developer.android.com/training/data-storage/app-specific#java

package com.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        File f = new File(getFilesDir(), "apk-debug.zip");
        TestExtract testExtract = new TestExtract();
        testExtract.testExtract(f, getApplicationContext());
    }
}

omicronapps avatar Dec 03 '22 02:12 omicronapps

Hello, I tested your code, it didn't work. Instead of all this strange and inconsistent complexity, fix your library so that it takes an input file of FileinputSteam and FileOutputSteam. Make coding easier for everyone. I can't understand your code. Because I want to use it in the file manager and let my user implement his own path

Arashvscode avatar Dec 03 '22 22:12 Arashvscode

Great! This library is a straight Android wrapper for the 7-Zip-JBinding Java library, and provides the same functionality and APIs as the Java counterpart. But improvements are encouraged, and you could open a pull requests with your changes at sevenzipjbinding.

omicronapps avatar Dec 04 '22 18:12 omicronapps

Great! This library is a straight Android wrapper for the 7-Zip-JBinding Java library, and provides the same functionality and APIs as the Java counterpart. But improvements are encouraged, and you could open a pull requests with your changes at sevenzipjbinding.

First, I don't understand you at all and I don't know how to use your library in my file manager. Second, I don't have a computer and I use CodeAssist. It can only compile the library and create the average program. I don't think it will be difficult to have an output file, like 7zipj, if I wrote its name correctly

Arashvscode avatar Dec 04 '22 20:12 Arashvscode

This library, "7-Zip-JBinding-4Android", is a port for Android devices of the Java library called "7-Zip-JBinding". The Android version only implements the same functionality as the Java library, nothing more, nothing less.

The Java library is documented here: https://sevenzipjbind.sourceforge.net/javadoc/index.html

The Android library implements the same functionality.

I currently have no plans to improve the Android library beyond the functionality of the Java library.

omicronapps avatar Dec 06 '22 04:12 omicronapps