"Truncated ZIP file" error with Apache POI and akka-http

For my current SaaS project, I need to be able to upload and parse an excel file. I am using akkt-http for the server side routing table and Apache POI to parse the excel file. The general flow of the program was implemented quickly, but I kept getting the error "Truncated ZIP file" from time to time.

At first I thought this was due to the excel file being manipulated while I was re-uploading it in the meantime. I suspected that the browser was trying to be too clever and it would cache the file in some way. This was supported by the fact that I could get it working sometimes by clicking around in the open-file dialog and change folders back and forth.

At last, I realised that the problem was occuring more frequently with the live version. Thus, I started to play around with Chrome's feature to throttle the network speed (and thus simulate a slow connection). And indeed, the error popped up right away.

While rearranging my code a bit and trying out various things, I got a warning message, saying

Sending an 2xx 'early' response before end of request for ...

So, it looked like akka-http tried to be too clever by prematurely responding (and closing the connection) to the client.

Further looking for other people with the same problem and the same warning, I found a solution suggestion to use toStrictEntity. That helped and solved my problem!

So, if you ever get the Truncated ZIP file with akka-http and Apache POI, try something like the following

path("upload") {
  post {
    // Next line prevents 'Truncated ZIP file' error
    toStrictEntity(10 seconds) {
      fileUpload("excel-file") {
        case (_, bytes) =>
          val resultF = processExcelFile(bytes)
          completeFutureResult(resultF)
      }
    }
  }
},

Just know that this disables any fancy streaming feature. But at least in my case it's better than having nothing working.

Previous
Previous

Adding Canonical URLs to a Remix website

Next
Next

Creating a responsive menu with remix and tailwind