From 036f1897124567d3d9dd8a5986a03abb986cbdef Mon Sep 17 00:00:00 2001 From: Ilia Miheev Date: Mon, 13 Oct 2025 16:47:15 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20=D1=81=20zip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ipars/ZipManagerCode.py | 51 +++++++++++++++++++++++++++++++++++++++++ ipars/__init__.py | 1 + 2 files changed, 52 insertions(+) create mode 100644 ipars/ZipManagerCode.py diff --git a/ipars/ZipManagerCode.py b/ipars/ZipManagerCode.py new file mode 100644 index 0000000..b5ebe9b --- /dev/null +++ b/ipars/ZipManagerCode.py @@ -0,0 +1,51 @@ +import os +import zipfile + +class ZipManager: + """ + Параметр: + - compression (str): + - 'none': Без сжатия + - 'normal': Обычное сжатие + - 'hard': Увеличенное сжатие + - 'maximum': Максимальное сжатие + """ + def __init__(self, compression :str = 'normal'): + self.compression = self.setСompression(compression) + + def setСompression(self, compressionStr): + if compressionStr == 'none': + return zipfile.ZIP_STORED + elif compressionStr == 'normal': + return zipfile.ZIP_DEFLATED + elif compressionStr == 'hard': + return zipfile.ZIP_BZIP2 + elif compressionStr == 'maximum': + return zipfile.ZIP_LZMA + else: + raise ValueError("Некорректное значение compression. Допустимы значения none, normal, hard, maximun") + + def zipFile(self, filePath:str, zipFilePath:str): + """Архивируем один файл + + filePath (str): Путь к файлу, который нужно заархивировать + zipFilePath (str): Путь к выходному ZIP-файлу + """ + + with zipfile.ZipFile(zipFilePath, 'w', compression=self.compression) as zipf: + zipf.write(filePath, os.path.basename(filePath)) + + def zipFolder(self, folderPath:str, zipFilePath:str): + """Архивируем папку + + folderPath (str): Путь к папке, которую нужно заархивировать + zipFilePath (str): Путь к выходному ZIP-файлу + """ + + with zipfile.ZipFile(zipFilePath, 'w', compression=self.compression) as zipf: + baseName = os.path.basename(os.path.normpath(folderPath)) + for root, _, files in os.walk(folderPath): + for file in files: + filePath = os.path.join(root, file) + arcname = os.path.join(baseName, os.path.relpath(filePath, folderPath)) + zipf.write(filePath, arcname) diff --git a/ipars/__init__.py b/ipars/__init__.py index c2e5a37..4998bc9 100644 --- a/ipars/__init__.py +++ b/ipars/__init__.py @@ -3,3 +3,4 @@ from .JsonManagerCode import JsonManager from .CsvManagerCode import CsvManager from .ProgressBarCode import ProgressBarManager from .TimerManagerCode import TimerManager +from .ZipManagerCode import ZipManager