Skip to content

Commit

Permalink
minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
WirelessAlien committed Feb 27, 2024
1 parent 354dff9 commit 4b15fa8
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 65 deletions.

Large diffs are not rendered by default.

127 changes: 69 additions & 58 deletions app/src/main/java/com/wirelessalien/zipxtract/ExtractFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -450,46 +450,46 @@ class ExtractFragment : Fragment() {
toggleExtractButtonEnabled(false)

lifecycleScope.launch {
val passwordEditText = EditText(requireContext())
passwordEditText.inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD

val passwordDialog = MaterialAlertDialogBuilder(requireContext())
.setTitle(getString(R.string.enter_password))
.setView(passwordEditText)
.setPositiveButton(getString(R.string.extract)) { _, _ ->
val password = passwordEditText.text.toString()

extractSplitZipFile(password, outputDirectory)
}.setNegativeButton(getString(R.string.no_password)) { _, _ ->

extractSplitZipFile(null, outputDirectory)

}
passwordDialog.show()
val inflater = layoutInflater
val dialogView = inflater.inflate(R.layout.password_input_dialog, null)
val passwordEditText = dialogView.findViewById<EditText>(R.id.passwordInput)

val passwordDialog = MaterialAlertDialogBuilder(requireContext(), R.style.MaterialDialog)
.setTitle(getString(R.string.enter_password))
.setView(dialogView)
.setPositiveButton(getString(R.string.extract)) { _, _ ->
val password = passwordEditText.text.toString()
extractSplitZipFile(password, outputDirectory)
}.setNegativeButton(getString(R.string.no_password)) { _, _ ->
extractSplitZipFile(null, outputDirectory)
}
passwordDialog.show()
}
}


private fun isSplitZipFileEncrypted(): Boolean {
val sortedTempFiles = tempFiles.sortedBy { file ->
val fileName = file.name.lowercase()
when {
fileName.lastIndexOf(".zip.") != -1 -> fileName.substringAfterLast(".zip.").toIntOrNull() ?: Int.MAX_VALUE
fileName.lastIndexOf(".z") != -1 -> fileName.substringAfterLast(".z").toIntOrNull() ?: Int.MAX_VALUE
else -> Int.MAX_VALUE
}
}
for (file in sortedTempFiles) {
val zipFile = ZipFile(file)
if (zipFile.isEncrypted) {
return true
}
}
return false
}
// private fun isSplitZipFileEncrypted(): Boolean {
// val sortedTempFiles = tempFiles.sortedBy { file ->
// val fileName = file.name.lowercase()
// when {
// fileName.lastIndexOf(".zip.") != -1 -> fileName.substringAfterLast(".zip.").toIntOrNull() ?: Int.MAX_VALUE
// fileName.lastIndexOf(".z") != -1 -> fileName.substringAfterLast(".z").toIntOrNull() ?: Int.MAX_VALUE
// else -> Int.MAX_VALUE
// }
// }
// for (file in sortedTempFiles) {
// val zipFile = ZipFile(file)
// if (zipFile.isEncrypted) {
// return true
// }
// }
// return false
// }

private fun extractSplitZipFile(password: String?, outputDirectory: DocumentFile?) {
toggleExtractButtonEnabled(false)
binding.progressBar.visibility = View.VISIBLE
binding.progressTextView.visibility = View.VISIBLE

val sortedTempFiles = tempFiles.sortedBy { file ->
val fileName = file.name.lowercase()
Expand Down Expand Up @@ -542,11 +542,18 @@ class ExtractFragment : Fragment() {
})

zipFile.getInputStream(header).use { inputStream ->
val buffer = ByteArray(4096)
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var bytesRead: Int
var totalBytesRead = 0L
val fileSize = header.uncompressedSize

while (inputStream.read(buffer).also { bytesRead = it } != -1) {
bufferedOutputStream.write(buffer, 0, bytesRead)
totalBytesRead += bytesRead

// Calculate and update the progress
val progress = (totalBytesRead * 100 / fileSize).toInt()
updateProgress(progress)
}
bufferedOutputStream.close()
}
Expand All @@ -558,15 +565,20 @@ class ExtractFragment : Fragment() {
updateProgress(progress)
}

// Show the extraction completed snackbar
} catch (e: ZipException) {
lifecycleScope.launch(Dispatchers.Main) {
showExtractionCompletedSnackbar(outputDirectory)
showToast("${getString(R.string.extraction_failed)} ${e.message}")
binding.progressBar.visibility = View.GONE
binding.progressTextView.visibility = View.GONE
}

} catch (e: ZipException) {
showToast("${getString(R.string.extraction_failed)} ${e.message}")
} finally {
toggleExtractButtonEnabled(true)
lifecycleScope.launch(Dispatchers.Main) {
toggleExtractButtonEnabled(true)
binding.progressBar.visibility = View.GONE
binding.progressTextView.visibility = View.GONE
showExtractionCompletedSnackbar(outputDirectory)
}
}
}
}
Expand Down Expand Up @@ -639,6 +651,7 @@ class ExtractFragment : Fragment() {
binding.progressBar.visibility = View.GONE
binding.progressTextView.visibility = View.GONE
toggleExtractButtonEnabled(true)
showExtractionCompletedSnackbar(outputDirectory)
}
} catch (e: IOException) {
if (isNoStorageSpaceException(e)) {
Expand Down Expand Up @@ -702,7 +715,7 @@ class ExtractFragment : Fragment() {
private suspend fun createTempFileFromInputStreamAsync(inputStream: InputStream): File = withContext(Dispatchers.IO) {
val tempFile = File.createTempFile("temp_", ".zip", requireActivity().cacheDir)
FileOutputStream(tempFile).use { outputStream ->
val buffer = ByteArray(4096)
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var count: Int
while (inputStream.read(buffer).also { count = it } != -1) {
outputStream.write(buffer, 0, count)
Expand Down Expand Up @@ -746,7 +759,7 @@ class ExtractFragment : Fragment() {
val bufferedOutputStream = BufferedOutputStream(outputFile?.uri?.let { requireActivity().contentResolver.openOutputStream(it) })

zipFile.getInputStream(header).use { inputStream ->
val buffer = ByteArray(4096)
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var bytesRead: Int
var totalBytesRead = 0L
val fileSize = header.uncompressedSize
Expand All @@ -768,12 +781,7 @@ class ExtractFragment : Fragment() {
val progress = (extractedEntries * 100 / totalEntries)
updateProgress(progress)
}

// Show the extraction completed snackbar
lifecycleScope.launch(Dispatchers.Main) {
showExtractionCompletedSnackbar(outputDirectory)
tempFile.delete()
}
tempFile.delete()

} catch (e: ZipException) {
showToast("${getString(R.string.extraction_failed)} ${e.message}")
Expand All @@ -782,8 +790,10 @@ class ExtractFragment : Fragment() {
binding.progressTextView.visibility = View.GONE
}
} finally {

toggleExtractButtonEnabled(true)
lifecycleScope.launch(Dispatchers.Main) {
toggleExtractButtonEnabled(true)
showExtractionCompletedSnackbar(outputDirectory)
}
}
}
}
Expand Down Expand Up @@ -818,7 +828,7 @@ class ExtractFragment : Fragment() {
outputFile?.uri?.let { uri ->
requireActivity().contentResolver.openOutputStream(uri)
?.use { outputStream ->
val buffer = ByteArray(1024)
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var count: Int
try {
while (tarInputStream.read(buffer).also { count = it } != -1) {
Expand Down Expand Up @@ -857,7 +867,7 @@ class ExtractFragment : Fragment() {

outputFile?.uri?.let { uri ->
requireActivity().contentResolver.openOutputStream(uri)?.use { outputStream ->
val buffer = ByteArray(1024)
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var count: Int
try {
while (bzip2InputStream.read(buffer).also { count = it } != -1) {
Expand Down Expand Up @@ -894,7 +904,7 @@ class ExtractFragment : Fragment() {

outputFile?.uri?.let { uri ->
requireActivity().contentResolver.openOutputStream(uri)?.use { outputStream ->
val buffer = ByteArray(1024)
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var count: Int
try {
while (gzipInputStream.read(buffer).also { count = it } != -1) {
Expand Down Expand Up @@ -924,7 +934,7 @@ class ExtractFragment : Fragment() {
private suspend fun createTemp7zFileInBackground(bufferedInputStream: BufferedInputStream): File = withContext(Dispatchers.IO) {
return@withContext File.createTempFile("temp_", ".7z", requireActivity().cacheDir).apply {
FileOutputStream(this).use { outputStream ->
val buffer = ByteArray(4096)
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var count: Int
while (bufferedInputStream.read(buffer).also { count = it } != -1) {
outputStream.write(buffer, 0, count)
Expand Down Expand Up @@ -974,7 +984,7 @@ class ExtractFragment : Fragment() {

outputFile?.uri?.let { uri ->
requireActivity().contentResolver.openOutputStream(uri)?.use { outputStream ->
val buffer = ByteArray(4096)
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
try {
var count: Int
while (sevenZFile.read(buffer).also { count = it } != -1) {
Expand Down Expand Up @@ -1049,7 +1059,7 @@ class ExtractFragment : Fragment() {

outputFile?.uri?.let { uri ->
requireActivity().contentResolver.openOutputStream(uri)?.use { outputStream ->
val buffer = ByteArray(1024)
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var count: Int
try {
while (xzInputStream.read(buffer).also { count = it } != -1) {
Expand Down Expand Up @@ -1099,7 +1109,7 @@ class ExtractFragment : Fragment() {
outputFile?.uri?.let { uri ->
requireActivity().contentResolver.openOutputStream(uri)
?.use { outputStream ->
val buffer = ByteArray(1024)
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var count: Int
try {
while (jarInputStream.read(buffer).also { count = it } != -1) {
Expand Down Expand Up @@ -1138,7 +1148,7 @@ class ExtractFragment : Fragment() {

outputFile?.uri?.let { uri ->
requireActivity().contentResolver.openOutputStream(uri)?.use { outputStream ->
val buffer = ByteArray(1024)
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var count: Int
try {
while (zInputStream.read(buffer).also { count = it } != -1) {
Expand Down Expand Up @@ -1246,6 +1256,7 @@ class ExtractFragment : Fragment() {
binding.progressBar.visibility = View.GONE
binding.progressTextView.visibility = View.GONE
toggleExtractButtonEnabled(true)
showExtractionCompletedSnackbar(outputDirectory)
}
}
}
Expand Down Expand Up @@ -1280,7 +1291,7 @@ class ExtractFragment : Fragment() {
} else {
outputFile?.uri?.let { uri ->
requireActivity().contentResolver.openOutputStream(uri)?.use { outputStream ->
val buffer = ByteArray(4096)
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var count: Int
try {
FileInputStream(file).use { inputStream ->
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/seven_z_option_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
style="@style/Widget.Material3.TextInputEditText.OutlinedBox"
android:hint="@string/enter_password"/>
</com.google.android.material.card.MaterialCardView>
Expand Down
31 changes: 28 additions & 3 deletions app/src/main/res/layout/zip_settings_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/passwordEditText"
style="@style/Widget.Material3.TextInputEditText.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.Material3.TextInputEditText.OutlinedBox"
android:hint="@string/enter_password"/>
android:hint="@string/enter_password"
android:hyphenationFrequency="none"
android:inputType="textPassword"/>
</com.google.android.material.card.MaterialCardView>

<com.google.android.material.card.MaterialCardView
Expand Down Expand Up @@ -175,10 +177,17 @@
android:layout_margin="5dp"/>


<com.google.android.material.card.MaterialCardView

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="5dp">
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
app:cardCornerRadius="10dp"
android:layout_margin="5dp"
android:layout_weight="8"
style="@style/Widget.Material3.CardView.Outlined"
android:layout_height="wrap_content">

Expand All @@ -192,5 +201,21 @@
android:inputType="number" />
</com.google.android.material.card.MaterialCardView>

<com.google.android.material.card.MaterialCardView
style="@style/Widget.Material3.CardView.Outlined"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_margin="5dp"
android:layout_gravity="center_vertical"
android:layout_weight="1"
app:cardCornerRadius="10dp">

<Spinner
android:id="@+id/splitSizeUnitSpinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:entries="@array/split_size_units" />
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
</LinearLayout>
</ScrollView>
5 changes: 5 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@
<item>Maximum</item>
<item>Ultra</item>
</array>
<array name="split_size_units">
<item>KB</item>
<item>MB</item>
<item>GB</item>
</array>
</resources>
6 changes: 3 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@
<string name="max_available_thread">0 for maximum available threads</string>
<string name="solid_compression">Solid Compression</string>
<string name="enter_name_of_archive_optional">Enter Name of Archive (Optional)</string>
<string name="split_size_mb">Split Size (MB)</string>
<string name="split_size_mb">Split Size</string>
<string name="split_zip">Split Zip</string>
<string name="encryption_strength">Encryption Strength</string>
<string name="_ten">10</string>
<string name="_ten">Default is 64kb</string>
<string name="percent_complete">%d %%</string>
<string name="please_wait_end_of_job">Please Wait...If you have selected large files, it may take a few moments</string>
<string name="please_wait_end_of_job">Please WaitIf you have selected large files, it may take a few moments</string>
</resources>

0 comments on commit 4b15fa8

Please sign in to comment.