I'm trying to unzip a zipfile (compressed with BZ2) into a directory. The zipfile contains multiple files.
All (and I've seen quite a few already...) of the examples show how to decompress the zipfile into one file.
This is what I have so far:
def unzipBzip2(passed_targetDir, passed_zipfile):
full_zipfile = pathlib.Path(constants.APP.ROOT, constants.DOWNLOAD_FOLDER, passed_zipfile)
full_target = pathlib.Path(constants.APP.ROOT, constants.DOWNLOAD_FOLDER, passed_targetDir)
with open(file=full_zipfile, mode="rb") as zipfile, open(full_target, 'wb') as target:
decompressor = bz2.BZ2Decompressor()
for data in iter(lambda : zipfile.read(100*1024), b''):
target.write(decompressor.decompress(data))
return
Error is:
Traceback (most recent call last):
... (stack) ...
File "/Users/bert/Project/unzipBzip2.py", line 26, in unzipBzip2
with open(file=fullzipfile, mode="rb") as zipfile, open(full_target, 'wb') as target:
IsADirectoryError: [Errno 21] Is a directory: '/Users/bert/Project/data/51fba56e-c598-491a-a5e4-57373a59367a'
Well, "/Users/bert/Project/data/51fba56e-c598-491a-a5e4-57373a59367a" is indeed a directory. And that's what it should be, since the unzipped files (from the BZ2 zipfile) should be written in that directory.
Why does decompressor complain that this is a directory?
If I change the target to a file
full_target = pathlib.Path(constants.APP.ROOT, constants.DOWNLOAD_FOLDER, passed_targetDir, 'x.x')
it gives the following error:
File "/Users/bert/Project/unzipBzip2.py", line 30, in unzipBzip2
target.write(decompressor.decompress(data))
OSError: Invalid data stream
If your zipfile is a bz2 compressed zip, the code below should work.
You could try to use
filecommand to identify archive format. for example your file isabc.unkown.bz2now we can decompress it using bzip2, and got
abc.unkownthen continue with de decompressed
abc.unkownthe example file is
zipformat inside bz2