0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

pyminizipでcompress_multipleを使わずに複数のファイルをまとめる方法

Posted at

概要

pyminizipを使って圧縮ファイルにパスワードを付ける時、
複数のファイルをまとめるならcompress_multipleを使うけれども、
イテレートしながらファイルをまとめたい時のやり方。

方法

  • pyminizip.compressでパスワード付きのzipファイルを作成してから、zipfile.ZipFileの追記モードでファイルを追加していくとできる。
import zipfile
from pathlib import Path

import pyminizip


class ZipFileProcessor:

    @staticmethod
    def init_zipfile_with_password(zip_file_name: str, file_name: str, password: str) -> None:
        pyminizip.compress(file_name, '', zip_file_name, password, zipfile.ZIP_DEFLATED)

    def process_zipfile(self, zip_file_name: str, file_name: str, password: str) -> None:
        if not Path(zip_file_name).exists():
            self.init_zipfile_with_password(zip_file_name, file_name, password)
            return

        with zipfile.ZipFile(zip_file_name, 'a') as zip_file:
            zip_file.write(
                file_name,
                compress_type=zipfile.ZIP_DEFLATED
            )
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?