![]() ![]() In addition to creating a reading archives/files, ZIP allows us to also append files to existing archives. We see that it has the expected content, so we can go ahead and extract it to file specified by path ( /tmp/). In this example we just print the list of ZipInfo objects, but you could also inspect its attributes to get CRC, size, compression type, etc.Īfter checking all the files we open and read one of them. Before reading any files we check CRC and file headers, afterwards we retrieve information about all files present in the archive. Next, to demonstrate that it worked, we open the archive. After adding all the files, we also set archive password using setpassword method. You will notice that we didn't actually need to open the files that we're adding - all we needed to do is call write passing in the file name. In this snippet we start by creating ZIP archive using ZipFile context manager in "write" ( w) mode and then add the files to this archive. This is a fairly long piece of code, but covers all the important features of zipfile module. This is fairly low level library and therefore might not be so commonly used so let's just look at the basic compression/decompression of whole file at once: So, let’s see how we can perform these basic operations with each of them:įirst up, zlib. Some of them more basic, some of them with a lot of extra features, but what they all have in common is that they (obviously) include functions for compression. We’ve got a plenty of libraries to choose from. It also has support for other features we know from tar utility - list of those is available at the top of above linked docs page. It can read and write gzip, bz2 and lzma files or archives. ![]() tarfile - as with zipfile above, you can probably guess that this module is used for working with tar archives.This module provides all the expected methods for creating, reading, writing or appending to ZIP files as well as classes and objects for easier manipulation of such files. zipfile - as the name suggests - allows us to work with zip archives in Python.shutils is a module we generally don't associate with compression and decompression, but it provides utility methods for working with archives and can be a convenient way for producing tar, gztar, zip, bztar or xztar archives.This module uses the already mentioned zlib compression algorithm and serves as an interface similar to the gzip and gunzip utilities. gzip is a utility most of us are familiar with.It can produce higher compression ratio than some older methods and is the algorithm behind the xz utility (more specifically LZMA2). lzma is both name of the algorithm and Python module.It also works only on individual files and therefore can't create archives. This algorithm is generally more effective than the deflate method, but might be slower. bz2 is a module that provides support for bzip2 compression.More about this library can be found on Wikipedia. So, by using this Python module, you're essentially using gzip compatible compression algorithm without the convenient wrapper. zlib is a library and Python module that provides code for working with Deflate compression and decompression format which is used by zip, gzip and many others.So, let’s first take a look at each of them and see why you might want to use them: All The FormatsĪs mentioned above, Python has library for (almost) every tool/format imaginable. So, to help you navigate through all the available options, we will in this article explore all of these modules and learn how to compress, decompress, verify, test and secure our archives of all kinds of formats with help of Python's standard library. With all these options, deciding what might be the right tool for the task at hand might not be so obvious, though. Whether it’s basics like tar and zip, specific tools or formats such as gzip and bz2 or even more exotic formats like lzma, Python has it all. Python standard library provides great modules and tools for pretty much any task you can think of and modules for working with compressed files are no exception. ![]()
0 Comments
Leave a Reply. |