-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathziphero.py
1387 lines (1218 loc) · 52.3 KB
/
ziphero.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ZIPHero: A multipurpose tool for extracting and repairing ZIP, 7z, RAR, and TAR archives.
Includes:
- Automatic backup before repair
- Multiple repair strategies for ZIP
- 7z, RAR, and TAR extraction
- Basic "decompress-recompress" repair approach for 7z and RAR
- GUI with Tkinter for easy usage
- Command-line support for advanced usage
"""
import zipfile
import os
import shutil
import logging
import hashlib
import sys
from typing import Optional, List, Dict
from concurrent.futures import ThreadPoolExecutor, as_completed
from contextlib import contextmanager
import tempfile
from pathlib import Path
from datetime import datetime
import json
import zlib
import struct
import tkinter as tk
from tkinter import filedialog, ttk
import webbrowser
# Attempt to import rarfile gracefully
try:
import rarfile
except ImportError:
rarfile = None
print("WARNING: 'rarfile' not installed. RAR extraction/repair will be unavailable.")
# Attempt to import py7zr gracefully
try:
import py7zr
except ImportError:
py7zr = None
print("WARNING: 'py7zr' not installed. 7z extraction/repair will be unavailable.")
# Attempt to import patoolib gracefully
try:
import patoolib
except ImportError:
patoolib = None
print("WARNING: 'patoolib' not installed. TAR extraction (and other patool features) may be unavailable.")
# Attempt to import pyzipper for enhanced ZIP handling
try:
import pyzipper
except ImportError:
pyzipper = None
print("WARNING: 'pyzipper' not installed. Password-protected ZIP handling will be unavailable.")
# Platform-specific imports (used for Windows-only UI enhancements)
if sys.platform.startswith('win'):
import ctypes
from ctypes import wintypes
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('zip_processor.log'),
logging.StreamHandler(sys.stdout)
]
)
# Exceptions
class ZipProcessingError(Exception):
error_message = "An error occurred during ZIP processing"
def __init__(self, message: str = None):
self.message = message or self.error_message
def __str__(self):
return self.message
class ZipRepairError(Exception):
error_message = "An error occurred during ZIP repair"
def __init__(self, message: str = None):
self.message = message or self.error_message
def __str__(self):
return self.message
class ZipUnlockError(Exception):
error_message = "An error occurred during ZIP unlocking"
def __init__(self, message: str = None):
self.message = message or self.error_message
def __str__(self):
return self.message
@contextmanager
def error_handler(operation: str, skip_on_error: bool = True):
"""Context manager for handling operations with optional skip on error"""
try:
yield
except Exception:
logging.exception(f"Error during {operation}")
if not skip_on_error:
raise
def verify_file_integrity(file_path: str) -> bool:
"""Verify file integrity using a basic MD5 checksum read."""
try:
with open(file_path, 'rb') as f:
file_hash = hashlib.md5()
while True:
chunk = f.read(8192)
if not chunk:
break
file_hash.update(chunk)
# Optionally, we could do something with file_hash.hexdigest() here.
return True
except Exception:
logging.exception(f"File integrity check failed for {file_path}")
return False
class ArchiveFormat:
"""Archive format detection and validation"""
SIGNATURES = {
'zip': b'PK\x03\x04',
'7z': b'7z\xBC\xAF\x27\x1C',
'rar': b'Rar!\x1A\x07',
'tar': b'ustar',
}
@staticmethod
def detect_format(file_path: str) -> Optional[str]:
"""
Attempt to detect format by reading enough bytes for each signature.
We also read at least 300 bytes to handle checking for 'ustar' at offset 257.
"""
try:
with open(file_path, 'rb') as f:
data = f.read(300) # Enough to detect various signatures
# ZIP
if data.startswith(ArchiveFormat.SIGNATURES['zip']):
return 'zip'
# 7z
if data.startswith(ArchiveFormat.SIGNATURES['7z']):
return '7z'
# RAR
if data.startswith(ArchiveFormat.SIGNATURES['rar']):
return 'rar'
# Additional check for TAR at offset 257
if len(data) >= 262 and data[257:262] == b'ustar':
return 'tar'
except Exception:
logging.exception(f"Error in detect_format for {file_path}")
return None
return None
class ZipAutoRepair:
"""
A class that auto-repairs ZIP, 7z, and RAR archives by attempting
different repair strategies (e.g., reconstructing headers, decompress-recompress, etc.).
"""
def __init__(self, backup_dir: str = None):
self.backup_dir = backup_dir or tempfile.gettempdir()
self.repair_log = {}
# Now we define dictionary references to newly implemented methods:
self.repair_strategies = {
'zip': self._repair_zip,
'7z': self._repair_7z,
'rar': self._repair_rar
}
def create_backup(self, file_path: str) -> str:
"""Create a backup of the original file before repair attempts."""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
backup_path = os.path.join(
self.backup_dir,
f"{Path(file_path).stem}_backup_{timestamp}{Path(file_path).suffix}"
)
try:
shutil.copy2(file_path, backup_path)
logging.info(f"Created backup at: {backup_path}")
return backup_path
except Exception:
logging.exception("Failed to create backup")
raise ZipRepairError("Backup creation failed")
# -------------------------------------------------
# ZIP: Single entry point to a "comprehensive" repair
# -------------------------------------------------
def repair_zip(self, file_path: str) -> bool:
"""
Attempt to repair a corrupted zip file with multiple strategies.
Each strategy is tried in turn, stopping if any succeed.
"""
backup_path = self.create_backup(file_path)
self.repair_log[file_path] = {"attempts": [], "successful": False}
repair_methods = [
self._repair_method_header_fix,
self._repair_method_repack,
self._repair_method_stream_repair,
self._repair_method_deep_scan,
self._repair_method_decompress_recompress,
self._repair_method_central_directory
]
for method in repair_methods:
try:
if method(file_path):
self.repair_log[file_path]["successful"] = True
self.repair_log[file_path]["attempts"].append({
"method": method.__name__,
"status": "Success",
"timestamp": datetime.now().isoformat()
})
self._save_repair_log()
return True
except Exception as e:
logging.exception(f"Repair method {method.__name__} raised an exception.")
self.repair_log[file_path]["attempts"].append({
"method": method.__name__,
"error": str(e),
"timestamp": datetime.now().isoformat()
})
# Restore from backup for next attempt
try:
shutil.copy2(backup_path, file_path)
except Exception:
logging.exception("Failed to restore backup during ZIP repair loop.")
self._save_repair_log()
return False
# -------------------------------------------------
# Methods for dictionary-based calls: _repair_zip, ...
# -------------------------------------------------
def _repair_zip(self, file_path: str) -> bool:
"""
Called from self.repair_strategies['zip'].
Uses the advanced 'repair_zip' logic defined above.
"""
return self.repair_zip(file_path)
def _repair_7z(self, file_path: str) -> bool:
"""
Attempt to repair a 7z file by:
1) Creating a backup
2) Extracting all files into a temp dir
3) Re-compressing them into a new 7z
4) Replacing the original if successful
"""
if not py7zr:
logging.error("Cannot repair 7z: py7zr not installed.")
return False
backup_path = self.create_backup(file_path)
try:
temp_dir = tempfile.mkdtemp()
# Step 1: Attempt to extract
with py7zr.SevenZipFile(file_path, 'r') as archive:
archive.extractall(temp_dir)
# Step 2: Re-compress into a new .7z
new_path = file_path + '.new.7z'
with py7zr.SevenZipFile(new_path, 'w') as new_archive:
for root, dirs, files in os.walk(temp_dir):
for each_file in files:
full_path = os.path.join(root, each_file)
arcname = os.path.relpath(full_path, temp_dir)
new_archive.write(full_path, arcname)
# Step 3: Replace original
shutil.move(new_path, file_path)
# Optional: verify integrity. We'll do a simple check:
if self._verify_7z(file_path):
logging.info(f"7z repair successful: {file_path}")
return True
else:
logging.error("7z re-compression produced an invalid archive.")
# revert from backup
shutil.copy2(backup_path, file_path)
return False
except Exception as e:
logging.exception("7z repair failed.")
# revert from backup
try:
shutil.copy2(backup_path, file_path)
except Exception:
logging.exception("Failed to restore backup after 7z repair attempt.")
return False
finally:
shutil.rmtree(temp_dir, ignore_errors=True)
def _repair_rar(self, file_path: str) -> bool:
"""
Attempt to repair a RAR file by:
1) Creating a backup
2) Extracting all files into a temp dir
3) Re-compressing them into a new RAR (if rarfile supports writing)
4) Replacing the original if successful
"""
if not rarfile:
logging.error("Cannot repair RAR: 'rarfile' not installed.")
return False
backup_path = self.create_backup(file_path)
try:
temp_dir = tempfile.mkdtemp()
# Step 1: Attempt to extract
with rarfile.RarFile(file_path) as rf:
rf.extractall(temp_dir)
# Step 2: Re-compress into a new .rar
# 'rarfile' does not provide an official "write" or "create" method,
# so we either need external calls (like `patoolib` or the official RAR)
# or fallback. We'll do a patool-based approach if patoolib is installed.
if patoolib:
new_path = file_path + '.new.rar'
try:
patoolib.create_archive(new_path, (temp_dir,), program="rar")
# Step 3: Replace original
shutil.move(new_path, file_path)
# Optional: verify. We'll do a simple open test:
if self._verify_rar(file_path):
logging.info(f"RAR repair successful: {file_path}")
return True
else:
logging.error("RAR re-compression produced an invalid archive.")
shutil.copy2(backup_path, file_path)
return False
except Exception:
logging.exception("Error re-compressing RAR using patoolib.")
shutil.copy2(backup_path, file_path)
return False
else:
# patoolib not installed. We cannot re-compress automatically.
# We’ll at least confirm we can extract successfully, i.e. the backup is good.
logging.error("Cannot re-compress RAR: patoolib not installed.")
shutil.copy2(backup_path, file_path)
return False
except Exception:
logging.exception("RAR repair failed.")
try:
shutil.copy2(backup_path, file_path)
except Exception:
logging.exception("Failed to restore backup after RAR repair attempt.")
return False
finally:
shutil.rmtree(temp_dir, ignore_errors=True)
# -------------------------------------------------
# Additional "verify" methods for 7z and rar
# -------------------------------------------------
def _verify_7z(self, file_path: str) -> bool:
"""A quick check to see if we can open and list a 7z file."""
if not py7zr:
return False
try:
with py7zr.SevenZipFile(file_path, 'r') as test_sz:
test_sz.getnames() # attempt to list
return True
except Exception:
logging.exception("7z verification failed.")
return False
def _verify_rar(self, file_path: str) -> bool:
"""A quick check to see if we can open and list a RAR file."""
if not rarfile:
return False
try:
with rarfile.RarFile(file_path) as test_rar:
test_rar.namelist() # attempt to list
return True
except Exception:
logging.exception("RAR verification failed.")
return False
# -------------------------------------------------
# Methods for repairing password-protected zips
# -------------------------------------------------
def unlock_zip(self, file_path: str, password_list: List[str]) -> Optional[str]:
"""
Attempt to unlock a password-protected ZIP file using a list of passwords.
Returns the password if successful, else None.
"""
if not pyzipper:
logging.error("pyzipper not installed; cannot unlock password-protected ZIP files.")
raise ZipUnlockError("pyzipper library is required for unlocking ZIP files.")
try:
with pyzipper.AESZipFile(file_path) as z:
for password in password_list:
try:
z.pwd = password.encode('utf-8')
# Attempt to read the first file to check password
z.testzip()
logging.info("Successfully unlocked ZIP with a provided password.")
return password
except RuntimeError:
logging.warning("Incorrect password provided.")
continue
logging.error("Failed to unlock ZIP file with provided password list.")
return None
except Exception as e:
logging.exception("An error occurred while attempting to unlock the ZIP file.")
raise ZipUnlockError(str(e))
def repair_and_unlock_zip(self, file_path: str, password_list: List[str]) -> bool:
"""
Attempt to repair and unlock a password-protected ZIP file.
"""
# First, attempt to repair the ZIP
if not self.repair_zip(file_path):
logging.error("Repairing the ZIP file failed.")
return False
# Check if the ZIP is password-protected
try:
with zipfile.ZipFile(file_path, 'r') as z:
try:
z.namelist() # if it fails due to a password, we handle
except RuntimeError:
logging.info("ZIP file is password-protected. Attempting to unlock.")
password = self.unlock_zip(file_path, password_list)
if password:
# Re-save the ZIP with the found password
self.save_unlocked_zip(file_path, password)
return True
else:
logging.error("Failed to unlock the ZIP file.")
return False
except Exception:
logging.exception("Error while checking ZIP password protection.")
return False
# If no password needed or no error, we’re good
return True
def save_unlocked_zip(self, file_path: str, password: str) -> bool:
"""
Save an unlocked version of the ZIP file using the discovered password.
"""
try:
with pyzipper.AESZipFile(file_path, 'r') as z:
z.pwd = password.encode('utf-8')
unlocked_path = file_path + '.unlocked.zip'
with pyzipper.AESZipFile(unlocked_path, 'w') as unlocked_zip:
for item in z.infolist():
data = z.read(item.filename)
unlocked_zip.writestr(item, data)
# Replace the original file with the unlocked version
shutil.move(unlocked_path, file_path)
logging.info(f"Unlocked ZIP saved: {file_path}")
return True
except Exception:
logging.exception("Failed to save the unlocked ZIP file.")
return False
# -------------------------------------------------
# The main internal ZIP repair sub-methods
# -------------------------------------------------
def _repair_method_header_fix(self, file_path: str) -> bool:
"""Attempt to repair ZIP header by trimming data before PK header."""
try:
with open(file_path, 'rb+') as f:
data = f.read()
if not data.startswith(b'PK\x03\x04'):
zip_start = data.find(b'PK\x03\x04')
if zip_start != -1:
f.seek(0)
f.write(data[zip_start:])
f.truncate()
return self._verify_zip(file_path)
return False
except Exception:
logging.exception("Header fix failed")
return False
def _repair_method_repack(self, file_path: str) -> bool:
"""Attempt to repair by repacking valid entries into a fresh ZIP."""
temp_dir = tempfile.mkdtemp()
try:
with zipfile.ZipFile(file_path, 'r') as source_zip:
valid_files = []
for item in source_zip.filelist:
try:
source_zip.extract(item, temp_dir)
valid_files.append(os.path.join(temp_dir, item.filename))
except Exception:
logging.warning(f"Skipping a corrupted file: {item.filename}")
continue
if valid_files:
with zipfile.ZipFile(file_path, 'w') as new_zip:
for vf in valid_files:
arcname = os.path.relpath(vf, temp_dir)
new_zip.write(vf, arcname)
return self._verify_zip(file_path)
return False
except Exception:
logging.exception("Repack method failed")
return False
finally:
shutil.rmtree(temp_dir, ignore_errors=True)
def _repair_method_stream_repair(self, file_path: str) -> bool:
"""Attempt to repair by streaming and validating chunks (very naive)."""
temp_path = file_path + '.tmp'
chunk_size = 1024 * 1024 # 1MB chunks
try:
with open(file_path, 'rb') as source, open(temp_path, 'wb') as target:
while True:
chunk = source.read(chunk_size)
if not chunk:
break
cleaned_chunk = self._clean_chunk(chunk)
if cleaned_chunk:
target.write(cleaned_chunk)
shutil.move(temp_path, file_path)
return self._verify_zip(file_path)
except Exception:
logging.exception("Stream repair failed")
if os.path.exists(temp_path):
os.remove(temp_path)
return False
def _repair_method_deep_scan(self, file_path: str) -> bool:
"""Deep scan and reconstruction of ZIP file by searching 'PK\\x03\\x04'."""
temp_path = file_path + '.reconstructed'
try:
with open(file_path, 'rb') as f:
data = f.read()
pk_positions = []
pos = 0
while True:
pos = data.find(b'PK\x03\x04', pos)
if pos == -1:
break
pk_positions.append(pos)
pos += 4
if not pk_positions:
return False
with open(temp_path, 'wb') as wf:
for start in pk_positions:
try:
next_start = data.find(b'PK\x03\x04', start + 4)
chunk = data[start:next_start] if next_start != -1 else data[start:]
if self._validate_zip_chunk(chunk):
wf.write(chunk)
except Exception:
logging.exception("Error reconstructing chunk in deep scan.")
continue
if os.path.getsize(temp_path) > 0:
shutil.move(temp_path, file_path)
return self._verify_zip(file_path)
return False
except Exception:
logging.exception("Deep scan failed")
return False
finally:
if os.path.exists(temp_path):
os.remove(temp_path)
def _repair_method_decompress_recompress(self, file_path: str) -> bool:
"""Attempt repair by full decompression and recompression."""
temp_dir = tempfile.mkdtemp()
try:
with zipfile.ZipFile(file_path, 'r') as z:
z.extractall(temp_dir)
new_path = file_path + '.new'
with zipfile.ZipFile(new_path, 'w', compression=zipfile.ZIP_DEFLATED, compresslevel=9) as z:
for root, _, files in os.walk(temp_dir):
for f_ in files:
full_path = os.path.join(root, f_)
arcname = os.path.relpath(full_path, temp_dir)
z.write(full_path, arcname)
shutil.move(new_path, file_path)
return self._verify_zip(file_path)
except Exception:
logging.exception("Decompress/recompress method failed")
return False
finally:
shutil.rmtree(temp_dir, ignore_errors=True)
def _repair_method_central_directory(self, file_path: str) -> bool:
"""Attempt to rebuild the central directory by scanning local file headers."""
try:
with open(file_path, 'rb') as f:
data = f.read()
entries = []
pos = 0
while True:
pos = data.find(b'PK\x03\x04', pos)
if pos == -1:
break
try:
header = data[pos:pos+30]
name_length = struct.unpack('<H', header[26:28])[0]
extra_length = struct.unpack('<H', header[28:30])[0]
file_name = data[pos+30:pos+30+name_length]
entries.append({
'offset': pos,
'header': header,
'name': file_name,
'name_length': name_length,
'extra_length': extra_length
})
except Exception:
logging.exception("Error reading local file header in central directory repair.")
pos += 4
if not entries:
return False
central_dir = bytearray()
for entry in entries:
central_dir.extend(b'PK\x01\x02')
central_dir.extend(entry['header'][4:])
central_dir.extend(struct.pack('<L', entry['offset']))
central_dir.extend(entry['name'])
new_path = file_path + '.rebuilt'
with open(new_path, 'wb') as wf:
wf.write(data)
wf.write(central_dir)
wf.write(b'PK\x05\x06') # End of central directory
# We do a simplified approach. Some additional fields might be needed in real usage:
wf.write(struct.pack('<HHLLH',
0, 0,
len(entries), len(entries),
len(central_dir),
len(data)))
shutil.move(new_path, file_path)
return self._verify_zip(file_path)
except Exception:
logging.exception("Central directory repair failed")
return False
# -------------------------------------------------
# Utility chunk cleaning
# -------------------------------------------------
@staticmethod
def _clean_chunk(chunk: bytes) -> bytes:
"""Clean chunk data if needed. In a production version, you might do more advanced checks."""
return chunk
@staticmethod
def _validate_zip_chunk(chunk: bytes) -> bool:
"""Validate if a chunk contains valid ZIP data (very naive)."""
try:
return (
len(chunk) >= 4
and chunk.startswith(b'PK\x03\x04')
and (zlib.crc32(chunk) & 0xFFFFFFFF) != 0
)
except Exception:
logging.exception("Zip chunk validation failed.")
return False
@staticmethod
def _verify_zip(file_path: str) -> bool:
"""Verify if the ZIP file is valid by opening and test-reading it."""
try:
with zipfile.ZipFile(file_path, 'r') as z:
result = z.testzip()
return result is None
except Exception:
logging.exception(f"Verification of repaired ZIP failed for {file_path}")
return False
def _save_repair_log(self):
"""Save repair log to file"""
log_path = os.path.join(self.backup_dir, 'zip_repair_log.json')
try:
with open(log_path, 'w') as f:
json.dump(self.repair_log, f, indent=2)
except Exception:
logging.exception("Failed to save repair log.")
class ZipProcessor:
"""
Orchestrates the detection, repair, and extraction of various archive files (ZIP, 7z, RAR, TAR).
Provides fallback extraction methods for ZIP.
"""
def __init__(self, max_retries: int = 3, timeout: int = 30):
self.max_retries = max_retries
self.timeout = timeout
self.failed_files: List[str] = []
self.processed_files: Dict[str, bool] = {}
self.repair_tool = ZipAutoRepair()
# Map archive formats to their “process” methods
self.supported_formats = {
'zip': self._process_zip,
'7z': self._process_7z,
'rar': self._process_rar,
'tar': self._process_tar
}
def process_archive(self, file_path: str, output_dir: str) -> bool:
"""Process any supported archive format by detection, repair if needed, and extraction."""
archive_format = ArchiveFormat.detect_format(file_path)
if not archive_format:
logging.error(f"Unsupported or invalid archive format: {file_path}")
return False
processor = self.supported_formats.get(archive_format)
if not processor:
logging.error(f"No processor available for format: {archive_format}")
return False
return processor(file_path, output_dir)
# --------------------------
# ZIP Processing
# --------------------------
def _process_zip(self, file_path: str, output_dir: str) -> bool:
return self.process_zip_file(file_path, output_dir)
def process_zip_file(self, zip_path: str, output_dir: str) -> bool:
"""Enhanced zip file processing with repair, unlock, and fallback extraction strategies."""
if not os.path.exists(zip_path):
logging.error(f"Zip file not found: {zip_path}")
return False
# Integrity check
if not verify_file_integrity(zip_path):
logging.warning(f"Zip file integrity check failed, attempting repair: {zip_path}")
if not self.repair_tool.repair_zip(zip_path):
logging.error("Zip file repair failed")
return False
else:
logging.info("Zip file successfully repaired")
# Check if ZIP is password-protected
if self.is_zip_password_protected(zip_path):
logging.info(f"Zip file is password-protected: {zip_path}")
# Define a list of potential passwords or load from a file
password_list = self.load_password_list()
if not password_list:
logging.error("No passwords provided for unlocking.")
return False
password = self.repair_tool.unlock_zip(file_path=zip_path, password_list=password_list)
if password:
logging.info("Successfully unlocked the ZIP file.")
else:
logging.error("Failed to unlock the password-protected ZIP file.")
return False
# Attempt extraction with fallback methods
try:
# First attempt: Standard extraction
self._standard_extraction(zip_path, output_dir)
except Exception:
logging.exception("Standard extraction failed; trying fallback methods.")
# Fallback 1: Single-file extraction
try:
self._single_file_extraction(zip_path, output_dir)
except Exception:
logging.exception("Single-file extraction failed; trying chunk-based extraction.")
# Fallback 2: Chunk-based extraction
try:
self._chunk_based_extraction(zip_path, output_dir)
except Exception:
logging.exception("All extraction methods failed.")
return False
self._report_processing_results()
return len(self.failed_files) == 0
def is_zip_password_protected(self, zip_path: str) -> bool:
"""Check if a ZIP file is password-protected by scanning flag_bits."""
try:
with zipfile.ZipFile(zip_path, 'r') as z:
for zinfo in z.infolist():
# If the 'flag_bits' has bit 0 set => password protected
if zinfo.flag_bits & 0x1:
return True
return False
except Exception:
logging.exception("Failed to determine if ZIP is password-protected.")
return False
def load_password_list(self) -> List[str]:
"""Load a list of passwords from a file named 'passwords.txt' in the same directory."""
password_list = []
try:
# Adjust path if needed. This is a default approach:
pass_file = os.path.join(os.getcwd(), 'passwords.txt')
with open(pass_file, 'r', encoding='utf-8') as f:
for line in f:
password_list.append(line.strip())
except Exception:
logging.exception("Failed to load password list.")
return password_list
def _standard_extraction(self, zip_path: str, output_dir: str) -> None:
with zipfile.ZipFile(zip_path, 'r') as z:
z.extractall(output_dir)
def _extract_single_file(self, z: zipfile.ZipFile, file_info: zipfile.ZipInfo, output_dir: str) -> None:
try:
with error_handler(f"extracting {file_info.filename}", skip_on_error=True):
z.extract(file_info, output_dir)
self.processed_files[file_info.filename] = True
except Exception:
self.failed_files.append(file_info.filename)
logging.exception(f"Failed to extract {file_info.filename}")
def _single_file_extraction(self, zip_path: str, output_dir: str) -> None:
with zipfile.ZipFile(zip_path, 'r') as z:
for file_info in z.infolist():
for retry in range(self.max_retries):
try:
with error_handler(f"extracting {file_info.filename}", skip_on_error=True):
z.extract(file_info, output_dir)
self.processed_files[file_info.filename] = True
break
except Exception:
logging.exception(f"Retry {retry+1}/{self.max_retries} for {file_info.filename}")
if retry == self.max_retries - 1:
self.failed_files.append(file_info.filename)
def _chunk_based_extraction(self, zip_path: str, output_dir: str) -> None:
"""
Read each file in chunk-sized blocks to handle big files or partial corruptions.
"""
chunk_size = 1024 * 1024 # 1MB
with zipfile.ZipFile(zip_path, 'r') as z:
for file_info in z.infolist():
try:
self._extract_in_chunks(z, file_info, output_dir, chunk_size)
except Exception:
self.failed_files.append(file_info.filename)
logging.exception(f"Chunk-based extraction failed for {file_info.filename}")
def _extract_in_chunks(self, z: zipfile.ZipFile, file_info: zipfile.ZipInfo,
output_dir: str, chunk_size: int) -> None:
"""Extract a single file from a ZIP in chunk_size increments."""
target_path = os.path.join(output_dir, file_info.filename)
os.makedirs(os.path.dirname(target_path), exist_ok=True)
with z.open(file_info) as source, open(target_path, 'wb') as target:
while True:
chunk = source.read(chunk_size)
if not chunk:
break
target.write(chunk)
def _report_processing_results(self) -> None:
"""Report processing results across the entire extraction process."""
total_files = len(self.processed_files)
failed_count = len(self.failed_files)
success_rate = ((total_files - failed_count) / total_files * 100) if total_files > 0 else 0
logging.info(f"""
Processing Summary:
Total Files: {total_files}
Successfully Processed: {total_files - failed_count}
Failed: {failed_count}
Success Rate: {success_rate:.2f}%
""")
# --------------------------
# 7z Processing
# --------------------------
def _process_7z(self, file_path: str, output_dir: str) -> bool:
"""Process a 7z archive if py7zr is available, else fail."""
if not py7zr:
logging.error("py7zr not installed; cannot process 7z files.")
return False
# Attempt to see if the file even opens
if not verify_file_integrity(file_path):
# Attempt a basic repair
logging.info(f"Attempting 7z repair: {file_path}")
repaired = self.repair_tool.repair_strategies['7z'](file_path)
if not repaired:
logging.error("7z file repair failed.")
return False
try:
with py7zr.SevenZipFile(file_path, 'r') as sz:
sz.extractall(output_dir)
return True
except Exception:
logging.exception("Error processing 7z file")
return False
# --------------------------
# RAR Processing
# --------------------------
def _process_rar(self, file_path: str, output_dir: str) -> bool:
"""Process a RAR archive if rarfile is available, else fail."""
if not rarfile:
logging.error("rarfile not installed; cannot process RAR files.")
return False
if not verify_file_integrity(file_path):
logging.info(f"Attempting RAR repair: {file_path}")
repaired = self.repair_tool.repair_strategies['rar'](file_path)
if not repaired:
logging.error("RAR file repair failed.")
return False
try:
with rarfile.RarFile(file_path) as rf:
rf.extractall(output_dir)
return True
except Exception:
logging.exception("Error processing RAR file")
return False
# --------------------------
# TAR Processing
# --------------------------
def _process_tar(self, file_path: str, output_dir: str) -> bool:
"""Process a TAR archive if patoolib is available, else fail."""
if not patoolib:
logging.error("patoolib not installed; cannot process TAR (via patoolib).")
return False
# We'll trust that a basic TAR doesn't need "repair" for now
try:
patoolib.extract_archive(file_path, outdir=output_dir)
return True
except Exception:
logging.exception("Error processing TAR file")
return False
# --------------------------
# Additional Feature:
# "Chunk" a single large ZIP into multiple parts
# --------------------------
def chunk_zip_file(self, zip_path: str, chunk_size_gib: int) -> List[str]:
"""Enhanced zip file chunking with verification."""
chunk_paths = []
chunk_size_bytes = chunk_size_gib * (1024 ** 3)
base_name = os.path.basename(zip_path)
base_dir = os.path.dirname(zip_path)
if not verify_file_integrity(zip_path):
raise ZipProcessingError("Source file integrity check failed")
try:
with open(zip_path, 'rb') as f:
chunk_number = 0
while True:
chunk = f.read(chunk_size_bytes)
if not chunk:
break
chunk_file_name = f"{base_name}.part{chunk_number:03d}"
chunk_file_path = os.path.join(base_dir, chunk_file_name)
with error_handler(f"writing chunk {chunk_number}"):
with open(chunk_file_path, 'wb') as chunk_file:
chunk_file.write(chunk)
if verify_file_integrity(chunk_file_path):
chunk_paths.append(chunk_file_path)
logging.info(f"Successfully created and verified chunk: {chunk_file_name}")
else:
raise ZipProcessingError(f"Chunk verification failed: {chunk_file_name}")
chunk_number += 1
except Exception:
logging.exception("Failed to chunk file; cleaning up partial chunks.")
# Cleanup partial chunks