When passing a zip file on Mac-> Windows (Japanese environment), the character code of the file name remains utf-8 with Mac default zip compression, and garbled characters occur when decompressing with Windows. It is possible to install software such as MacWinZip to compress it so that it does not happen, but the installation is somewhat troublesome. So I created a Zip compression script for Windows that runs on Python 2.7.
With OSX Yosemite that I use, zip files created in Windows can be decompressed without problems, but in environments such as linux, it seems that the sjis file name may be garbled, so in that case, change the file name Let's use a script that changes to utf-8 and decompresses it (Example of decompression script).
$ python win_zip.py <The path of the folder you want to compress>
__ Code (win_zip.py) __
# coding:utf-8
import os, sys, zipfile, shutil
from unicodedata import normalize
def mac_path_to_sjis(mac_path):
#Since the voiced sound mark is divided in the file name of mac, the problem is solved.
#If you do not do this, you will get an exception where the dakuten cannot be changed to sjis.
norm_path = normalize('NFC', unicode(mac_path,'utf8'))
return norm_path.encode('sjis')
def check_yes_no(message):
# raw_input returns the empty string for "enter"
yes = set(['yes','y'])
no = set(['no','n'])
choice = raw_input(message + os.linesep).lower()
if choice in yes:
return True
elif choice in no:
return False
else:
print "Please respond with 'yes' or 'no'"
return check_yes_no(message) # loop
def zip_dir(in_dir_path):
in_parent_path, in_dirname = os.path.split(in_dir_path)
os.chdir(in_parent_path)
with zipfile.ZipFile(in_dirname + '.zip', 'w') as zip_file:
for parent_path, dirs, files in os.walk(in_dirname):
sjis_dir_path = mac_path_to_sjis(parent_path)
os.makedirs(sjis_dir_path)
for fname in files:
if fname[0] == '.': continue # ignore all secret file
file_path = os.sep.join((parent_path, fname)) # path of original file
print 'zipping : {0}'.format(file_path)
sjis_file_path = mac_path_to_sjis(file_path)
if file_path == sjis_file_path:
zip_file.write(file_path) # file name is all alphabet
else:
shutil.copyfile(file_path, sjis_file_path) # copy file in sjis file name
zip_file.write(sjis_file_path)
os.remove(sjis_file_path) # remove sjis format file
# remove parent folder in sjis format
os.removedirs(sjis_dir_path)
if __name__ == '__main__':
if len(sys.argv) != 2:
sys.exit('Only 1 argument(path of directory) has to be specified as argument!')
zip_target_path = sys.argv[1]
if os.path.exists(zip_target_path) == False:
sys.exit('Directory {0} does not exists!'.format(zip_target_path))
if zip_target_path.endswith(os.sep):
zip_target_path = zip_target_path[0:-1] # remove seperator
zip_path = zip_target_path + '.zip'
if os.path.exists(zip_path):
if check_yes_no('Zip file already exists. Do you want to replace? [y/n]') == False:
print 'Bye'
sys.exit()
zip_dir(zip_target_path)
If you have any problems or improvements, please do not hesitate to let us know.
Recommended Posts