import zipfile
import sys

"""Zip file password cracker"""


def zip_crack(target_file, dictionary):

    password = ""
    zip_file = zipfile.ZipFile(target_file)  # Use ZipFile method to format target file
    with open(dictionary, "r") as dictionary:
        count = 0
        for line in dictionary.readlines():  # Loop through each line in wordlist
            password = line.strip("\n")
            try:
                zip_file.extractall(pwd=password)   # Try to crack the password
                print("Found password: {} in {} tries.".format(password, count))
                return
            except:
                pass
            finally:
                count += 1


def main():
    if len(sys.argv) != 2:
        print("Usage: python zipCracker.py archive.zip fasttrack.txt")
        print("sys.argv[1] should be the name of the zip file to crack")
        print("sys.argv[2] should be the path to the wordlist")

    print("Attempting to crack {} with {}".format(sys.argv[1], sys.argv[2]))
    zip_crack(sys.argv[1], sys.argv[2])

    zip_crack("archive.zip", "fasttrack.txt")


if __name__ == "__main__":
    main()
