Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import shutil, zipfile,os
- def main():
- os.system('clear')
- #main menu
- COMPRESS = 1
- DECOMP = 2
- QUIT = 0
- choice = 99
- while choice != QUIT:
- menu()
- try:
- CHOICE = int(input("\nSelect an option: "))
- if CHOICE == COMPRESS:
- #inialize file compression function
- fileCompression()
- elif CHOICE == DECOMP:
- #inialize file decompression function
- fileDeCompression()
- elif CHOICE == QUIT:
- #quit
- print("Bye for now!")
- break
- else:
- #handle if user doesn't input a valid option
- print("\nPlease enter a valid option (1,2 and 0)")
- #error handling if user inputs a non integer
- except ValueError as err:
- print("\nEnter an integer (1,2 and 0)")
- print(err)
- def fileCompression():
- os.system('clear')
- print(" CREATE A COMPRESSED FILE")
- print("---------------------------------")
- print("\nnote: the only working compression file type is .zip - file will automatically be saved as a zip file")
- #take in information
- new_directory_name = input("\nPlease name your compressed file: ")
- archive_from = input("\nPlease enter the directory (and path) you want to archive: ")
- #user input for number of copies
- backup_count = int(input("How many copies do you want to create?: "))
- print("\nEnter a directory (and path) you want to save your compressed file to:")
- new_directory = input("Path: ")
- #add forward slash to end of directory if user doesn't add a slash
- if new_directory[-1] != '/':
- new_directory = new_directory + '/'
- #loop through number of copies
- for num in range(backup_count):
- #if iteration is equal to 0, do not add numeric addition to title
- if num == 0:
- zippedFile = shutil.make_archive(f"{new_directory}{new_directory_name}", 'zip', archive_from)
- print("Zipped File Created!")
- #add numeric ending so multiple files can be created
- else:
- zippedFile = shutil.make_archive(f"{new_directory}{new_directory_name}_{str(num)}", 'zip', archive_from)
- print("Zipped File Created!")
- print("\nJob Completed.")
- #continue?
- choice = input("Compress another file (y/n)?: ").lower()
- if choice == "y":
- fileCompression()
- else:
- main()
- def fileDeCompression():
- os.system('clear')
- print(" DECOMPRESS A FILE")
- print("--------------------------------")
- #take in information
- print("\nnote: the only working compression file type is .zip")
- file_name = input("\nEnter file you want to unpack (including file extension and directory path): ")
- #add file extension if user forgets
- if ".zip" not in file_name:
- file_name = file_name + ".zip"
- extract_dir = input(f"\nEnter directory and path that you want to extract to (leave blank to use current directory: {os.getcwd()}): ")
- #extract content from desired file
- shutil.unpack_archive(file_name,extract_dir,'zip')
- print("File has been unpacked!")
- #continue?
- choice = input("Do you want to delete the compressed file (y/n)?: ").lower()
- if choice == 'y':
- os.remove(file_name)
- choice = input("Do you want to decompress another file (y/n)?: ").lower()
- if choice == 'y':
- fileDeCompression()
- else:
- main()
- else:
- choice = input("Do you want to decompress another file(y/n)?: ").lower()
- if choice == 'y':
- fileDeCompression()
- else:
- main()
- def menu():
- #main menu screen
- print(" File Compression Application")
- print("-----------------------------------------")
- print("MENU")
- print("1. Compress a directory")
- print("2. Decompress an existing file")
- print("0. QUIT")
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement