- Python creating zip file
- Создание и закрытие файла
- Запись файлов в архив
- Получение информации о файлах в архиве
- Извлечение файлов из архива
- Считывание файла
- Открытие файла
- How to create a zip file using Python?
- Creating uncompressed ZIP file in Python
- Using shutil.make_archive to create Zip file
- Syntax
- Example
- Output
- Creating compressed ZIP file in Python
- Creating ZIP file from multiple files
- Example
- Output
- Creating ZIP file from entire directory
- Example
- Output
- Creating ZIP file from specific files in a directory
- Example
- Output
Python creating zip file
Zip представляет наиболее популярный формат архивации и сжатия файлов. И язык Python имеет встроенный модуль для работы с ними — zipfile . С помощью этого модуля можно создавать, считывать, записывать zip-файлы, получать их содержимое и добавлять в них файлы. Также поддерживается шифрование, но не поддерживается дешифрование.
Для представления zip-файла в этом модуле определен класс ZipFile . Он имеет следующий конструктор:
ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, compresslevel=None, *, strict_timestamps=True, metadata_encoding=None)
- file : путь к zip-файлу
- mode : режим открытия файла. Может принимать следующие значения:
- r : применяется для чтения существующего файла
- w : применяется для записи нового файла
- a : применяется для добавления в файл
- ZIP_STORED : архивация без сжатия (значение по умолчанию)
- ZIP_DEFLATED : стандартный тип сжатия при архивации в zip
- ZIP_BZIP2 : сжатие с помощью способа BZIP2
- ZIP_LZMA : сжатие с помощью способа LZMA
Для работы с файлами этот класс предоставляет ряд методов:
- close() : закрывает zip-файл
- getinfo() : возвращает информацию об одном файле из архива в виде объекта ZipInfo
- namelist() : возвращает список файлов архива
- infolist() : возвращает информацию обо всех файлах из архива в виде списока объектов ZipInfo
- open() : предоставляет доступ к одному из файлов в архиве
- read() : считывает файл из архива в набор байтов
- extract() : извлекает из архива один файл
- extractall() : извлекает все элементы из архива
- setpassword() : устанавливает пароль для zip-файла
- printdir() : выводит на консоль содержимое архива
Создание и закрытие файла
Для создания архивного файла в конструктор ZipFile передается режим «w» или «a»:
from zipfile import ZipFile myzip = ZipFile("metanit.zip", "w")
После выполнения кода в текущей папке будет создаваться пустой архивный файл «metanit.zip».
После окончания работы с архивом для его закрытия применяется метод close() :
from zipfile import ZipFile myzip = ZipFile("metanit.zip", "w") myzip.close()
Но так как ZipFile также представляет менеджер контекста, то он поддерживает выражение with , которое определяет контекст и автоматически закрывает файл по завершению контекста:
from zipfile import ZipFile with ZipFile("metanit.zip", "w") as myzip: pass
Запись файлов в архив
Для записи файлов в архив применяется файл write() :
write(filename, arcname=None, compress_type=None, compresslevel=None)
Первый параметр представляет файл, который записиывается в архив. Второй параметр — arcname устанавливает произвольное имя для файла внутри архива (по умолчанию это само имя файла). Третий параметр — compress_type представляет тип сжатия, а параметр compresslevel — уровень сжатия.
Например, запишем в архив «metanit.zip» файл «hello.txt» (который, как предполагается, находится в той же папке, где и текущий скрипт python):
from zipfile import ZipFile with ZipFile("metanit.zip", "w") as myzip: myzip.write("hello.txt")
Стоит учитывать, что при открытии файла в режиме «w» при всех последующих записях текущее содержимое будет затираться, то есть фактически архивный файл будет создаваться заново. Если нам необходимо добавить, то необходимо определять zip-файл в режиме «a»:
from zipfile import ZipFile with ZipFile("metanit.zip", "a") as myzip: myzip.write("hello2.txt") myzip.write("forest.jpg")
Стоит отметить, что по умолчанию сжатие не применяется. Но при необходимости можно применить какой-нибудь способ сжатия и уровень сжатия»
from zipfile import ZipFile, ZIP_DEFLATED with ZipFile("metanit.zip", "w", compression=ZIP_DEFLATED, compresslevel=3) as myzip: myzip.write("hello.txt")
Необходимо учитывать, что если мы попробуем добавить в архив файлы с уже имеющимися именами, то консоль выведет предупреждение. Чтобы избежать наличия файлов с дублирующимися именами можно через второй папаметр метода write явным образом определить для них уникальное имя внутри архива:
from zipfile import ZipFile with ZipFile("metanit.zip", "a") as myzip: myzip.write("hello.txt", "hello1.txt") myzip.write("hello.txt", "hello2.txt") myzip.write("hello.txt", "hello3.txt")
Получение информации о файлах в архиве
Метод infolist() возвращает информацию о файлах в архиве с виде списка, где каждый отдельный файл представлен объектом ZipInfo:
from zipfile import ZipFile with ZipFile("metanit.zip", "a") as myzip: print(myzip.infolist())
Класс ZipInfo предоставляет ряд атрибутов для хранения информации о файле. Основные из них:
- filename : название файла
- date_time : дата и время последнего изменения файла в виде кортежа в формате (год, месяц, день, час, минута, секунда)
- compress_type : тип сжатия
- compress_size : размер после сжатия
- file_size : оригинальный размер файла до сжатия
Получим эти данные по каждому отдельному файлу в архиве:
from zipfile import ZipFile with ZipFile("metanit.zip", "r") as myzip: for item in myzip.infolist(): print(f"File Name: Date: Size: ")
Примерный консольный вывод:
File Name: hello.txt Date: (2022, 11, 23, 20, 21, 34) Size: 18 File Name: forest.jpg Date: (2022, 11, 19, 20, 46, 52) Size: 103956 File Name: hello1.txt Date: (2022, 11, 23, 20, 21, 34) Size: 18 File Name: hello2.txt Date: (2022, 11, 23, 20, 21, 34) Size: 18 File Name: hello3.txt Date: (2022, 11, 23, 20, 21, 34) Size: 18
С помощью метода is_dir() можно проверить, является ли элемент в архиве папкой:
from zipfile import ZipFile with ZipFile("metanit.zip", "r") as myzip: for item in myzip.infolist(): if(item.is_dir()): print(f"Папка: ") else: print(f"Файл: ")
Если надо получить только список имен входящих в архив файлов, то применяется метод namelist() :
from zipfile import ZipFile with ZipFile("metanit.zip", "r") as myzip: for item in myzip.namelist(): print(item)
Консольный вывод в моем случае:
hello.txt forest.jpg hello1.txt hello2.txt hello3.txt
С помощью метода getinfo() можно получить данные по одному из архивированных файлов, передав в метод его имя в архиве. Результат метода — объект ZipInfo:
from zipfile import ZipFile with ZipFile("metanit.zip", "r") as myzip: try: hello_file = myzip.getinfo("hello.txt") print(hello_file.file_size) except KeyError: print("Указанный файл отсутствует")
Если в архиве не окажется элемента с указанным именем, то метод сгенерирует ошибку KeyError.
Извлечение файлов из архива
Для извлечения всех файлов из архива применяется метод extractall() :
extractall(path=None, members=None, pwd=None)
Первый параметр метода устанавливает каталог для извлечения архива (по умолчанию извлечение идет в текущий каталог). Параметр members представляет список строк — список названий файлов, которые надо извлечт из архива. И третий параметр — pwd представляет пароль, в случае если архив закрыт паролем.
Например, извлечем все файлы из архива:
from zipfile import ZipFile with ZipFile("metanit.zip", "r") as myzip: myzip.extractall()
Извлечение в определенную папку:
myzip.extractall(path="metanit")
# извлекаем файлы "hello.txt", "forest.jpg" в папку "metanit2" myzip.extractall(path="metanit2", members=["hello.txt", "forest.jpg"])
Для извлечения одного файла применяется метод extract() , в который в качестве обязательного параметра передается имя извлекаемого файла:
Считывание файла
Метод read() позволяет считать содержимое файла из архива в набор байтов:
from zipfile import ZipFile with ZipFile("metanit.zip", "r") as myzip: content = myzip.read("hello5.txt") print(content)
Открытие файла
Метод open() позволяет открывать отдельные файлы из архива без непосредственного их извлечения:
open(name, mode='r', pwd=None, *, force_zip64=False)
В качестве первого обязательного параметра передается имя файла внутри архива. Второй параметр — mode устанавливает режим открытия. Параметр pwd задает пароль, если файл защищен паролем. И параметр force_zip64 при значении True позволяет открывать файлы больше 4 Гб.
Этот файл может быть полезен для манипулирования файлом, например, для считывания его содержимого или, наоборот, для записи в него. Например, откроем файл и считаем его содержимое:
from zipfile import ZipFile with ZipFile("metanit.zip", "a") as myzip: # записываем в архив новый файл "hello5.txt" with myzip.open("hello5.txt", "w") as hello_file: encoded_str = bytes("Python. ", "UTF-8") hello_file.write(encoded_str)
How to create a zip file using Python?
ZIP is an archive file format used to for lossless data compression. One or more directories or files are used to create a ZIP file. ZIP supports multiple compression algorithms, DEFLATE being the most common. ZIP files have .zip as extension. In this article we are going to discuss how to create a Zip file using Python.
Creating uncompressed ZIP file in Python
Uncompressed ZIP files do not reduce the size of the original directory. As no compression is sharing uncompressed ZIP files over a network has no advantage as compared to sharing the original file.
Using shutil.make_archive to create Zip file
Python has a standard library shutil which can be used to create uncompressed ZIP files. This method of creating ZIP file should be used only to organize multiple files in a single file.
Syntax
Following is the syntax of shutil.make_archive −
shutil.make_archive(‘output file name’, ‘zip’, ‘directory name’)
Example
Following is an example to create ZIP file using shutil.make_archive −
import shutil import os.path # Creating the ZIP file archived = shutil.make_archive('E:/Zipped file', 'zip', 'E:/Folder to be zipped') if os.path.exists('E:/Zipped file.zip'): print(archived) else: print("ZIP file not created")Output
Following is an output of the above code −
Creating compressed ZIP file in Python
Compressed ZIP files reduce the size of the original directory by applying compression algorithm. Compressed ZIP files result in faster file sharing over a network as the size of the ZIP file is significantly smaller than original file.
The zipfile library in python allows for creation of compressed ZIP files using different methods.
Creating ZIP file from multiple files
In this method, ZipFile() creates a ZIP file in which the files which are to be compressed are added. This is achieved by creating object of ZipFile using with keyword and then writing the files using .write() method.
Example
Following is an example to create ZIP file using multiple files −
import os from zipfile import ZipFile # Create a ZipFile Object with ZipFile('E:/Zipped file.zip', 'w') as zip_object: # Adding files that need to be zipped zip_object.write('E:/Folder to be zipped/Greetings.txt') zip_object.write('E:/Folder to be zipped/Introduction.txt') # Check to see if the zip file is created if os.path.exists('E:/Zipped file.zip'): print("ZIP file created") else: print("ZIP file not created")Output
Following is an output of the above code −
Creating ZIP file from entire directory
In this method, a for loop is used to traverse the entire directory and then add all the files present in the directory to a ZIP file which is created using ZipFile.
Example
Following is an example to create ZIP file from entire directory −
import os from zipfile import ZipFile # Create object of ZipFile with ZipFile('E:/Zipped file.zip', 'w') as zip_object: # Traverse all files in directory for folder_name, sub_folders, file_names in os.walk('E:/Folder to be zipped'): for filename in file_names: # Create filepath of files in directory file_path = os.path.join(folder_name, filename) # Add files to zip file zip_object.write(file_path, os.path.basename(file_path)) if os.path.exists('E:/Zipped file.zip'): print("ZIP file created") else: print("ZIP file not created")Output
Following is an output of the above code −
Creating ZIP file from specific files in a directory
In this method, lambda function is used to filter files with specific extensions to be added in the ZIP file. The lambda function is passed as parameter to a function in which the files are filtered based on the extension.
Example
Following is an example to create ZIP file using specific files in a directory −
import os from zipfile import ZipFile def zip_csv(directory_name, zip_file_name, filter): # Create object of ZipFile with ZipFile(zip_file_name, 'w') as zip_object: # Traverse all files in directory for folder_name, sub_folders, file_names in os.walk(directory_name): for filename in file_names: # Filter for csv files if filter(filename): # Create filepath of files in directory file_path = os.path.join(folder_name, filename) # Add files to zip file zip_object.write(file_path, os.path.basename(file_path)) if __name__ == '__main__': zip_csv('E:/Folder to be zipped', 'E:/Zipped file.zip', lambda name: 'csv' in name) if os.path.exists('E:/Zipped file.zip'): print("ZIP file created with only CSV files") else: print("ZIP file not created")Output
Following is an output of the above code −
ZIP file created with only CSV files