Hello,
My app receives a ZIP file as a BASE64 encoded text file. It´s just a 80k file with a .db file inside.
The file is encoded by a PHP web app using standard functions. I dont´t have access to this server, so I can't change anything. I just receive the file as is.
I save this file as zipfile.txt to DirDefaultExternal.
I need to know how I can decode this file back to zip.
I have no idea how can I do this, not even in my windows notebook.
I know how to open the ZIP in my app, after it is decoded. I read the file to a string, and tried the Agraham's Encryption lib with no success, the result file is corrupt.
I'm using "UTF8" to decode with this libray. Is this OK?
In a few words, what I'm trying to do is decode in my APP a file that was encoded in BAS64. I have the BASE64 string and need to convert it back to file.
I tested the BASE64 data by decoding it in online tools and the data is 100% OK.
Unfortunately the server sends the data not as a file to download, but as a big text string BASE64 enconded.
I send a URL with my authentication data and receive a pure text result, that I load in a string via http ok.
Using Encription lib and String Utils results in a corrupt file.
I will create and upload later a example project, and the files.
Thank you for your help and for your great work in this community.
It seems that decoding the data to a string is ok. But maybe the problem is the way I'm recording the data to file, using
B4X:
data = "xxxxxxxxxxxxxxxxx" 'Base64 encoded data received from http
Dim b64 As Base64
decoded = b64.DecodeStoS(data,"UTF-8")
File.WriteString(File.DirDefaultExternal,"grades.zip",decoded)
I tried to use Random Access File, but the result was the same.
B4X:
Dim wraf As RandomAccessFile
wraf.Initialize(File.DirDefaultExternal,"grades.zip",False)
WriteUTF(wraf,decoded)
Sub WriteUTF(raf As RandomAccessFile, s As String)
Dim b() As Byte = s.GetBytes("UTF8")
raf.WriteShort(b.Length, raf.CurrentPosition)
raf.WriteBytes(b, 0, b.Length, raf.CurrentPosition)
End Sub
The Base64 data received have 6000 bytes. After decoded the data have 4464. After recording to file, with both methods, the file size is 6632.
And the resulting file is not a ZIP, it's corrupt.
Can it be related to something like binary recording, or decode to bytes array and record this array?
If you have access to some server (you can use also some sort of freehosting) build your own php which will download the file decode it and then you will download it to your app from your custom php file. Here example:
PHP:
<?php
//setup file location
$file = "http://site.com/file.zip";
//get basename
$file2 = basename($file);
//load file to variable
$something = file_get_contents($file);
//decode it
$decoded = base64_decode($something);
//if it's already saved delete it
if(file_exists($file2)){
unlink($file2);
}
//save it
file_put_contents($file2, $decoded);
//redirect to file
header("location: $file2");
exit();
?>
You are right but there a really huge amount of free hosting for example 8 from 10 slovak hosting offer you allow_url_fopen true. Registration of hosting cost you maybe 2minutes. If it will be not allowed on your hosting you simply switch to another one. I have good experiences with moxo.cz free hosting or freehostinghero.com on both is the parameter allow_url_fopen enabled.
@Erel your code worked 100%. Thanks.
This file is generated by a school's ridiculous webserver, and it contains grades of my classe's students.
I'm creating an app so the kids can see their info in their own phones.
I told the system admin to return this data in a simple Json encoded strings, but he's an "expert", and just asked me "why?"
No more problem, It's working now.
Thank you all!