diff --git a/app/src/main/java/com/wirelessalien/zipxtract/service/ArchiveSplitZipService.kt b/app/src/main/java/com/wirelessalien/zipxtract/service/ArchiveSplitZipService.kt index 475baf2..cff1d46 100644 --- a/app/src/main/java/com/wirelessalien/zipxtract/service/ArchiveSplitZipService.kt +++ b/app/src/main/java/com/wirelessalien/zipxtract/service/ArchiveSplitZipService.kt @@ -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 @@ -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() { @@ -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, @@ -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}") diff --git a/app/src/main/java/com/wirelessalien/zipxtract/service/ArchiveZipService.kt b/app/src/main/java/com/wirelessalien/zipxtract/service/ArchiveZipService.kt index 98a273d..35eabd1 100644 --- a/app/src/main/java/com/wirelessalien/zipxtract/service/ArchiveZipService.kt +++ b/app/src/main/java/com/wirelessalien/zipxtract/service/ArchiveZipService.kt @@ -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 @@ -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() { @@ -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, @@ -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}")