Update pycatfile.py
[PyCatFile.git] / neocatfile.py
blobc66f349b7f7c6d07d8552310f60fa6aa4b6975e3
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 '''
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the Revised BSD License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 Revised BSD License for more details.
13 Copyright 2018-2024 Cool Dude 2k - http://idb.berlios.de/
14 Copyright 2018-2024 Game Maker 2k - http://intdb.sourceforge.net/
15 Copyright 2018-2024 Kazuki Przyborowski - https://github.com/KazukiPrzyborowski
17 $FileInfo: neocatfile.py - ast Update: 10/22/2024 Ver. 0.14.2 RC 1 - Author: cooldude2k $
18 '''
20 from __future__ import absolute_import, division, print_function, unicode_literals, generators, with_statement, nested_scopes
21 import argparse
22 import pycatfile
24 # Compatibility layer for Python 2 and 3 input
25 try:
26 input = raw_input
27 except NameError:
28 pass
30 # Determine if rar file support is enabled
31 rarfile_support = pycatfile.rarfile_support
32 py7zr_support = pycatfile.py7zr_support
34 # Set up the argument parser
35 argparser = argparse.ArgumentParser(
36 description="Manipulates concatenated files for various operations like creation, extraction, and validation.")
37 argparser.add_argument("-V", "--version", action="version", version="{0} {1}".format(
38 pycatfile.__program_name__, pycatfile.__version__), help="Displays the program's version.")
39 argparser.add_argument("-i", "--input", required=True,
40 help="Specifies input file(s) for processing.")
41 argparser.add_argument(
42 "-o", "--output", help="Specifies the output file name.")
43 argparser.add_argument("-d", "--verbose", action="store_true",
44 help="Enables verbose mode for detailed information.")
45 argparser.add_argument("-c", "--create", action="store_true",
46 help="Creates a new concatenated file from input.")
47 argparser.add_argument("-e", "--extract", action="store_true",
48 help="Extracts files from a concatenated archive.")
49 argparser.add_argument("-l", "--list", action="store_true",
50 help="Lists contents of a specified concatenated file.")
51 argparser.add_argument("-r", "--repack", action="store_true",
52 help="Repacks an existing concatenated file.")
53 argparser.add_argument("-v", "--validate", action="store_true",
54 help="Validates a concatenated file's integrity.")
55 argparser.add_argument("--checksum", default="crc32",
56 help="Specifies the checksum type (default: crc32).")
57 argparser.add_argument("--compression", default="auto",
58 help="Specifies the compression method (default: auto).")
59 argparser.add_argument("--level", help="Specifies the compression level.")
60 argparser.add_argument("--preserve", action="store_true",
61 help="Preserves file attributes when extracting.")
62 argparser.add_argument("--convert", choices=['tar', 'zip', '7zip', 'rar'],
63 help="Convert from an archive format (tar, zip, 7zip, rar) to a concatenated file.")
64 args = argparser.parse_args()
66 # Determine the primary action based on user input
67 primary_action = None
68 if args.create:
69 primary_action = 'create'
70 elif args.repack:
71 primary_action = 'repack'
72 elif args.extract:
73 primary_action = 'extract'
74 elif args.list:
75 primary_action = 'list'
76 elif args.validate:
77 primary_action = 'validate'
79 # Functionality mappings
80 if primary_action == 'create':
81 if args.convert == 'tar':
82 pycatfile.PackArchiveFileFromTarFile(args.input, args.output, args.compression, args.level, args.checksum, [
83 ], pycatfile.__file_format_list__, args.verbose, False)
84 elif args.convert == 'zip':
85 pycatfile.PackArchiveFileFromZipFile(args.input, args.output, args.compression, args.level, args.checksum, [
86 ], pycatfile.__file_format_list__, args.verbose, False)
87 elif py7zr_support and args.convert == '7zip':
88 pycatfile.PackArchiveFileFromSevenZipFile(args.input, args.output, args.compression, args.level, args.checksum, [
89 ], pycatfile.__file_format_list__, args.verbose, False)
90 elif rarfile_support and args.convert == 'rar':
91 pycatfile.PackArchiveFileFromRarFile(args.input, args.output, args.compression, args.level, args.checksum, [
92 ], pycatfile.__file_format_list__, args.verbose, False)
93 else:
94 pycatfile.PackArchiveFile(args.input, args.output, args.verbose, args.compression, args.level,
95 False, args.checksum, [], pycatfile.__file_format_list__, args.verbose, False)
96 elif primary_action == 'repack':
97 pycatfile.RePackArchiveFile(
98 args.input, args.output, args.compression, args.level, args.checksum, args.verbose)
99 elif primary_action == 'extract':
100 pycatfile.UnPackArchiveFile(
101 args.input, args.output, args.verbose, args.preserve)
102 elif primary_action == 'list':
103 if args.convert == 'tar':
104 pycatfile.TarFileListFiles(args.input, verbose=args.verbose)
105 elif args.convert == 'zip':
106 pycatfile.ZipFileListFiles(args.input, verbose=args.verbose)
107 elif args.convert == '7zip':
108 pycatfile.SevenZipFileListFiles(args.input, verbose=args.verbose)
109 elif rarfile_support and args.convert == 'rar':
110 pycatfile.RarFileListFiles(args.input, verbose=args.verbose)
111 else:
112 pycatfile.ArchiveFileListFiles(args.input, verbose=args.verbose)
113 elif primary_action == 'validate':
114 is_valid = pycatfile.ArchiveFileValidate(args.input, args.verbose)
115 result_msg = "Validation result for {0}: {1}".format(
116 args.input, 'Valid' if is_valid else 'Invalid')
117 print(result_msg)