Add files via upload
[PyCatFile.git] / catfile.py
blob5bf27a8faf15e36a4c5faae927b852529fe5b139
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: 3/8/2024 Ver. 0.3.4 RC 1 - Author: cooldude2k $
18 '''
20 from __future__ import absolute_import, division, print_function, unicode_literals;
21 import os, sys, logging, argparse, pycatfile, binascii;
22 from io import open as open;
24 rarfile_support = pycatfile.rarfile_support;
26 if(sys.version[0]=="2"):
27 try:
28 from io import StringIO, BytesIO;
29 except ImportError:
30 try:
31 from cStringIO import StringIO;
32 from cStringIO import StringIO as BytesIO;
33 except ImportError:
34 from StringIO import StringIO;
35 from StringIO import StringIO as BytesIO;
36 elif(sys.version[0]>="3"):
37 from io import StringIO, BytesIO;
38 else:
39 teststringio = 0;
40 if(teststringio<=0):
41 try:
42 from cStringIO import StringIO as BytesIO;
43 teststringio = 1;
44 except ImportError:
45 teststringio = 0;
46 if(teststringio<=0):
47 try:
48 from StringIO import StringIO as BytesIO;
49 teststringio = 2;
50 except ImportError:
51 teststringio = 0;
52 if(teststringio<=0):
53 try:
54 from io import BytesIO;
55 teststringio = 3;
56 except ImportError:
57 teststringio = 0;
59 __project__ = pycatfile.__project__;
60 __program_name__ = pycatfile.__program_name__;
61 __file_format_name__ = pycatfile.__file_format_name__;
62 __file_format_lower__ = pycatfile.__file_format_lower__;
63 __file_format_len__ = pycatfile.__file_format_len__;
64 __file_format_hex__ = pycatfile.__file_format_hex__;
65 __file_format_delimiter__ = pycatfile.__file_format_delimiter__;
66 __file_format_list__ = pycatfile.__file_format_list__;
67 __project_url__ = pycatfile.__project_url__;
68 __version_info__ = pycatfile.__version_info__;
69 __version_date_info__ = pycatfile.__version_date_info__;
70 __version_date__ = pycatfile.__version_date__;
71 __version_date_plusrc__ = pycatfile.__version_date_plusrc__;
72 __version__ = pycatfile.__version__;
74 argparser = argparse.ArgumentParser(description="Manipulate concatenated files.", conflict_handler="resolve", add_help=True);
75 argparser.add_argument("-V", "--version", action="version", version=__program_name__ + " " + __version__);
76 argparser.add_argument("-i", "-f", "--input", help="Specify the file(s) to concatenate or the concatenated file to extract.", required=True);
77 argparser.add_argument("-d", "-v", "--verbose", action="store_true", help="Enable verbose mode to display various debugging information.");
78 argparser.add_argument("-c", "--create", action="store_true", help="Perform concatenation operation only.");
79 argparser.add_argument("-checksum", "--checksum", default="crc32", help="Specify the type of checksum to use. Default is crc32.");
80 argparser.add_argument("-e", "-x", "--extract", action="store_true", help="Perform extraction operation only.");
81 argparser.add_argument("-format", "--format", default=__file_format_list__[0], help="Specify the format to use");
82 argparser.add_argument("-delimiter", "--delimiter", default=__file_format_list__[4], help="Specify the format to use");
83 argparser.add_argument("-formatver", "--formatver", default=__file_format_list__[5], help="Specify the format version");
84 argparser.add_argument("-l", "-t", "--list", action="store_true", help="List files included in the concatenated file.");
85 argparser.add_argument("-repack", "--repack", action="store_true", help="Re-concatenate files, fixing checksum errors if any.");
86 argparser.add_argument("-o", "--output", default=None, help="Specify the name for the extracted concatenated files or the output concatenated file.");
87 argparser.add_argument("-compression", "--compression", default="auto", help="Specify the compression method to use for concatenation.");
88 argparser.add_argument("-level", "--level", default=None, help="Specify the compression level for concatenation.");
89 argparser.add_argument("-t", "-tar", "--converttar", action="store_true", help="Convert a tar file to a catfile.");
90 argparser.add_argument("-z", "-zip", "--convertzip", action="store_true", help="Convert a zip file to a catfile.");
91 argparser.add_argument("-rar", "--convertrar", action="store_true", help="Convert a rar file to a catfile.");
92 argparser.add_argument("-T", "--text", action="store_true", help="Read file locations from a text file.");
93 getargs = argparser.parse_args();
95 fname = getargs.format;
96 fnamelower = fname.lower();
97 fnamelen = len(fname);
98 fnamehex = binascii.hexlify(fname.encode("UTF-8")).decode("UTF-8");
99 fnamever = getargs.formatver;
100 fnamelist = [fname, fnamelower, fnamelen, fnamehex, getargs.delimiter, fnamever];
102 # Determine actions based on user input
103 should_create = getargs.create and not getargs.extract and not getargs.list;
104 should_extract = getargs.extract and not getargs.create and not getargs.list;
105 should_list = getargs.list and not getargs.create and not getargs.extract;
106 should_repack = getargs.create and getargs.repack;
108 # Execute the appropriate functions based on determined actions and arguments
109 if should_create:
110 if getargs.converttar:
111 pycatfile.PackArchiveFileFromTarFile(getargs.input, getargs.output, getargs.compression, getargs.level, getargs.checksum, [], fnamelist, getargs.verbose, False);
112 elif getargs.convertzip:
113 pycatfile.PackArchiveFileFromZipFile(getargs.input, getargs.output, getargs.compression, getargs.level, getargs.checksum, [], fnamelist, getargs.verbose, False);
114 elif rarfile_support and getargs.convertrar:
115 pycatfile.PackArchiveFileFromRarFile(getargs.input, getargs.output, getargs.compression, getargs.level, getargs.checksum, [], fnamelist, getargs.verbose, False);
116 else:
117 pycatfile.PackArchiveFile(getargs.input, getargs.output, getargs.text, getargs.compression, getargs.level, False, getargs.checksum, [], fnamelist, getargs.verbose, False);
119 if should_repack:
120 pycatfile.RePackArchiveFile(getargs.input, getargs.output, getargs.compression, getargs.level, False, 0, 0, getargs.checksum, False, [], fnamelist, getargs.verbose, True, False);
122 if should_extract:
123 pycatfile.UnPackArchiveFile(getargs.input, getargs.output, False, 0, 0, False, fnamelist, getargs.verbose, False);
125 if should_list:
126 if getargs.converttar:
127 pycatfile.TarFileListFiles(getargs.input, getargs.verbose, True, False);
128 elif getargs.convertzip:
129 pycatfile.ZipFileListFiles(getargs.input, getargs.verbose, True, False);
130 elif rarfile_support and getargs.convertrar:
131 pycatfile.RarFileListFiles(getargs.input, getargs.verbose, True, False);
132 else:
133 pycatfile.ArchiveFileListFiles(getargs.input, 0, 0, False, fnamelist, getargs.verbose, True, False);