square / okio

A modern I/O library for Android, Java, and Kotlin Multiplatform.
https://square.github.io/okio/
Apache License 2.0
8.72k stars 1.17k forks source link

Source#readZip() #1462

Open vanniktech opened 3 months ago

vanniktech commented 3 months ago

Counterpart of what I wrote here https://github.com/square/okio/issues/1442#issuecomment-2024801707

When the user is choosing for instance on Android from the file picker a file, I can get an InputStream quite easily. Which I can also call source() on but from there I don't get an easy way of reading it as a zip file. I could as a workaround write it to a file and then call openZip() but I think it's beneficial to have a readZip() directly on a Source so that any source is readable as a Zip.

swankjesse commented 3 months ago

Unfortunately the thing we need to read a zip FileSystem isn’t a Source (streaming) but rather a FileHandle (random access).

vanniktech commented 3 months ago

That makes sense. Does InputStream have random access? If not, how is ZipInputStream able to use an InputStream?

JakeWharton commented 3 months ago

ZipInputStream reads valid entries from the beginning of the stream through to the end. Only once at the end do you reach the central index and can realize that some of the entities you saw were not in the directory and thus were old/fake/malicious.

JakeWharton commented 3 months ago

Still worth doing, in my opinion, as it has its uses too. Just gotta put a big caveat.