Skip to content

Commit

Permalink
bug fix - zip archive
Browse files Browse the repository at this point in the history
  • Loading branch information
WirelessAlien committed Dec 16, 2024
1 parent f97af42 commit f0564d9
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_ARCHIVE_ER
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import net.lingala.zip4j.ZipFile
import net.lingala.zip4j.exception.ZipException
Expand All @@ -45,6 +44,8 @@ import net.lingala.zip4j.model.enums.CompressionMethod
import net.lingala.zip4j.model.enums.EncryptionMethod
import net.lingala.zip4j.progress.ProgressMonitor
import java.io.File
import java.nio.file.Files
import java.nio.file.StandardCopyOption

class ArchiveSplitZipService : Service() {

Expand Down Expand Up @@ -136,7 +137,7 @@ class ArchiveSplitZipService : Service() {
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
}

private suspend fun createSplitZipFile(
private fun createSplitZipFile(
archiveName: String,
password: String?,
compressionMethod: CompressionMethod,
Expand Down Expand Up @@ -167,50 +168,61 @@ class ArchiveSplitZipService : Service() {
val progressMonitor = zipFile.progressMonitor

try {
val tempDir = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Files.createTempDirectory("tempArchive").toFile()
} else {
val tempDir = File(cacheDir, "tempArchive")
tempDir.mkdirs()
tempDir
}
val renamedTempDir = File(tempDir.parent, archiveName)
tempDir.renameTo(renamedTempDir)

selectedFiles.forEach { filePath ->
while (progressMonitor.state != ProgressMonitor.State.READY) {
delay(100)
}
val file = File(filePath)
val relativePath = file.relativeTo(baseDirectory).path
val destFile = File(renamedTempDir, file.relativeTo(baseDirectory).path)
if (file.isDirectory) {
zipFile.createSplitZipFileFromFolder(
file,
zipParameters.apply { rootFolderNameInZip = relativePath },
true,
splitSize
)
destFile.mkdirs()
file.copyRecursively(destFile, overwrite = true)
} else {
zipFile.createSplitZipFile(
listOf(file),
zipParameters.apply { fileNameInZip = relativePath },
true,
splitSize
)
destFile.parentFile?.mkdirs()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Files.copy(file.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
} else {
file.inputStream().use { input ->
destFile.outputStream().use { output ->
input.copyTo(output)
}
}
}
}
if (archiveJob?.isCancelled == true) throw ZipException("Extraction cancelled")
}

zipFile.createSplitZipFileFromFolder(renamedTempDir, zipParameters, true, splitSize)

while (!progressMonitor.state.equals(ProgressMonitor.State.READY)) {
val progress = progressMonitor.percentDone
updateProgress(progress)
}

if (progressMonitor.result == ProgressMonitor.Result.SUCCESS) {
showCompletionNotification("$archiveName.zip created successfully")
sendLocalBroadcast(Intent(ACTION_ARCHIVE_COMPLETE))
} else {
showErrorNotification("Archive creation failed")
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra("error_message", "Archive creation failed: ${progressMonitor.result}"))
}
} catch (e: Exception) {

if (archiveJob?.isCancelled == true) throw ZipException("Extraction cancelled")

renamedTempDir.deleteRecursively()

} catch (e: ZipException) {
e.printStackTrace()
showErrorNotification("Archive creation failed: ${e.message}")
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra("error_message", "Archive creation failed: ${e.message}"))
return
}

while (progressMonitor.state != ProgressMonitor.State.READY) {
val progress = progressMonitor.percentDone
updateProgress(progress)
}

if (progressMonitor.result == ProgressMonitor.Result.SUCCESS) {
showCompletionNotification("$archiveName.zip created successfully")
sendLocalBroadcast(Intent(ACTION_ARCHIVE_COMPLETE))
} else {
showErrorNotification("Archive creation failed")
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra("error_message", "Archive creation failed: ${progressMonitor.result}"))
}

} catch (e: Exception) {
e.printStackTrace()
showErrorNotification("Archive creation failed: ${e.message}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import com.wirelessalien.zipxtract.constant.BroadcastConstants.ACTION_ARCHIVE_ER
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import net.lingala.zip4j.ZipFile
import net.lingala.zip4j.exception.ZipException
Expand All @@ -45,6 +44,8 @@ import net.lingala.zip4j.model.enums.CompressionMethod
import net.lingala.zip4j.model.enums.EncryptionMethod
import net.lingala.zip4j.progress.ProgressMonitor
import java.io.File
import java.nio.file.Files
import java.nio.file.StandardCopyOption

class ArchiveZipService : Service() {

Expand Down Expand Up @@ -134,7 +135,7 @@ class ArchiveZipService : Service() {
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
}

private suspend fun createZipFile(
private fun createZipFile(
archiveName: String,
password: String?,
compressionMethod: CompressionMethod,
Expand Down Expand Up @@ -164,44 +165,61 @@ class ArchiveZipService : Service() {
val progressMonitor = zipFile.progressMonitor

try {
val tempDir = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Files.createTempDirectory("tempArchive").toFile()
} else {
val tempDir = File(cacheDir, "tempArchive")
tempDir.mkdirs()
tempDir
}
val renamedTempDir = File(tempDir.parent, archiveName)
tempDir.renameTo(renamedTempDir)

selectedFiles.forEach { filePath ->
while (progressMonitor.state != ProgressMonitor.State.READY) {
delay(100)
}
val file = File(filePath)
val relativePath = file.relativeTo(baseDirectory).path
when {
file.isDirectory -> zipFile.addFolder(
file,
zipParameters.apply { rootFolderNameInZip = relativePath })

else -> zipFile.addFile(
file,
zipParameters.apply { fileNameInZip = relativePath })
val destFile = File(renamedTempDir, file.relativeTo(baseDirectory).path)
if (file.isDirectory) {
destFile.mkdirs()
file.copyRecursively(destFile, overwrite = true)
} else {
destFile.parentFile?.mkdirs()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Files.copy(file.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
} else {
file.inputStream().use { input ->
destFile.outputStream().use { output ->
input.copyTo(output)
}
}
}
}
}

if (archiveJob?.isCancelled == true) throw ZipException("Extraction cancelled")
zipFile.addFolder(renamedTempDir, zipParameters)

while (!progressMonitor.state.equals(ProgressMonitor.State.READY)) {
val progress = progressMonitor.percentDone
updateProgress(progress)
}

if (progressMonitor.result == ProgressMonitor.Result.SUCCESS) {
showCompletionNotification("$archiveName.zip created successfully")
sendLocalBroadcast(Intent(ACTION_ARCHIVE_COMPLETE))
} else {
showErrorNotification("Archive creation failed")
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra("error_message", "Archive creation failed: ${progressMonitor.result}"))
}

if (archiveJob?.isCancelled == true) throw ZipException("Extraction cancelled")

renamedTempDir.deleteRecursively()

} catch (e: ZipException) {
e.printStackTrace()
showErrorNotification("Archive creation failed: ${e.message}")
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra("error_message", "Archive creation failed: ${e.message}"))
return
}

while (!progressMonitor.state.equals(ProgressMonitor.State.READY)) {
val progress = progressMonitor.percentDone
updateProgress(progress)
}

if (progressMonitor.result == ProgressMonitor.Result.SUCCESS) {
showCompletionNotification("$archiveName.zip created successfully")
sendLocalBroadcast(Intent(ACTION_ARCHIVE_COMPLETE))
} else {
showErrorNotification("Archive creation failed")
sendLocalBroadcast(Intent(ACTION_ARCHIVE_ERROR).putExtra("error_message", "Archive creation failed: ${progressMonitor.result}"))
}

} catch (e: ZipException) {
e.printStackTrace()
showErrorNotification("Archive creation failed: ${e.message}")
Expand Down

0 comments on commit f0564d9

Please sign in to comment.