-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
add extraction support for - "tar.bz2", "tar.gz", "tar.lz4", "tar.lzm…
…a", "tar.sz", "tar.xz", "tar.z", "tar.zstd"
1 parent
f0564d9
commit 6ffed44
Showing
9 changed files
with
354 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
app/src/main/java/com/wirelessalien/zipxtract/CreateZipFragment.kt
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
app/src/main/java/com/wirelessalien/zipxtract/adapter/FilePathAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright (C) 2023 WirelessAlien <https://github.com/WirelessAlien> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.wirelessalien.zipxtract.adapter | ||
|
||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.Button | ||
import android.widget.TextView | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.wirelessalien.zipxtract.R | ||
import java.io.File | ||
|
||
class FilePathAdapter(private val filePaths: MutableList<String>, private val onDeleteClick: (String) -> Unit) : | ||
RecyclerView.Adapter<FilePathAdapter.ViewHolder>() { | ||
|
||
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | ||
val filePathText: TextView = itemView.findViewById(R.id.textFileName) | ||
val deleteButton: Button = itemView.findViewById(R.id.deleteBtn) | ||
|
||
init { | ||
deleteButton.setOnClickListener { | ||
val position = adapterPosition | ||
if (position != RecyclerView.NO_POSITION) { | ||
val filePath = filePaths[position] | ||
onDeleteClick(filePath) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
val view = LayoutInflater.from(parent.context).inflate(R.layout.list_item_file, parent, false) | ||
return ViewHolder(view) | ||
} | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
val filePath = filePaths[position] | ||
holder.filePathText.text = filePath | ||
|
||
val file = File(filePath) | ||
if (file.isDirectory) { | ||
holder.deleteButton.visibility = View.GONE | ||
} else { | ||
holder.deleteButton.visibility = View.VISIBLE | ||
} | ||
} | ||
|
||
override fun getItemCount(): Int { | ||
return filePaths.size | ||
} | ||
|
||
fun removeFilePath(filePath: String) { | ||
val position = filePaths.indexOf(filePath) | ||
if (position != -1) { | ||
filePaths.removeAt(position) | ||
notifyItemRemoved(position) | ||
notifyItemRangeChanged(position, filePaths.size) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
239 changes: 239 additions & 0 deletions
239
app/src/main/java/com/wirelessalien/zipxtract/service/ExtractCsArchiveService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
/* | ||
* Copyright (C) 2023 WirelessAlien <https://github.com/WirelessAlien> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.wirelessalien.zipxtract.service | ||
|
||
import android.app.Notification | ||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.app.PendingIntent | ||
import android.app.Service | ||
import android.content.Intent | ||
import android.os.Build | ||
import android.os.IBinder | ||
import androidx.core.app.NotificationCompat | ||
import androidx.localbroadcastmanager.content.LocalBroadcastManager | ||
import com.wirelessalien.zipxtract.R | ||
import com.wirelessalien.zipxtract.constant.BroadcastConstants | ||
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_EXTRACTION_COMPLETE | ||
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_EXTRACTION_ERROR | ||
import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_EXTRACT_CANCEL | ||
import com.wirelessalien.zipxtract.constant.BroadcastConstants.EXTRACTION_NOTIFICATION_CHANNEL_ID | ||
import com.wirelessalien.zipxtract.constant.BroadcastConstants.EXTRA_ERROR_MESSAGE | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.launch | ||
import org.apache.commons.compress.compressors.CompressorException | ||
import org.apache.commons.compress.compressors.CompressorStreamFactory | ||
import org.apache.commons.compress.compressors.lzma.LZMACompressorInputStream | ||
import java.io.BufferedInputStream | ||
import java.io.File | ||
import java.io.FileInputStream | ||
import java.io.FileOutputStream | ||
import java.io.IOException | ||
import java.io.InputStream | ||
import java.io.OutputStream | ||
import java.nio.file.Files | ||
|
||
class ExtractCsArchiveService : Service() { | ||
|
||
companion object { | ||
const val NOTIFICATION_ID = 596 | ||
const val EXTRA_FILE_PATH = "file_path" | ||
} | ||
|
||
private var extractionJob: Job? = null | ||
|
||
override fun onBind(intent: Intent): IBinder? = null | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
createNotificationChannel() | ||
} | ||
|
||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | ||
val filePath = intent?.getStringExtra(EXTRA_FILE_PATH) | ||
|
||
if (filePath == null) { | ||
stopSelf() | ||
return START_NOT_STICKY | ||
} | ||
|
||
if (intent.action == ACTION_EXTRACT_CANCEL) { | ||
extractionJob?.cancel() | ||
stopForegroundService() | ||
stopSelf() | ||
return START_NOT_STICKY | ||
} | ||
|
||
startForeground(NOTIFICATION_ID, createNotification(0)) | ||
|
||
extractionJob = CoroutineScope(Dispatchers.IO).launch { | ||
extractArchive(File(filePath)) | ||
} | ||
|
||
return START_NOT_STICKY | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
extractionJob?.cancel() | ||
} | ||
|
||
private fun createCancelIntent(): PendingIntent { | ||
val cancelIntent = Intent(this, ExtractCsArchiveService::class.java).apply { | ||
action = ACTION_EXTRACT_CANCEL | ||
} | ||
return PendingIntent.getService(this, 0, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT) | ||
} | ||
|
||
private fun createNotificationChannel() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
val channel = NotificationChannel( | ||
EXTRACTION_NOTIFICATION_CHANNEL_ID, | ||
"Extraction Service", | ||
NotificationManager.IMPORTANCE_LOW | ||
) | ||
val notificationManager = getSystemService(NotificationManager::class.java) | ||
notificationManager.createNotificationChannel(channel) | ||
} | ||
} | ||
|
||
private fun createNotification(progress: Int): Notification { | ||
val builder = NotificationCompat.Builder(this, EXTRACTION_NOTIFICATION_CHANNEL_ID) | ||
.setContentTitle(getString(R.string.extraction_ongoing)) | ||
.setSmallIcon(R.drawable.ic_notification_icon) | ||
.setProgress(100, progress, progress == 0) | ||
.setOngoing(true) | ||
.addAction(R.drawable.ic_round_cancel, getString(R.string.cancel), createCancelIntent()) | ||
|
||
return builder.build() | ||
} | ||
|
||
private fun sendLocalBroadcast(intent: Intent) { | ||
LocalBroadcastManager.getInstance(this).sendBroadcast(intent) | ||
} | ||
|
||
private fun extractArchive(file: File) { | ||
val buffer = ByteArray(DEFAULT_BUFFER_SIZE) | ||
val outputFile = File(file.parent, file.nameWithoutExtension) | ||
val fin: InputStream | ||
val outStream: OutputStream | ||
|
||
try { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
fin = Files.newInputStream(file.toPath()) | ||
outStream = Files.newOutputStream(outputFile.toPath()) | ||
} else { | ||
fin = FileInputStream(file) | ||
outStream = FileOutputStream(outputFile) | ||
} | ||
|
||
val inStream = BufferedInputStream(fin) | ||
|
||
val compressorInputStream = when (file.extension.lowercase()) { | ||
"lzma" -> LZMACompressorInputStream(inStream) | ||
else -> { | ||
try { | ||
CompressorStreamFactory().createCompressorInputStream(inStream) | ||
} catch (e: CompressorException) { | ||
showErrorNotification(getString(R.string.unsupported_compression_format)) | ||
sendLocalBroadcast(Intent(ACTION_EXTRACTION_ERROR).putExtra(EXTRA_ERROR_MESSAGE, getString(R.string.unsupported_compression_format))) | ||
return | ||
} | ||
} | ||
} | ||
|
||
val totalBytes = file.length() | ||
var bytesRead = 0L | ||
|
||
var n: Int | ||
while (compressorInputStream.read(buffer).also { n = it } != -1) { | ||
if (extractionJob?.isCancelled == true) throw Exception("Extraction cancelled") | ||
outStream.write(buffer, 0, n) | ||
bytesRead += n | ||
val progress = (bytesRead * 100 / totalBytes).toInt() | ||
updateProgress(progress) | ||
} | ||
|
||
outStream.close() | ||
compressorInputStream.close() | ||
|
||
showCompletionNotification() | ||
sendLocalBroadcast(Intent(ACTION_EXTRACTION_COMPLETE)) | ||
|
||
} catch (e: CompressorException) { | ||
e.printStackTrace() | ||
showErrorNotification(e.message ?: getString(R.string.extraction_failed)) | ||
sendLocalBroadcast(Intent(ACTION_EXTRACTION_ERROR).putExtra(EXTRA_ERROR_MESSAGE, e.message ?: getString(R.string.extraction_failed))) | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
showErrorNotification(e.message ?: getString(R.string.extraction_failed)) | ||
sendLocalBroadcast(Intent(ACTION_EXTRACTION_ERROR).putExtra(EXTRA_ERROR_MESSAGE, e.message ?: getString(R.string.extraction_failed))) | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
showErrorNotification(e.message ?: getString(R.string.extraction_failed)) | ||
sendLocalBroadcast(Intent(ACTION_EXTRACTION_ERROR).putExtra(EXTRA_ERROR_MESSAGE, e.message ?: getString(R.string.extraction_failed))) | ||
} | ||
} | ||
|
||
private fun updateProgress(progress: Int) { | ||
val notification = createNotification(progress) | ||
val notificationManager = getSystemService(NotificationManager::class.java) | ||
notificationManager.notify(ExtractArchiveService.NOTIFICATION_ID, notification) | ||
|
||
sendLocalBroadcast(Intent(BroadcastConstants.ACTION_EXTRACTION_PROGRESS).putExtra( | ||
BroadcastConstants.EXTRA_PROGRESS, progress)) | ||
} | ||
|
||
private fun showCompletionNotification() { | ||
stopForegroundService() | ||
val notification = NotificationCompat.Builder(this, EXTRACTION_NOTIFICATION_CHANNEL_ID) | ||
.setContentTitle(getString(R.string.extraction_success)) | ||
.setSmallIcon(R.drawable.ic_notification_icon) | ||
.setAutoCancel(true) | ||
.build() | ||
|
||
val notificationManager = getSystemService(NotificationManager::class.java) | ||
notificationManager.notify(NOTIFICATION_ID + 1, notification) | ||
} | ||
|
||
private fun showErrorNotification(error: String) { | ||
stopForegroundService() | ||
val notification = NotificationCompat.Builder(this, EXTRACTION_NOTIFICATION_CHANNEL_ID) | ||
.setContentTitle(getString(R.string.extraction_failed)) | ||
.setContentText(error) | ||
.setSmallIcon(R.drawable.ic_notification_icon) | ||
.setAutoCancel(true) | ||
.build() | ||
|
||
val notificationManager = getSystemService(NotificationManager::class.java) | ||
notificationManager.notify(NOTIFICATION_ID + 2, notification) | ||
} | ||
|
||
@Suppress("DEPRECATION") | ||
private fun stopForegroundService() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
stopForeground(STOP_FOREGROUND_REMOVE) | ||
} else { | ||
stopForeground(true) | ||
} | ||
val notificationManager = getSystemService(NotificationManager::class.java) | ||
notificationManager.cancel(NOTIFICATION_ID) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters