Read a Compressed File Fully in Python
This article explains how i tin perform diverse operations on a zippo file using a simple python program.
What is a zip file?
ZIP is an archive file format that supports lossless information compression. By lossless pinch, we hateful that the compression algorithm allows the original data to be perfectly reconstructed from the compressed data. So, a ZIP file is a unmarried file containing one or more compressed files, offering an ideal manner to make large files smaller and keep related files together.
Why do nosotros demand zip files?
- To reduce storage requirements.
- To improve transfer speed over standard connections.
To work on zip files using python, we will utilize an inbuilt python module chosen zipfile.
ane. Extracting a zip file
from
zipfile
import
ZipFile
file_name
=
"my_python_files.zip"
with ZipFile(file_name,
'r'
) as
zip
:
zip
.printdir()
print
(
'Extracting all the files now...'
)
zip
.extractall()
print
(
'Done!'
)
The to a higher place program extracts a zip file named "my_python_files.goose egg" in the same directory as of this python script.
The output of above program may wait similar this:
Let u.s.a. endeavor to understand the above lawmaking in pieces:
-
from zipfile import ZipFile
ZipFile is a course of zipfile module for reading and writing zip files. Here we import simply class ZipFile from zipfile module.
-
with ZipFile(file_name, 'r') as cypher:
Here, a ZipFile object is made by calling ZipFile constructor which accepts naught file name and manner parameters. We create a ZipFile object in READ style and name information technology as goose egg.
-
nada.printdir()
printdir() method prints a tabular array of contents for the annal.
-
cipher.extractall()
extractall() method will extract all the contents of the zip file to the current working directory. You can as well call extract() method to excerpt whatsoever file past specifying its path in the zip file.
For example:goose egg.extract('python_files/python_wiki.txt')
This volition extract simply the specified file.
If you want to read some specific file, you can go like this:
data = cipher.read(name_of_file_to_read)
2. Writing to a zilch file
Consider a directory (folder) with such a format:
Here, we will need to crawl the whole directory and its sub-directories in club to get a list of all file paths before writing them to a zip file.
The following program does this past crawling the directory to be zipped:
from
zipfile
import
ZipFile
import
os
def
get_all_file_paths(directory):
file_paths
=
[]
for
root, directories, files
in
bone.walk(directory):
for
filename
in
files:
filepath
=
os.path.join(root, filename)
file_paths.append(filepath)
return
file_paths
def
principal():
directory
=
'./python_files'
file_paths
=
get_all_file_paths(directory)
print
(
'Post-obit files volition exist zipped:'
)
for
file_name
in
file_paths:
print
(file_name)
with ZipFile(
'my_python_files.null'
,
'w'
) as
zip
:
for
file
in
file_paths:
zip
.write(
file
)
impress
(
'All files zipped successfully!'
)
if
__name__
=
=
"__main__"
:
master()
The output of to a higher place program looks similar this:
Let us try to empathise above code by dividing into fragments:
-
def get_all_file_paths(directory): file_paths = [] for root, directories, files in bone.walk(directory): for filename in files: filepath = os.path.bring together(root, filename) file_paths.suspend(filepath) return file_paths
Kickoff of all, to go all file paths in our directory, we have created this function which uses the bone.walk() method. In each iteration, all files present in that directory are appended to a list called file_paths.
In the end, nosotros return all the file paths. -
file_paths = get_all_file_paths(directory)
Here nosotros pass the directory to exist zipped to the get_all_file_paths() function and obtain a list containing all file paths.
-
with ZipFile('my_python_files.zip','west') every bit zip:
Hither, we create a ZipFile object in WRITE mode this fourth dimension.
-
for file in file_paths: zip.write(file)
Hither, nosotros write all the files to the zilch file one by i using write method.
3. Getting all information most a cypher file
from
zipfile
import
ZipFile
import
datetime
file_name
=
"case.zip"
with ZipFile(file_name,
'r'
) equally
nil
:
for
info
in
nix
.infolist():
impress
(info.filename)
print
(
'\tModified:\t'
+
str
(datetime.datetime(
*
info.date_time)))
print
(
'\tSystem:\t\t'
+
str
(info.create_system)
+
'(0 = Windows, 3 = Unix)'
)
print
(
'\tZIP version:\t'
+
str
(info.create_version))
impress
(
'\tCompressed:\t'
+
str
(info.compress_size)
+
' bytes'
)
print
(
'\tUncompressed:\t'
+
str
(info.file_size)
+
' bytes'
)
The output of above programme may look like this:
for info in cipher.infolist():
Hither, infolist() method creates an instance of ZipInfo class which contains all the information about the zippo file.
We can access all information like terminal modification engagement of files, file names, organisation on which files were created, Zip version, size of files in compressed and uncompressed form, etc.
This article is contributed by Nikhil Kumar. If y'all similar GeeksforGeeks and would like to contribute, yous can also write an article using write.geeksforgeeks.org or mail your commodity to review-squad@geeksforgeeks.org. See your article appearing on the GeeksforGeeks principal page and assist other Geeks.
Please write comments if y'all find anything incorrect, or you lot desire to share more information virtually the topic discussed above.
Source: https://www.geeksforgeeks.org/working-zip-files-python/
0 Response to "Read a Compressed File Fully in Python"
Post a Comment