0

I am downloading a zip file from the internet and I get it as a plain text string. I now want to save this to the disk, but how should I convert this text string to a file buffer? What is needed to be done so that this string can be used in a filesystem write function?

EDIT:

The problem when I write the data directly to the disk is that the zip file becomes corrupted.

The size of the downloaded file is 34712 bytes, however when I save it directly to the disk it get the size of 64760 bytes.

The first 5 lines of the files:

The new created file as binary:
504b 0304 1400 0200 0800 47fd 3a3d 3efd
fd57 fd5b 0000 006a 0000 0c00 0000 6963
6f6e 7365 7874 2e65 7865 fdfd 7758 fd4b
1738 fdfd fdfd 2424 fa63 fdfd 4140 fd45
5a30 4840 6a28 2140 0811 1240 050c fd05

The new created file as utf8:
504b 0304 1400 0200 0800 47ef bfbd 3a3d
3eef bfbd efbf bd57 efbf bd5b 0000 006a
0000 0c00 0000 6963 6f6e 7365 7874 2e65
7865 efbf bdef bfbd 7758 efbf bd4b 1738
efbf bdef bfbd efbf bdef bfbd 2424 f48e


Original noncorrupted file from the internet:
504b 0304 1400 0200 0800 4799 3a3d 3eae
b857 865b 0000 006a 0000 0c00 0000 6963
6f6e 7365 7874 2e65 7865 edfd 7758 934b
1738 8abe e981 2424 f48e a1a3 b4d0 4140
e945 5a30 4840 6a28 2140 0811 1240 050c
1
  • 1
    What kind of text string? If it's the binary data attempting to be displayed as text, you'd get some very weird characters. Most likely, it's Base64, which you'll have to decode. But the encoding depends entirely on the source. – Bob Mar 11 '14 at 12:41
0

Why you can't just download file?

If you insist to download stream just redirect what you are getting to the file, so:

*nix:

output_of_download_command > /path/to/file_name.zip

Windows:

output_of_download_command > c:\path\to\file_name.zip

Mind you, you might have some extra information before and/after after actual string! Without knowing exactly what you are doing it's impossible to give specific command but you can redirect all errors to different place:

Please see here:

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true

2
  • Thanks, the issue though is that the resulting zip file is twice as big as the original that was uploaded to the internet, and it is also corrupted. I believe this has something to do with the encoding, which is utf8. Should the zip string be decoded/encoded or something before saved? – richie Mar 11 '14 at 12:37
  • A zip file can't have utf-8 as encoding. Utf-8 is an encoding for text data. The zip file is binary. – Lorenz Meyer Mar 11 '14 at 13:19
0

So I found a solution by first converting the string into a Uint8 and from there converting it to a file buffer which I then could save.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.