Created
May 30, 2018 10:04
-
-
Save voghDev/ad86c0f2b6a953802e38ac6b8ed5823e to your computer and use it in GitHub Desktop.
Android Service that extracts a Zip File into a folder specified on extras
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
/* | |
* Copyright (C) 2018 Olmo Gallegos Hernández. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.patrimonioglobalgra.audioguiasgranada.global.service | |
import android.app.Activity.RESULT_CANCELED | |
import android.app.Activity.RESULT_OK | |
import android.app.IntentService | |
import android.content.Intent | |
import android.content.Intent.EXTRA_RETURN_RESULT | |
import java.io.* | |
import java.util.zip.ZipEntry | |
import java.util.zip.ZipInputStream | |
class ExtractZipService(val name: String?) : IntentService(name) { | |
companion object { | |
val EXTRA_DESTINATION_PATH = "destinationPath" | |
val EXTRA_ZIP_FILE = "zipFile" // e.g. "monuments.zip" | |
val ACTION_EXTRACT_ZIP = "extractZip" | |
} | |
override fun onHandleIntent(intent: Intent?) { | |
val path = intent?.getStringExtra(EXTRA_DESTINATION_PATH) ?: "" | |
val file = intent?.getStringExtra(EXTRA_ZIP_FILE) ?: "" | |
val result = unpackZip(path, file) | |
notifyServiceResult(ACTION_EXTRACT_ZIP, if (result) RESULT_OK else RESULT_CANCELED) | |
} | |
private fun unpackZip(path: String, zipName: String): Boolean { | |
try { | |
val absolutePath = "$path${File.separator}$zipName" | |
var fis = FileInputStream(absolutePath) | |
var inputStream: InputStream = fis | |
var zipInputStream = ZipInputStream(BufferedInputStream(inputStream)) | |
var buffer = ByteArray(1024) | |
var zipEntry: ZipEntry = zipInputStream.nextEntry | |
while (zipEntry != null) { | |
val filename = zipEntry.name | |
val zipEntryAbsolutePath = "$path${File.separator}$filename" | |
if (zipEntry.isDirectory) { | |
val dir = File(zipEntryAbsolutePath) | |
dir.mkdirs() | |
} else { | |
val fos = FileOutputStream(zipEntryAbsolutePath) | |
var count = zipInputStream.read(buffer) | |
while (count != -1) { | |
fos.write(buffer, 0, count) | |
count = zipInputStream.read(buffer) | |
} | |
fos.close() | |
} | |
zipInputStream.closeEntry() | |
zipEntry = zipInputStream.nextEntry | |
} | |
} catch (e: IOException) { | |
e.printStackTrace() | |
return false | |
} | |
return true | |
} | |
private fun notifyServiceResult(action: String, result: Int = RESULT_OK) { | |
val intent = Intent(action).apply { | |
putExtra(EXTRA_RETURN_RESULT, result) | |
} | |
sendBroadcast(intent) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment