title: Android 11 + Kotlin: Reading a .zip File
body: <p>I've got an Android app written in Kotlin targeting framework 30+, so I'm working within the new <a href="https://developer.android.com/about/versions/11/privacy/storage" rel="nofollow noreferrer">Android 11 file access restrictions</a>. The app needs to be able to open an arbitrary .zip file in the shared storage (chosen interactively by the user) then do stuff with the contents of that .zip file.</p>
  <p>I'm getting a URI for the .zip file in what I'm led to understand is the canonical way:</p>
  <pre><code>    val activity = this
      val getContent = registerForActivityResult(ActivityResultContracts.GetContent()) {
          CoroutineScope(Dispatchers.Main).launch {
              if(it != null) doStuffWithZip(activity, it)
              ...
          }
      }
      getContent.launch(&quot;application/zip&quot;)
  </code></pre>
  <p>My problem is that the <a href="https://developer.android.com/reference/java/util/zip/ZipFile" rel="nofollow noreferrer">Java.util.zip.ZipFile class</a> I'm using only knows how to open a .zip file specified by a <a href="https://developer.android.com/reference/java/lang/String" rel="nofollow noreferrer">String</a> or a <a href="https://developer.android.com/reference/java/io/File" rel="nofollow noreferrer">File</a>, and I don't have any easy way to get to either of those from a <a href="https://developer.android.com/reference/android/net/Uri" rel="nofollow noreferrer">Uri</a>. (I'm guessing that the ZipFile object needs the actual file rather than some kind of stream because it needs to be able to seek...)</p>
  <p>The workaround I'm using at present is to turn the Uri into an <a href="https://developer.android.com/reference/java/io/InputStream" rel="nofollow noreferrer">InputStream</a>, copy the contents to a temp file in private storage, and make a ZipFile instance from that:</p>
  <pre><code>        private suspend fun &lt;T&gt; withZipFromUri(
              context: Context,
              uri: Uri, block: suspend (ZipFile) -&gt; T
          ) : T {
              val file = File(context.filesDir, &quot;tempzip.zip&quot;)
              try {
                  return withContext(Dispatchers.IO) {
                      kotlin.runCatching {
                          context.contentResolver.openInputStream(uri).use { input -&gt;
                              if (input == null) throw FileNotFoundException(&quot;openInputStream failed&quot;)
                              file.outputStream().use { input.copyTo(it) }
                          }
                          ZipFile(file, ZipFile.OPEN_READ).use { block.invoke(it) }
                      }.getOrThrow()
                  }
              } finally {
                  file.delete()
              }
          }
  </code></pre>
  <p>Then, I can use it like this:</p>
  <pre><code>        suspend fun doStuffWithZip(context: Context, uri: Uri) {
              withZipFromUri(context, uri) { // it: ZipFile
                  for (entry in it.entries()) {
                      dbg(&quot;entry: ${entry.name}&quot;) // or whatever
                  }
              }
          }
  </code></pre>
  <p>This works, and (in my particular case, where the .zip file in question is never more than a couple MB) is reasonably performant.</p>
  <p>But, I tend to regard programming by temporary file as the last refuge of the terminally incompetent, thus I can't escape the feeling that I'm missing a trick here. (Admittedly, I <em>am</em> terminally incompetent in the context of Android + Kotlin, but I'd like to learn to not be...)</p>
  <p>Any better ideas? Is there a cleaner way to implement this that doesn't involve making an extra copy of the file?</p>