0

Here is my function that to get the binary data.

def get_remote_file_binary_data(self, filename, connection_string = ""):
    file_data = None
    tmp_file_path = copyfile.make_temp_path()
    try:
        if connection_string:
            print("connection string is: ", connection_string) 
            source_file = copyfile.join(connection_string, filename)
        else:
            source_file = filename
    
        copyfile.copyfile(source_file, tmp_file_path)
    
        tmp_file = open(tmp_file_path, "rb")
        file_data = tmp_file.read()
        tmp_file.close()
    finally:
        os.remove(tmp_file_path)
    return file_data

This is my data being returned

b"PK\x03\x04\x14\x00\x01\x00\x08\x00Fu\x87RR\xa6cpr\x00\x00\x00z\x00\x00\x00\x0c\x00\x00\x00fake_xml.xml)\xe0H\x86]\x83\xd2\xb8\\\xe2\x10\x95f\xde\xa6\x83|&\xa5\x99VwOS\x19\xea\xdb}\xee1[\xe0\x0f'0:C|6u2\xe4\xa6\xbcc\x8e\xdb\xd16(\x1d?\xba\x98Vp\x11\xb5\x06\xe8\xdcn\x0b\xc5I'B\x83#W\xac\xe5*+t\xa6\xa9\xd5\x85\x04\x86+{\xad\xe1\x8b{\xbf\x88L\xc4\xff\xa9=\x18\x9di\xa96Y$3\xfa\x98\x9c\x10\x99\n\x80PG\x9c\xd0GPK\x01\x02?\x00\x14\x00\x01\x00\x08\x00Fu\x87RR\xa6cpr\x00\x00\x00z\x00\x00\x00\x0c\x00$\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00fake_xml.xml\n\x00
\x00\x00\x00\x00\x00\x01\x00\x18\x00\x89R~\x1a\xe6+\xd7\x01
\xe3\xf8\x1a\xe6+\xd7\x01T\xb5N\x1a\xe6+\xd7\x01PK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00^\x00\x00\x00\x9c\x00\x00\x00\x00\x00"

Here is the code calling the function above.

zip_file = self.get_remote_file_binary_data(zip_filename, connection_str)

Here is what I tried and the errors I got in comments

zip_file = zip_file.decode('UTF-8') # 'utf-8' codec can't decode byte 0x87 in position 12: invalid start byte
zip_file = zip_file.decode('UTF-16')  # codec can't decode bytes in position 54-55: illegal encoding
zip_file = zip_file.decode('ascii')  #'ascii' codec can't decode byte 0x87 in position 12: ordinal not in range(128)
zip_file = zip_file.decode('ANSI_X3.4-1968') # ascii' codec can't decode byte 0x87 in position 12: ordinal not in range(128)
5
  • What makes you think this is any sort of encoded text? Commented Apr 15, 2021 at 2:12
  • Heck, you literally called it zip_file. You seem to be aware it's a ZIP file. You need to decompress it with a ZIP decompressor. Commented Apr 15, 2021 at 2:15
  • 1
    If there were any doubt, the fact that the first two letters are "PK" confirms it. It's a ZIP file. Use the zipfile module to read it. Commented Apr 15, 2021 at 2:32
  • What makes me think it was encoded text was because if I ran type() it showed me class bytes, so in this case is it just a regular zip file and not bytes? Commented Apr 15, 2021 at 3:16
  • 2
    It's the raw bytes of a zip file. Not every bytes object represents encoded text. Commented Apr 15, 2021 at 3:25

1 Answer 1

1
import io
from zipfile import ZipFile

bites = b"PK\x03\x04\x14\x00\x01\x00\x08\x00Fu\x87RR\xa6cpr\x00\x00\x00z\x00\x00\x00\x0c\x00\x00\x00fake_xml.xml)\xe0H\x86]\x83\xd2\xb8\\xe2\x10\x95f\xde\xa6\x83|&\xa5\x99VwOS\x19\xea\xdb}\xee1[\xe0\x0f'0:C|6u2\xe4\xa6\xbcc\x8e\xdb\xd16(\x1d?\xba\x98Vp\x11\xb5\x06\xe8\xdcn\x0b\xc5I'B\x83#W\xac\xe5*+t\xa6\xa9\xd5\x85\x04\x86+{\xad\xe1\x8b{\xbf\x88L\xc4\xff\xa9=\x18\x9di\xa96Y$3\xfa\x98\x9c\x10\x99\n\x80PG\x9c\xd0GPK\x01\x02?\x00\x14\x00\x01\x00\x08\x00Fu\x87RR\xa6cpr\x00\x00\x00z\x00\x00\x00\x0c\x00$\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00fake_xml.xml\n\x00 \x00\x00\x00\x00\x00\x01\x00\x18\x00\x89R~\x1a\xe6+\xd7\x01 \xe3\xf8\x1a\xe6+\xd7\x01T\xb5N\x1a\xe6+\xd7\x01PK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00^\x00\x00\x00\x9c\x00\x00\x00\x00\x00"

print(ZipFile(io.BytesIO(bites)).filelist[0])

Output:

<ZipInfo filename='fake_xml.xml' compress_type=deflate external_attr=0x20 file_size=122 compress_size=114>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.