Skip to content

Commit 9f5e52f

Browse files
authored
Merge pull request #11 from nekitmm/fix/colab-extraction-fix
Google colab extraction problem
2 parents 9791334 + fdaf451 commit 9f5e52f

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

dlpacker/utils.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
from collections import defaultdict
4646

47-
import py7zr
47+
from py7zr import Bad7zFile, SevenZipFile
4848
import gdown
4949
import os
5050
import glob
@@ -70,15 +70,27 @@ def unzip_weights(archive_path, output_dir):
7070
for fname in volume_files:
7171
with open(fname, 'rb') as infile:
7272
outfile.write(infile.read())
73-
7473
print("Created single archive file: ", archive_path)
7574

76-
with py7zr.SevenZipFile(archive_path, mode='r') as z:
77-
z.extractall(path=output_dir)
75+
try:
76+
# this should extract the archive into .h5 file
77+
with SevenZipFile(archive_path, mode='r') as z:
78+
z.extractall(path=output_dir)
79+
except Bad7zFile as e:
80+
print(f"Extraction failed: {e}")
81+
print("Falling back to using 7z command line tool")
82+
os.system(f"7z e {archive_path}.001 -o{output_dir}")
83+
except Exception as e:
84+
print(f"Extraction failed: {e}")
85+
print("Could not extract the weights archive using 7z command line tool. Please make sure it is installed and available in the PATH.")
86+
print("You should probably be able to install 7z by running `sudo apt install p7zip-full`")
7887

7988
# remove 7z archive
8089
os.remove(archive_path)
8190

91+
print("Extracted weights to: ", output_dir)
92+
print("Successfully extracted weights! (this is only done once)")
93+
8294
def fetch_and_unzip_google_drive_link(gdrive_link, output_dir):
8395
"""
8496
Fetches a shared file from a Google Drive link and extracts it from 7-zip format.
@@ -103,7 +115,7 @@ def fetch_and_unzip_google_drive_link(gdrive_link, output_dir):
103115
# Extract the downloaded file
104116
extracted_files = []
105117
try:
106-
with py7zr.SevenZipFile(output_file, mode='r') as z:
118+
with SevenZipFile(output_file, mode='r') as z:
107119
z.extractall(path=output_dir)
108120
extracted_files = os.listdir(output_dir)
109121
print(f'Extracted files: {extracted_files}')

0 commit comments

Comments
 (0)