Android Question Transmitting an already zipped file from Android via AsyncStreams

I have written a simple response to return the text of a file to the system requesting it. The text file is simple json and it works fine. However, I also need to send a zip file. The requirements do not allow me to send it via FTP, I have to send it via websocket, I am using AsyncStreams. I have reviewed the tutorials and read many threads, but I am having no luck.

The zip file transfers fine, but it is' invalid', it will not open with winzip or gzip, even though the size from the original to the host are the same file size. I looked at the contents of each file, and after the first few bytes, they are different. So there is a conversion/corruption happening.

I suppose I need to work with the bytes of the zip file, not as text?
Has anyone done this before, or does anyone have some suggestions?

B4A Code:
        If File.Exists(filePath, fileName) Then
            If fileName = "config.json" Then
                tempstr = File.ReadString(filePath, fileName)
                Log(File.Size(filePath, fileName))
                Dim strResponse As String = "HTTP/1.1 200 OK" & CRLF & _
                "Content-Type: text/html; charset=utf-8" & CRLF & _
                "Content-Transfer-Encoding: gzip" & CRLF & _
                "Content-Length: " & File.Size(filePath, fileName) & CRLF & _           
                "X-File-Name: " & deviceId & "_" & fileName & CRLF & _
                CRLF & tempstr
                Return strResponse
            Else If fileName.Contains(".zip") Then
                tempstr = File.ReadString(filePath, fileName)
                Dim strResponse As String = "HTTP/1.1 200 OK" & CRLF & _
                "Content-Type: application/zip; charset=IS8859-1" & CRLF & _
                "Content-Length: " & File.Size(filePath, fileName) & CRLF & _
                "X-File-Name: " & deviceId & "_" & fileName & CRLF & _
                "Content-Disposition: attachment; filename=" & fileName & CRLF & _               
                CRLF & tempstr
                Return strResponse   
            End If
            Return "HTTP/1.1 404 NOT FOUND"
        Else
            Return "HTTP/1.1 404 NOT FOUND"
        End If
 

emexes

Expert
Licensed User
charset IS8559-1 won't be helping

guessing it should be ISO8559-1 but not sure if space or hyphen or nothing between ISO and 8559
 
Upvote 0
Success! I had watched the tutorial but couldn’t mentally bridge the gap to a working solution. I decided to try GitHub Copilot (I have a paid subscription in VSCode). Initially, it suggested that what I was attempting wasn't feasible with B4A and recommended using B4J with a web server instead.

However, I was confident B4A could handle it. I experimented by pasting in working code that demonstrated successful text transfers—highlighting that the real issue was handling binary data. After that, Copilot began suggesting more relevant code, which eventually produced a functional solution. I applied a few refinements, and it’s now working as expected.

When accessed directly via a browser URL, the service prompts a file download. When invoked as a REST API, it returns the raw binary content suitable for programmatic consumption.
B4A Code:
Sub SendZipFile(astream As AsyncStreams, filePath As String, fileName As String, deviceId As String) As String
    If File.Exists(filePath, fileName) Then
        ' Send HTTP headers
        Dim headers As String = "HTTP/1.1 200 OK" & CRLF & _
            "Content-Type: application/zip; charset=ISO-8859-1" & CRLF & _
            "Content-Disposition: attachment; filename=" & fileName & CRLF & _
            "X-File-Name: " & deviceId & "_" & fileName & CRLF & _
            "Content-Length: " & File.Size(filePath, fileName) & CRLF & CRLF
        astream.Write(headers.GetBytes("UTF8"))
        ' Send the zip file content in chunks
        Dim in As InputStream = File.OpenInput(filePath, fileName)
        Dim buffer(1024) As Byte
        Try
            Do While True
                Dim bytesRead As Int = in.ReadBytes(buffer, 0, buffer.Length)
                If bytesRead > 0 Then
                    astream.Write(buffer)
                Else
                    Exit
                End If
            Loop
        Catch
            Log("End of Loop")
        End Try
        in.Close
    Else
        SendNotFound(astream)
    End If
    Return "HTTP/1.1 200 OK"
End Sub

Sub SendNotFound(astream As AsyncStreams)
    Dim response As String = "HTTP/1.1 404 Not Found" & CRLF & _
         "Content-Type: text/plain" & CRLF & CRLF & _
         "File not found"
    astream.Write(response.GetBytes("UTF8"))
End Sub
 
Upvote 0
Top