Last active
January 19, 2024 11:34
-
-
Save makamys/f279b286c8afe1b976ea18886df1cf7d to your computer and use it in GitHub Desktop.
Recursive zip search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
from zipfile import ZipFile, BadZipFile | |
import sys | |
import os | |
if len(sys.argv) != 3 and len(sys.argv) != 4: | |
sys.exit('''Usage: {} ROOT_DIR QUERY [--names] | |
Searches the zip files in ROOT_DIR recursively for QUERY. | |
Arguments: | |
--names: only search in file names, not in content. | |
'''.format(sys.argv[0])) | |
rootdir = sys.argv[1] | |
query = sys.argv[2] | |
names = len(sys.argv) == 4 and sys.argv[3] == "--names" | |
deep_scan = not names | |
def scan_zip(path): | |
zip = ZipFile(path) | |
names = [] | |
for name in zip.namelist(): | |
#if name.endswith(".class") and query.encode("utf8") in zip.read(name): | |
if (deep_scan and query.encode("utf8") in zip.read(name)) or (not deep_scan and query in name): | |
names.append(name) | |
return names | |
for path in glob.glob(rootdir + "/**/*", recursive=True): | |
if not os.path.isdir(path): | |
try: | |
found = scan_zip(path) | |
if found: | |
print("\n" + path + " contains query! in these files:\n" + "\n".join([" * " + x for x in found])) | |
except BadZipFile as e: | |
pass | |
except Exception as e: | |
print("\nException scanning", path, ":", e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment