From ff9dc0b2cf3a7c281e32ce7bb229f5e7ecff03dd Mon Sep 17 00:00:00 2001 From: yummyash Date: Tue, 4 Nov 2025 12:57:25 +0530 Subject: [PATCH] Add Simple File Zipper utility script --- file_zipper.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 file_zipper.py diff --git a/file_zipper.py b/file_zipper.py new file mode 100644 index 0000000..da595c4 --- /dev/null +++ b/file_zipper.py @@ -0,0 +1,59 @@ +""" +Simple File Zipper +------------------ +A utility script that creates a ZIP archive from one or more files/directories +using Python's built-in zipfile module. + +Usage: + python file_zipper.py ... + +Example: + python file_zipper.py archive.zip file1.txt folder1 +""" + +import sys +import os +import zipfile + + +def zip_paths(output_zip, paths): + """Create a ZIP archive from the provided file and directory paths.""" + try: + with zipfile.ZipFile(output_zip, "w", zipfile.ZIP_DEFLATED) as zipf: + for path in paths: + if os.path.isfile(path): + # Add single file + zipf.write(path, arcname=os.path.basename(path)) + print(f"Added file: {path}") + + elif os.path.isdir(path): + # Add entire directory + for root, _, files in os.walk(path): + for file in files: + full_path = os.path.join(root, file) + relative_path = os.path.relpath(full_path, path) + zipf.write(full_path, arcname=os.path.join(os.path.basename(path), relative_path)) + print(f"Added file: {full_path}") + + else: + print(f"⚠️ Skipped (not found): {path}") + + print(f"\n✅ ZIP file created successfully: {output_zip}") + + except Exception as e: + print(f"❌ Error while creating ZIP: {e}") + + +def main(): + if len(sys.argv) < 3: + print("Usage: python file_zipper.py ...") + sys.exit(1) + + output_zip = sys.argv[1] + paths = sys.argv[2:] + + zip_paths(output_zip, paths) + + +if __name__ == "__main__": + main()