Add files via upload
[PyCatFile.git] / catfile.py
blob773a3b06ce18900f5036295d34440e86d56b4abd
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: 2/28/2024 Ver. 0.1.6 RC 1 - Author: cooldude2k $
18 '''
20 from __future__ import absolute_import, division, print_function, unicode_literals;
21 import os, sys, logging, argparse, pycatfile;
22 from io import open as open;
24 if(sys.version[0]=="2"):
25 try:
26 from io import StringIO, BytesIO;
27 except ImportError:
28 try:
29 from cStringIO import StringIO;
30 from cStringIO import StringIO as BytesIO;
31 except ImportError:
32 from StringIO import StringIO;
33 from StringIO import StringIO as BytesIO;
34 elif(sys.version[0]>="3"):
35 from io import StringIO, BytesIO;
36 else:
37 teststringio = 0;
38 if(teststringio<=0):
39 try:
40 from cStringIO import StringIO as BytesIO;
41 teststringio = 1;
42 except ImportError:
43 teststringio = 0;
44 if(teststringio<=0):
45 try:
46 from StringIO import StringIO as BytesIO;
47 teststringio = 2;
48 except ImportError:
49 teststringio = 0;
50 if(teststringio<=0):
51 try:
52 from io import BytesIO;
53 teststringio = 3;
54 except ImportError:
55 teststringio = 0;
57 __project__ = pycatfile.__project__;
58 __program_name__ = pycatfile.__program_name__;
59 __project_url__ = pycatfile.__project_url__;
60 __version_info__ = pycatfile.__version_info__;
61 __version_date_info__ = pycatfile.__version_date_info__;
62 __version_date__ = pycatfile.__version_date__;
63 __version_date_plusrc__ = pycatfile.__version_date_plusrc__;
64 __version__ = pycatfile.__version__;
65 __cat_header_ver__ = pycatfile.__cat_header_ver__;
67 argparser = argparse.ArgumentParser(description="Manipulate concatenated files.", conflict_handler="resolve", add_help=True);
68 argparser.add_argument("-V", "--version", action="version", version=__program_name__ + " " + __version__);
69 argparser.add_argument("-i", "-f", "--input", help="Specify the file(s) to concatenate or the concatenated file to extract.", required=True);
70 argparser.add_argument("-d", "-v", "--verbose", action="store_true", help="Enable verbose mode to display various debugging information.");
71 argparser.add_argument("-c", "--create", action="store_true", help="Perform concatenation operation only.");
72 argparser.add_argument("-checksum", "--checksum", default="crc32", help="Specify the type of checksum to use. Default is crc32.");
73 argparser.add_argument("-e", "-x", "--extract", action="store_true", help="Perform extraction operation only.");
74 argparser.add_argument("-l", "-t", "--list", action="store_true", help="List files included in the concatenated file.");
75 argparser.add_argument("-r", "--repack", action="store_true", help="Re-concatenate files, fixing checksum errors if any.");
76 argparser.add_argument("-o", "--output", default=None, help="Specify the name for the extracted concatenated files or the output concatenated file.");
77 argparser.add_argument("-compression", "--compression", default="auto", help="Specify the compression method to use for concatenation.");
78 argparser.add_argument("-level", "--level", default=None, help="Specify the compression level for concatenation.");
79 argparser.add_argument("-t", "--converttar", action="store_true", help="Convert a tar file to a catfile.");
80 argparser.add_argument("-z", "--convertzip", action="store_true", help="Convert a zip file to a catfile.");
81 argparser.add_argument("-T", "--text", action="store_true", help="Read file locations from a text file.");
82 getargs = argparser.parse_args();
84 # Determine actions based on user input
85 should_create = getargs.create and not getargs.extract and not getargs.list;
86 should_extract = getargs.extract and not getargs.create and not getargs.list;
87 should_list = getargs.list and not getargs.create and not getargs.extract;
88 should_repack = getargs.create and getargs.repack;
90 # Execute the appropriate functions based on determined actions and arguments
91 if should_create:
92 if getargs.converttar:
93 pycatfile.PackCatFileFromTarFile(getargs.input, getargs.output, getargs.compression, getargs.level, getargs.checksum, [], getargs.verbose, False);
94 elif getargs.convertzip:
95 pycatfile.PackCatFileFromZipFile(getargs.input, getargs.output, getargs.compression, getargs.level, getargs.checksum, [], getargs.verbose, False);
96 else:
97 pycatfile.PackCatFile(getargs.input, getargs.output, getargs.text, getargs.compression, getargs.level, False, getargs.checksum, [], getargs.verbose, False);
99 if should_repack:
100 pycatfile.RePackCatFile(getargs.input, getargs.output, getargs.compression, getargs.level, False, 0, 0, getargs.checksum, False, [], getargs.verbose, False);
102 if should_extract:
103 pycatfile.UnPackCatFile(getargs.input, getargs.output, False, 0, 0, False, getargs.verbose, False);
105 if should_list:
106 if getargs.converttar:
107 pycatfile.TarFileListFiles(getargs.input, getargs.verbose, False);
108 elif getargs.convertzip:
109 pycatfile.ZipFileListFiles(getargs.input, getargs.verbose, False);
110 else:
111 pycatfile.CatFileListFiles(getargs.input, 0, 0, False, getargs.verbose, False);