Android Question Uploading large text files with compression using multipart/form-data

daemon

Active Member
Licensed User
Longtime User
Hi,

I have to upload large text files to PHP server using multipart/form-data.
However, to save user's data bandwidth, I also want data to be compressed.

I see some modules / libraries in forum for multipart/form-data uploads, but they do not seem to use compression.

Is it possible to achieve on-the-fly compression for HTTP uploads that is transparent to client and server?
Is there any code snippet / module / library for the same?

TIA.
 

daemon

Active Member
Licensed User
Longtime User
Uncompressed text files are around 700 KB each. If I zip them, they get to around 150 KB each.
Compression will save significant bandwidth.
Also, in my app, one such file will be uploaded every minute or so.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can add gzip compression to MultipartPost module with this code:
B4X:
If Files <> Null AND Files.IsInitialized Then
     'write the files
     Dim FD As FileData
     For i = 0 To Files.Size - 1
       FD = Files.Get(i)
       b = ("--" & boundary & EOL & "Content-Disposition: form-data; name=" _
         & QUOTE & FD.KeyName & QUOTE & "; filename=" & QUOTE & FD.FileName & QUOTE _
         & EOL & "Content-Type: "  & FD.ContentType & EOL & EOL).GetBytes("UTF8")
       stream.WriteBytes(b, 0, b.Length)
       Dim in As InputStream
       in = File.OpenInput(FD.Dir, FD.FileName)
       dim cs As CompressedStream '<----------------------- RandomAccessFile library
       in = cs.WrapInputStream(In, "gzip") '<----------------
       File.Copy2(in, stream) 'read the file and write it to the stream
       b = EOL.GetBytes("UTF8")
       stream.WriteBytes(b, 0, b.Length)
     Next
   End If
 
Upvote 0

daemon

Active Member
Licensed User
Longtime User
Oh, may be my question wasn't clear.

The file that I want to upload is uncompressed and let's assume its size is 700 KB.
If I upload it as it is, it will use 700 KB worth of bandwidth.
I want to reduce bandwidth usage.

I was wondering if one can use HTTP compression methods (Transfer-Encoding: gzip or Content-Encoding: gzip) for HTTP transfer to save user bandwidth.
 
Upvote 0
Top