Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zip file not accepting password when pulling entries that are password protected #559

Open
j-lanham opened this issue Aug 12, 2024 · 2 comments

Comments

@j-lanham
Copy link

The code that follows, and sorry it's in Kotlin, produces a "Wrong Password!" error.

                val password = runProps.getProperty("ZipPassword").trim().toCharArray()
                ZipFile(zipFileName).use { zipFile ->
                    val zipFileHeaders = zipFile.fileHeaders
                    val inputStream = FileInputStream(zipFileName.toString())
                    ZipInputStream(inputStream).use { _ ->
                        for (zipEntry in zipFileHeaders) {
                            if (zipEntry != null) {
                                ZipInputStream(zipFile.getInputStream(zipEntry), password).use { zipEntryInputStream ->
                                    val extractedFile = File("$directoryName/${zipEntry.fileName}")
                                    FileOutputStream(extractedFile).use { outputStream ->
                                        val data = ByteArray(2048)
                                        var count = zipEntryInputStream.read(data)
                                        while (count != -1) {
                                            outputStream.write(data, 0, count)
                                            count = zipEntryInputStream.read(data)
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

I have verified the password multiple times, but it always fails with the error including setting the password on the zipfile itself.

@j-lanham
Copy link
Author

Okay, I added the password to the ZipFile object and that fixed the wrong password error. The problem now is, when reading from the zip file stream, it always shows a count of -1, so no data is being read from the file header from the zip file.

@j-lanham
Copy link
Author

Okay, instead of trying to read and write the zip entries, I changed it to a ZipFile.extractFile based on the file name from the ZipEntry and it extracted all of them successfully. Since I was getting the zipstream from the zip entry, shouldn't the read/write functionality work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant