Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
[#68] fix support for rar < 5 archives
Browse files Browse the repository at this point in the history
  • Loading branch information
lfuelling committed Apr 24, 2020
1 parent e6ef9fc commit dfe9737
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 23 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ dependencies {
implementation 'org.tukaani:xz:1.8'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.core:core:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.github.junrar:junrar:4.0.0'
testImplementation 'junit:junit:4.13'
testImplementation 'org.mockito:mockito-core:3.3.3'
androidTestImplementation('androidx.test.espresso:espresso-core:3.2.0', {
Expand Down
13 changes: 9 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@

<application
android:name=".LrkFMApp"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:restoreAnyVersion="true"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning"
android:allowBackup="false" >
tools:ignore="GoogleAppIndexingWarning">

<meta-data
android:name="android.max_aspect"
android:value="ratio_float" />
Expand All @@ -28,12 +29,14 @@
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />

<data android:scheme="file" />
<data android:scheme="content" />
<data android:host="*" />
Expand All @@ -49,10 +52,12 @@
android:name=".activities.IntroActivity"
android:label="@string/title_activity_intro"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".activities.SettingsActivity"
<activity
android:name=".activities.SettingsActivity"
android:label="@string/title_activity_preference">
<intent-filter>
<action android:name="android.intent.action.APPLICATION_PREFERENCES" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/io/lerk/lrkFM/activities/IntroActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;

import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.core.app.ActivityCompat;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import io.lerk.lrkFM.R;
import io.lerk.lrkFM.activities.file.FileActivity;
Expand Down Expand Up @@ -35,7 +37,7 @@ protected void onCreate(Bundle savedInstanceState) {
final boolean permissionGranted = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;

FloatingActionButton fab = findViewById(R.id.fab);
if(permissionGranted) {
if (permissionGranted) {
fab.setImageResource(R.drawable.ic_chevron_right_white_24dp);
}
fab.setOnClickListener(view -> {
Expand Down
58 changes: 44 additions & 14 deletions app/src/main/java/io/lerk/lrkFM/tasks/archive/ArchiveUtil.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package io.lerk.lrkFM.tasks.archive;

import android.net.IpSecManager;
import android.os.Looper;
import android.util.Log;

import com.github.junrar.Junrar;
import com.github.junrar.exception.RarException;

import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
Expand Down Expand Up @@ -35,15 +39,37 @@ class ArchiveUtil {

private static final String TAG = ArchiveUtil.class.getCanonicalName();

boolean doExtractArchive(String path, FMFile f) throws BlockingStuffOnMainThreadException {
boolean doExtractArchive(String destination, FMFile f) throws BlockingStuffOnMainThreadException {
if (Looper.myLooper() == Looper.getMainLooper()) {
throw new BlockingStuffOnMainThreadException();
}
AtomicBoolean result = new AtomicBoolean(false);

FileType fileType = f.getFileType();
if (fileType != FileType.ARCHIVE_P7Z) {
fileType.newHandler(getArchiveExtractionCallback(path, result, fileType)).handle(f);
if(fileType == FileType.ARCHIVE_RAR) {
fileType.newHandler(fmFile -> {
File archiveFile = fmFile.getFile();
File destinationFile = new File(destination);
if (!destinationFile.exists()) {
if (destinationFile.mkdirs()) {
Log.i(TAG, "Folder created: '" + destinationFile.getAbsolutePath() + "'");
} else {
Log.e(TAG, "Unable to create folder: '" + destinationFile.getAbsolutePath() + "'");
}
}
try {
Junrar.extract(archiveFile, destinationFile);
result.set(true);
} catch (RarException | IOException e) {
Log.e(TAG, "Unable to extract rar file '" + fmFile.getAbsolutePath() + "'!", e);
result.set(false);
}
}).handle(f);
} else {
// handle archives using commons compress
fileType.newHandler(getArchiveExtractionCallback(destination, result, fileType)).handle(f);
}
} else {
fileType.newHandler(fi -> {
SevenZFile sevenZFile;
Expand All @@ -54,20 +80,24 @@ boolean doExtractArchive(String path, FMFile f) throws BlockingStuffOnMainThread
if (entry.isDirectory()) {
continue;
}
File curfile = new File(path, entry.getName());
File curfile = new File(destination, entry.getName());
File parent = curfile.getParentFile();
if (!parent.exists()) {
if (parent.mkdirs()) {
Log.i(TAG, "Folder created: '" + parent.getAbsolutePath() + "'");
} else {
Log.e(TAG, "Unable to create folder: '" + parent.getAbsolutePath() + "'");
if (parent != null) {
if (!parent.exists()) {
if (parent.mkdirs()) {
Log.i(TAG, "Folder created: '" + parent.getAbsolutePath() + "'");
} else {
Log.e(TAG, "Unable to create folder: '" + parent.getAbsolutePath() + "'");
}
}
FileOutputStream out = new FileOutputStream(curfile);
byte[] content = new byte[(int) entry.getSize()];
sevenZFile.read(content, 0, content.length);
out.write(content);
out.close();
} else {
throw new IOException("Unable to get parent file for '" + curfile.getAbsolutePath() + "'!");
}
FileOutputStream out = new FileOutputStream(curfile);
byte[] content = new byte[(int) entry.getSize()];
sevenZFile.read(content, 0, content.length);
out.write(content);
out.close();
}
result.set(true);
} catch (IOException e) {
Expand Down Expand Up @@ -120,7 +150,7 @@ private Handler<FMFile> getArchiveExtractionCallback(String path, AtomicBoolean
result.set(true);
}
} catch (IOException | ArchiveException e) {
Log.e(TAG, "Error extracting " + fileType.getExtension());
Log.e(TAG, "Error extracting " + fileType.getExtension(), e);
result.set(false);
}

Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<string name="err_activity_null">Unable to register context menu</string>
<string name="warn_file_exists_title">File exists</string>
<string name="warn_file_exists_msg">The existing file will be overwritten. Are you sure that you want to continue?</string>
<string name="yes">@android:string/yes</string>
<string name="yes">Yes</string>
<string name="delete">Delete</string>
<string name="warn_delete_msg">Are you sure that you want to delete: </string>
<string name="move">Move</string>
Expand Down Expand Up @@ -188,4 +188,6 @@
<string name="pref_vibration_length">Vibration Length</string>
<string name="pref_vibration_length_desc">Configure the length of vibration when toasts appear. Currently:</string>
<string name="pref_vibration_length_desc_init">Configure the length of vibration when toasts appear.</string>
<string name="pref_use_storage_access_framework">Use Storage Access Framework</string>
<string name="pref_use_storage_access_framework_desc"><![CDATA[Use the Android file picker instead of builtin. Helps on Android > 9.]]></string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
public class FileActivityTest {

private static final String PATH_SE0 = "/storage/emulated/0";
private static final String PATH_SE0 = "/storage";

//TODO: learn Mockito and implement tests!

Expand Down

0 comments on commit dfe9737

Please sign in to comment.