Skip to content

Commit

Permalink
woo! It's a command line tool and it will tell you if you've got an o…
Browse files Browse the repository at this point in the history
…bfuscated zip or tar file!
  • Loading branch information
Catherine Oborski committed Sep 7, 2024
1 parent 1546500 commit d094164
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
7 changes: 7 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from typer import Typer
import identify_file_type

app = Typer()
VERSION = "0.1.0-alpha"

app.add_typer(identify_file_type.app, name="identify_file_type")
40 changes: 40 additions & 0 deletions identify_file_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from typer import Typer, Argument
from rich import print, console
from log import create_rich_logger
from pprint import pprint
import argparse
import pathlib
import shutil
import tempfile
import zipfile
import magic

app = Typer()
logger = create_rich_logger()


@app.command()
def identify(files: list[str] = Argument(..., help="File paths to identify")):
"""Identifies the file type of provided files with incorrect extensions."""
for file_path in files:
try:
with open(file_path, "rb") as f:
file_type = magic.from_buffer(f.read(2048))

if file_type.startswith("ZIP archive"):
with zipfile.ZipFile(file_path, "r") as z:
print(f"{file_path}: ZIP archive containing:")
for member in z.namelist():
Text.print(f" - {member}")
elif file_type.startswith("TAR archive"):
with tarfile.open(file_path, "r") as tar:
print(f"{file_path}: TAR archive containing:")
for member in tar.getmembers():
print(f" - {member.name}")
else:
print(f"{file_path}: {file_type}")

except FileNotFoundError:
log.error(f"Error: File not found: {file_path}")

print(f"[bold green]Identified file type: {file_type}[/bold green]")
19 changes: 3 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
from typer import Typer, Argument
from rich import print, console
from pprint import pprint
import argparse
import pathlib
import shutil
import tempfile
import zipfile
from typer import Typer
from log import create_rich_logger
import identify_file_type

app = Typer()
VERSION = "0.1.0-alpha"
logger = create_rich_logger()


@app.command()
def identify(files: list[str] = Argument(..., help="File paths to identify")):
"""Identifies the file type of provided files with incorrect extensions."""
# ... (file identification logic)

print("[bold green]Identified file type:[/bold green]")

app.add_typer(identify_file_type.app, name="identify_file_type")

if __name__ == "__main__":
app()

0 comments on commit d094164

Please sign in to comment.