A python script for Mac that zips without garbled characters on Windows

background

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.

Unzip Windows-> Mac

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).

How to use

$ python win_zip.py <The path of the folder you want to compress>

Caution

__ 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

A python script for Mac that zips without garbled characters on Windows
A python script that deletes ._DS_Store and ._ * files created on Mac
Build a Python extension for E-Cell 4 on Windows 7 (64bit)
Procedure for building a CDK environment on Windows (Python)
A python script that wants to use Mac startup / end time for attendance management
Create a Python script for Wake on LAN (NAT traversal Wake on LAN [5])
Create a virtual environment for python on mac [Very easy]
Building a Python environment on Mac
Create a Python environment on Mac (2017/4)
Create a Python environment for professionals in VS Code on Windows
Workaround for the problem that sys.argv is not passed when executing a Python script with only the file name in Python2.7 on Windows
Notes for using OpenCV on Windows10 Python 3.8.3.
[Mac] Building a virtual environment for Python
Creating a python virtual environment on Windows
Use without installing python 2.x on Windows
Windows Qt5.4 Python3.4 QProcess Japanese garbled characters
Things to watch out for when creating a Python environment on a Mac
Introducing a library that was not included in pip on Python / Windows
Put MeCab binding for Python with pip on Windows, mac and Linux
Rock-paper-scissors with Python Let's run on a Windows local server for beginners
A class for PYTHON that can be operated without being aware of LDAP
(Windows) Causes and workarounds for UnicodeEncodeError on Python 3
Simply build a Python 3 execution environment on Windows
Build a Python environment on Mac (Mountain Lion)
Build a Python development environment on your Mac
Separately install a version of Python that is not pre-installed on your Mac
python on mac
A memorandum for touching python Flask on heroku
Installing TensorFlow on Windows Easy for Python beginners
Create a Python virtual development environment on Windows
A python script that generates a sample dataset for checking the operation of a classification tree
A python script that gets the number of jobs for a specified condition from indeed.com
A real way for people using python 3.8.0-2 from windows to work with multibyte characters
Create an exe file that works in a Windows environment without Python with PyInstaller
Build a GVim-based Python development environment on Windows 10 (3) GVim8.0 & Python3.6
I tried Python on Mac for the first time.
Build a local development environment for Laravel6.X on Mac
Build a machine learning Python environment on Mac OS
[Heroku] Memo for deploying Python apps using Heroku on Windows [Python]
Until drawing a 3D graph in Python on windows10
I made a VM that runs OpenCV for Python
Create a comfortable Python 3 (Anaconda) development environment on windows
A memo of installing Chainer 1.5 for GPU on Windows
Build a GVim-based Python development environment on Windows 10 (1) Installation
Build a Python development environment on Mac OS X
What's in that variable (when running a Python script)
PIL with Python on Windows 8 (for Google App Engine)
Build a Python environment on your Mac using pyenv
problem that certificate verify failed on mac python pip
Building a Python environment for programming beginners (Mac OS)
Create a decent shell and python environment on Windows
For those who can't install Python on Windows XP
Install Python on Mac
Install Python 3 on Mac
python basic on windows ②
Install python on windows
Install Python 3.4 on Mac
Install Python (for Windows)
[Visual Studio Code] [Python] [Windows] Support for garbled Japanese characters in Python in VS Code task / debug output
I replaced the Windows PowerShell cookbook with a python script.
Create a Python (pyenv / virtualenv) development environment on Mac (Homebrew)