ZIP Compression: A lossless compression method introduced in 1989. It combines two key aspects:
- Format: Allows multiple files to be compressed and stored in a single file.
- Algorithm: Compresses the data within the ZIP to reduce file size while preserving data integrity.
ZIP is widely used for efficient storage and file sharing without losing any data. (Docment)
I haven’t used ZIP compression much in development, but I have used it in scripts as an operator to reduce storage.
Since there’s lossless compression, there’s also lossy compression. Lossy compression works by discarding data that is less noticeable to the human senses, making it common in multimedia like photos, music, and videos.
For instance, platforms like YouTube adjust video quality based on user preferences and network conditions, which is a great example of lossy compression in action.
Currently, I’m working with images using OpenCV, so I’ll need to explore lossless compression to maintain image quality during processing.
Lossless compression methods vary, but the basic principle is to reduce redundancy by identifying and condensing repeating patterns within a file.
For example, data like "abcabcabc"
could be compressed to something like "3abc"
. This is a simplified explanation, but it gives you a clear idea of how the process works!
Zip(Lossless compression) Code
import os
import zipfile
def file_zip(source, target):
f_zip = zipfile.ZipFile(source, 'w')
f_zip.write(target, compress_type=zipfile.ZIP_DEFLATED)
f_zip.close()
def files_zip(source, filetype_target):
f_zip = zipfile.ZipFile(source, 'w')
for folder, subfolders, files in os.walk(os.path.join(os.path.dirname(
os.path.dirname(os.path.abspath(__file__))), 'tmp')):
for file in files:
if file.endswith(filetype_target):
f_zip.write(os.path.join(folder, file),
os.path.relpath(os.path.join(folder, file),
os.path.join(os.path.dirname(
os.path.dirname(os.path.abspath(__file__))), 'tmp')),
compress_type=zipfile.ZIP_DEFLATED)
f_zip.close()
def file_unzip(source, target):
f_zip = zipfile.ZipFile(source)
f_zip.extract(target, os.path.dirname(os.path.abspath(__file__)))
f_zip.close()
def file_all_unzip(source):
f_zip = zipfile.ZipFile(source)
f_zip.extractall(os.path.dirname(os.path.abspath(__file__)))
f_zip.close()
if __name__ == "__main__":
file_zip('compress.zip', 'source.file') #files_zip('compress.zip', './')
file_unzip('compress.zip', 'source.file')
file_all_unzip('compress.zip')
os.remove('compress.zip')
The code might look a bit complex because it includes a function, files_zip_all
, that handles compressing the contents of folders as well. This adds some complexity. Honestly, using a shell command is often simpler for tasks like this.
One thing I’ve learned in over 20 years as a programmer is that “programmers are language-agnostic.” If I need to handle ZIP compression in a Windows-based program, I’ll use this code. On Linux, though, I’d likely skip it and just use a straightforward shell command for simplicity.
Output
compreass.zip
Many modern lossless compression programs use various algorithms to balance the trade-off between compression speed and compression ratio. However, in most cases, the differences in results are minimal and often negligible.