Add files via upload
[PyCatFile.git] / catfile.py
blob70da14e1f13035850bccf3d65401eaca039c4679
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: catfile.py - Last Update: 4/10/2024 Ver. 0.7.6 RC 1 - Author: cooldude2k $
18 '''
20 from __future__ import absolute_import, division, print_function, unicode_literals;
21 import argparse, pycatfile, binascii;
23 rarfile_support = pycatfile.rarfile_support;
25 __project__ = pycatfile.__project__;
26 __program_name__ = pycatfile.__program_name__;
27 __file_format_name__ = pycatfile.__file_format_name__;
28 __file_format_lower__ = pycatfile.__file_format_lower__;
29 __file_format_magic__ = pycatfile.__file_format_magic__;
30 __file_format_len__ = pycatfile.__file_format_len__;
31 __file_format_hex__ = pycatfile.__file_format_hex__;
32 __file_format_delimiter__ = pycatfile.__file_format_delimiter__;
33 __file_format_list__ = pycatfile.__file_format_list__;
34 __use_new_style__ = pycatfile.__use_new_style__;
35 __use_advanced_list__ = pycatfile.__use_advanced_list__;
36 __use_alt_inode__ = pycatfile.__use_alt_inode__;
37 __project_url__ = pycatfile.__project_url__;
38 __version_info__ = pycatfile.__version_info__;
39 __version_date_info__ = pycatfile.__version_date_info__;
40 __version_date__ = pycatfile.__version_date__;
41 __version_date_plusrc__ = pycatfile.__version_date_plusrc__;
42 __version__ = pycatfile.__version__;
44 argparser = argparse.ArgumentParser(description="Manipulate concatenated files.", conflict_handler="resolve", add_help=True);
45 argparser.add_argument("-V", "--version", action="version", version=__program_name__ + " " + __version__);
46 argparser.add_argument("-i", "-f", "--input", help="Specify the file(s) to concatenate or the concatenated file to extract.", required=True);
47 argparser.add_argument("-d", "-v", "--verbose", action="store_true", help="Enable verbose mode to display various debugging information.");
48 argparser.add_argument("-c", "--create", action="store_true", help="Perform concatenation operation only.");
49 argparser.add_argument("-I", "-validate", "--validate", action="store_true", help="Validate CatFile checksums");
50 argparser.add_argument("-C", "-checksum", "--checksum", default="crc32", help="Specify the type of checksum to use. Default is crc32.");
51 argparser.add_argument("-S", "-skipchecksum", "--skipchecksum", action="store_true", help="Skip checksum check of files.");
52 argparser.add_argument("-e", "-x", "--extract", action="store_true", help="Perform extraction operation only.");
53 argparser.add_argument("-F", "-format", "--format", default=__file_format_list__[0], help="Specify the format to use");
54 argparser.add_argument("-D", "-delimiter", "--delimiter", default=__file_format_list__[5], help="Specify the format to use");
55 argparser.add_argument("-m", "-formatver", "--formatver", default=__file_format_list__[6], help="Specify the format version");
56 argparser.add_argument("-l", "-t", "--list", action="store_true", help="List files included in the concatenated file.");
57 argparser.add_argument("-p", "-preserve", "--preserve", action="store_false", help="Preserve permissions and time of files");
58 argparser.add_argument("-R", "-repack", "--repack", action="store_true", help="Re-concatenate files, fixing checksum errors if any.");
59 argparser.add_argument("-o", "--output", default=None, help="Specify the name for the extracted concatenated files or the output concatenated file.");
60 argparser.add_argument("-P", "-compression", "--compression", default="auto", help="Specify the compression method to use for concatenation.");
61 argparser.add_argument("-L", "-level", "--level", default=None, help="Specify the compression level for concatenation.");
62 argparser.add_argument("-t", "-tar", "--converttar", action="store_true", help="Convert a tar file to a catfile.");
63 argparser.add_argument("-z", "-zip", "--convertzip", action="store_true", help="Convert a zip file to a catfile.");
64 argparser.add_argument("-r", "-rar", "--convertrar", action="store_true", help="Convert a rar file to a catfile.");
65 argparser.add_argument("-T", "--text", action="store_true", help="Read file locations from a text file.");
66 getargs = argparser.parse_args();
68 fname = getargs.format;
69 fnamelower = fname.lower();
70 fnamemagic = fname;
71 fnamelen = len(fname);
72 fnamehex = binascii.hexlify(fname.encode("UTF-8")).decode("UTF-8");
73 fnamever = getargs.formatver;
74 fnamesty = __use_new_style__;
75 fnamelst = __use_advanced_list__;
76 fnameino = __use_alt_inode__;
77 fnamelist = [fname, fnamemagic, fnamelower, fnamelen, fnamehex, getargs.delimiter, fnamever, fnamesty, fnamelst, fnameino];
79 # Determine actions based on user input
80 should_create = getargs.create and not getargs.extract and not getargs.list;
81 should_extract = getargs.extract and not getargs.create and not getargs.list;
82 should_list = getargs.list and not getargs.create and not getargs.extract;
83 should_repack = getargs.create and getargs.repack;
84 should_validate = getargs.validate;
86 # Execute the appropriate functions based on determined actions and arguments
87 if should_create:
88 if getargs.converttar:
89 pycatfile.PackArchiveFileFromTarFile(getargs.input, getargs.output, getargs.compression, getargs.level, getargs.checksum, [], fnamelist, getargs.verbose, False);
90 elif getargs.convertzip:
91 pycatfile.PackArchiveFileFromZipFile(getargs.input, getargs.output, getargs.compression, getargs.level, getargs.checksum, [], fnamelist, getargs.verbose, False);
92 elif rarfile_support and getargs.convertrar:
93 pycatfile.PackArchiveFileFromRarFile(getargs.input, getargs.output, getargs.compression, getargs.level, getargs.checksum, [], fnamelist, getargs.verbose, False);
94 else:
95 pycatfile.PackArchiveFile(getargs.input, getargs.output, getargs.text, getargs.compression, getargs.level, False, getargs.checksum, [], fnamelist, getargs.verbose, False);
97 elif should_repack:
98 pycatfile.RePackArchiveFile(getargs.input, getargs.output, getargs.compression, getargs.level, False, 0, 0, getargs.checksum, getargs.skipchecksum, [], fnamelist, getargs.verbose, False);
100 elif should_extract:
101 pycatfile.UnPackArchiveFile(getargs.input, getargs.output, False, 0, 0, getargs.skipchecksum, fnamelist, getargs.verbose, getargs.preserve, getargs.preserve, False);
103 elif should_list:
104 if getargs.converttar:
105 pycatfile.TarFileListFiles(getargs.input, getargs.verbose, False);
106 elif getargs.convertzip:
107 pycatfile.ZipFileListFiles(getargs.input, getargs.verbose, False);
108 elif rarfile_support and getargs.convertrar:
109 pycatfile.RarFileListFiles(getargs.input, getargs.verbose, False);
110 else:
111 pycatfile.ArchiveFileListFiles(getargs.input, 0, 0, getargs.skipchecksum, fnamelist, getargs.verbose, False);
113 elif should_validate:
114 fvalid = pycatfile.ArchiveFileValidate(getargs.input, fnamelist, getargs.verbose, False);
115 if(not getargs.verbose):
116 import sys, logging;
117 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
118 if(fvalid):
119 pycatfile.VerbosePrintOut("File is valid: \n" + str(getargs.input));
120 else:
121 pycatfile.VerbosePrintOut("File is invalid: \n" + str(getargs.input));