Created
August 8, 2022 06:44
-
-
Save minhpqn/1766bd09c02a38f1329e6e0223991bdd to your computer and use it in GitHub Desktop.
Zip folder in SJIS Encoding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import zipfile | |
| from zipcp932patch import zipcp932patch | |
| def zipdir(path, output_path): | |
| # ziph is zipfile handle | |
| with zipcp932patch, zipfile.ZipFile(output_path, 'w') as ziph: | |
| for root, dirs, files in os.walk(path): | |
| for file in files: | |
| ziph.write(os.path.join(root, file), | |
| os.path.relpath(os.path.join(root, file), | |
| os.path.join(path, '..'))) | |
| if __name__ == "__main__": | |
| input_folder = "./作業28_生活示談書_10.1_全解析結果" | |
| output_path = "./作業28_生活示談書_10.1_全解析結果.zip" | |
| zipdir(input_folder, output_path) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Py3でcp932エンコードされたzipファイルを作成するパッチ | |
| https://scrapbox.io/shimizukawa/Python3でcp932なzipファイルを作りたい | |
| 利用コード例:: | |
| >>> from zipfile import ZipFile | |
| >>> from zipcp932patch import zipcp932patch | |
| >>> with zipcp932patch, ZipFile('output.zip', 'w') as zf: | |
| ... zf.write('日本語.txt') | |
| ... | |
| """ | |
| import unittest.mock | |
| def _patch_encodeFilenameFlags(self): | |
| try: | |
| return self.filename.encode('ascii'), self.flag_bits | |
| except UnicodeEncodeError: | |
| return self.filename.encode('cp932'), self.flag_bits | |
| zipcp932patch = unittest.mock.patch( | |
| 'zipfile.ZipInfo._encodeFilenameFlags', | |
| _patch_encodeFilenameFlags) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment