Search code examples
androidzipunzipfilenotfoundexception

file not found error when unzipping


i have about hundred .jpg files, i uploaded it as .zip to this link and then i'm trying to download it and unzip it in my application,so no problem with downloading but i have the error shown in the picture below when trying to unzip the file using the code below,

any idea why is this happening?

here are what i've already tried to solve the problem

1-getting the .jpg file mentioned in the error and rename it into the zip file(same error) 2- getting the .jpg file mentioned in the error and delete it (now the same error happened to the next .jpg file in the zip ) 3- change the directory (same error) 4- change each file name in the zip (same error) 5- trying to unzip it more than once it unzip an empty folder 6- trying to unzip it in computer (no problem everything is ok) 7- trying to unzip another files from another links (no error everything is ok) 8- re-zip the folder in computer after changing each file name and uploading it to different link (same problem)

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_questions1)
val foldername = "LQuestions"
        val zipFolder = File(Environment.getExternalStorageDirectory(), "السادس.اعدادي/$foldername.zip")
try{
zipFolder.unzip()}
catch(e:Exception){
                    Toast.makeText(applicationContext,e.toString(), Toast.LENGTH_LONG).show()
}

}

fun File.unzip(root: File? = null): Boolean {

    val rootFolder = root
            ?: File(parentFile.absolutePath + File.separator + nameWithoutExtension)
    if (!rootFolder.exists()) {
        rootFolder.mkdirs()
    }

    data class ZipIO(val entry: ZipEntry, val output: File)

    ZipFile(this).use { zip ->
        zip
                .entries()
                .asSequence()
                .map {
                    val outputFile = File(rootFolder.absolutePath + File.separator + it.name)
                    ZipIO(it, outputFile)
                }
                .filter { (entry, output) ->
                    !(entry.isDirectory && (output.exists() || output.mkdirs()))
                }
                .forEach { (entry, output) ->
                    zip.getInputStream(entry).use { input ->
                        output.outputStream().use { output ->
                            input.copyTo(output)
                        }
                    }
                }
    }
    return true
}

enter image description here


Solution

  • i used this library instead, it solved everything