jborean93 / smbprotocol

Python SMBv2 and v3 Client
MIT License
318 stars 73 forks source link

Copy zip files #164

Closed smsumana closed 2 years ago

smsumana commented 2 years ago

Hi,

Currently trying to copy a zip file from local to NAS using smbprotocol over smbv3. Seems like zip file is not supported. Any idea how I can achieve this.

Thanks in advance.

jborean93 commented 2 years ago

There's nothing special about a zip, this protocol can copy raw bytes to and from a file share. What's the issue you are facing and what's the code you've tried to run?

smsumana commented 2 years ago

It gives me an error as “utf-8 codec can’t decode byte 0xaa in position 10: invalid start byte”

i guess something to do with encoding?

smsumana commented 2 years ago

I’m opening the zip file and reading the contents and using the open_file to write to the file on NAS.

jborean93 commented 2 years ago

Yea so you are reading your zip in text mode rather than byte mode. Typically you want mode='rb' for reading and mode='wb' for writing so instead of trying to encode the bytes to text you are just reading the bytes directly.

Luckily there are helper functions inside smbclient.shutil to do all this work for you:

import smbclient.shutil

smbclient.shutil.copyfile(r'source', r'destination')

You can also use copy and copy2 to also copy some of the files attributes and timestamps respectively. Keep in mind the extra metadata these methods copy is quite limited with SMB so copyfile is usually enough.