2 # -*- coding: utf-8 -*-
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/26/2024 Ver. 0.8.6 RC 1 - Author: cooldude2k $
20 from __future__
import absolute_import
, division
, print_function
, unicode_literals
;
21 import sys
, argparse
, pycatfile
, binascii
;
23 rarfile_support
= pycatfile
.rarfile_support
;
24 py7zr_support
= pycatfile
.py7zr_support
;
26 if(sys
.version
[0]=="2"):
28 from io
import StringIO
, BytesIO
;
31 from cStringIO
import StringIO
;
32 from cStringIO
import StringIO
as BytesIO
;
34 from StringIO
import StringIO
;
35 from StringIO
import StringIO
as BytesIO
;
36 elif(sys
.version
[0]>="3"):
37 from io
import StringIO
, BytesIO
;
42 from cStringIO
import StringIO
as BytesIO
;
48 from StringIO
import StringIO
as BytesIO
;
54 from io
import BytesIO
;
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_magic__
= pycatfile
.__file
_format
_magic
__;
64 __file_format_len__
= pycatfile
.__file
_format
_len
__;
65 __file_format_hex__
= pycatfile
.__file
_format
_hex
__;
66 __file_format_delimiter__
= pycatfile
.__file
_format
_delimiter
__;
67 __file_format_list__
= pycatfile
.__file
_format
_list
__;
68 __use_new_style__
= pycatfile
.__use
_new
_style
__;
69 __use_advanced_list__
= pycatfile
.__use
_advanced
_list
__;
70 __use_alt_inode__
= pycatfile
.__use
_alt
_inode
__;
71 __project_url__
= pycatfile
.__project
_url
__;
72 __version_info__
= pycatfile
.__version
_info
__;
73 __version_date_info__
= pycatfile
.__version
_date
_info
__;
74 __version_date__
= pycatfile
.__version
_date
__;
75 __version_date_plusrc__
= pycatfile
.__version
_date
_plusrc
__;
76 __version__
= pycatfile
.__version
__;
78 argparser
= argparse
.ArgumentParser(description
="Manipulate concatenated files.", conflict_handler
="resolve", add_help
=True);
79 argparser
.add_argument("-V", "--version", action
="version", version
=__program_name__
+ " " + __version__
);
80 argparser
.add_argument("-i", "--input", help="Specify the file(s) to concatenate or the concatenated file to extract.", required
=True);
81 argparser
.add_argument("-d", "--verbose", action
="store_true", help="Enable verbose mode to display various debugging information.");
82 argparser
.add_argument("-c", "--create", action
="store_true", help="Perform concatenation operation only.");
83 argparser
.add_argument("-v", "--validate", action
="store_true", help="Validate CatFile checksums.");
84 argparser
.add_argument("-C", "--checksum", default
="crc32", help="Specify the type of checksum to use. The default is crc32.");
85 argparser
.add_argument("-s", "--skipchecksum", action
="store_true", help="Skip the checksum check of files.");
86 argparser
.add_argument("-e", "--extract", action
="store_true", help="Perform extraction operation only.");
87 argparser
.add_argument("-F", "--format", default
=__file_format_list__
[0], help="Specify the format to use.");
88 argparser
.add_argument("-D", "--delimiter", default
=__file_format_list__
[5], help="Specify the delimiter to use.");
89 argparser
.add_argument("-m", "--formatver", default
=__file_format_list__
[6], help="Specify the format version.");
90 argparser
.add_argument("-l", "--list", action
="store_true", help="List files included in the concatenated file.");
91 argparser
.add_argument("-p", "--preserve", action
="store_false", help="Preserve permissions and timestamps of files.");
92 argparser
.add_argument("-R", "--repack", action
="store_true", help="Re-concatenate files, fixing checksum errors, if any.");
93 argparser
.add_argument("-o", "--output", default
=None, help="Specify the name for the extracted or output concatenated files.");
94 argparser
.add_argument("-P", "--compression", default
="auto", help="Specify the compression method to use for concatenation.");
95 argparser
.add_argument("-L", "--level", default
=None, help="Specify the compression level for concatenation.");
96 argparser
.add_argument("-t", "--convert", action
="store_true", help="Convert a tar / zip / rar / 7zip file to a CatFile.");
97 argparser
.add_argument("-T", "--text", action
="store_true", help="Read file locations from a text file.");
98 getargs
= argparser
.parse_args()
100 fname
= getargs
.format
;
101 fnamelower
= fname
.lower();
103 fnamelen
= len(fname
);
104 fnamehex
= binascii
.hexlify(fname
.encode("UTF-8")).decode("UTF-8");
105 fnamesty
= __use_new_style__
;
106 fnamelst
= __use_advanced_list__
;
107 fnameino
= __use_alt_inode__
;
108 fnamelist
= [fname
, fnamemagic
, fnamelower
, fnamelen
, fnamehex
, getargs
.delimiter
, getargs
.formatver
, fnamesty
, fnamelst
, fnameino
];
110 # Determine actions based on user input
111 should_create
= getargs
.create
and not getargs
.extract
and not getargs
.list;
112 should_extract
= getargs
.extract
and not getargs
.create
and not getargs
.list;
113 should_list
= getargs
.list and not getargs
.create
and not getargs
.extract
;
114 should_repack
= getargs
.create
and getargs
.repack
;
115 should_validate
= getargs
.validate
;
117 # Execute the appropriate functions based on determined actions and arguments
120 checkcompressfile
= pycatfile
.CheckCompressionSubType(getargs
.input, fnamelist
, True);
121 if(checkcompressfile
=="tarfile"):
122 pycatfile
.PackArchiveFileFromTarFile(getargs
.input, getargs
.output
, getargs
.compression
, getargs
.level
, getargs
.checksum
, [], fnamelist
, getargs
.verbose
, False);
123 elif(checkcompressfile
=="zipfile"):
124 pycatfile
.PackArchiveFileFromZipFile(getargs
.input, getargs
.output
, getargs
.compression
, getargs
.level
, getargs
.checksum
, [], fnamelist
, getargs
.verbose
, False);
125 elif(checkcompressfile
=="catfile"):
126 pycatfile
.RePackArchiveFile(getargs
.input, getargs
.output
, getargs
.compression
, getargs
.level
, False, 0, 0, getargs
.checksum
, getargs
.skipchecksum
, [], fnamelist
, getargs
.verbose
, False);
127 elif(rarfile_support
and checkcompressfile
=="rarfile"):
128 pycatfile
.PackArchiveFileFromRarFile(getargs
.input, getargs
.output
, getargs
.compression
, getargs
.level
, getargs
.checksum
, [], fnamelist
, getargs
.verbose
, False);
129 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
130 pycatfile
.PackArchiveFileFromSevenZipFile(getargs
.input, getargs
.output
, getargs
.compression
, getargs
.level
, getargs
.checksum
, [], fnamelist
, getargs
.verbose
, False);
134 pycatfile
.PackArchiveFile(getargs
.input, getargs
.output
, getargs
.text
, getargs
.compression
, getargs
.level
, False, getargs
.checksum
, [], fnamelist
, getargs
.verbose
, False);
138 checkcompressfile
= pycatfile
.CheckCompressionSubType(getargs
.input, fnamelist
, True);
139 if(checkcompressfile
=="tarfile"):
140 pycatfile
.PackArchiveFileFromTarFile(getargs
.input, getargs
.output
, getargs
.compression
, getargs
.level
, getargs
.checksum
, [], fnamelist
, getargs
.verbose
, False);
141 elif(checkcompressfile
=="zipfile"):
142 pycatfile
.PackArchiveFileFromZipFile(getargs
.input, getargs
.output
, getargs
.compression
, getargs
.level
, getargs
.checksum
, [], fnamelist
, getargs
.verbose
, False);
143 elif(checkcompressfile
=="catfile"):
144 pycatfile
.RePackArchiveFile(getargs
.input, getargs
.output
, getargs
.compression
, getargs
.level
, False, 0, 0, getargs
.checksum
, getargs
.skipchecksum
, [], fnamelist
, getargs
.verbose
, False);
145 elif(rarfile_support
and checkcompressfile
=="rarfile"):
146 pycatfile
.PackArchiveFileFromRarFile(getargs
.input, getargs
.output
, getargs
.compression
, getargs
.level
, getargs
.checksum
, [], fnamelist
, getargs
.verbose
, False);
147 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
148 pycatfile
.PackArchiveFileFromSevenZipFile(getargs
.input, getargs
.output
, getargs
.compression
, getargs
.level
, getargs
.checksum
, [], fnamelist
, getargs
.verbose
, False);
152 pycatfile
.RePackArchiveFile(getargs
.input, getargs
.output
, getargs
.compression
, getargs
.level
, False, 0, 0, getargs
.checksum
, getargs
.skipchecksum
, [], fnamelist
, getargs
.verbose
, False);
156 checkcompressfile
= pycatfile
.CheckCompressionSubType(getargs
.input, fnamelist
, True);
158 if(checkcompressfile
=="tarfile"):
159 pycatfile
.PackArchiveFileFromTarFile(getargs
.input, tempout
, getargs
.compression
, getargs
.level
, getargs
.checksum
, [], fnamelist
, getargs
.verbose
, False);
160 elif(checkcompressfile
=="zipfile"):
161 pycatfile
.PackArchiveFileFromZipFile(getargs
.input, tempout
, getargs
.compression
, getargs
.level
, getargs
.checksum
, [], fnamelist
, getargs
.verbose
, False);
162 elif(checkcompressfile
=="catfile"):
163 pycatfile
.RePackArchiveFile(getargs
.input, tempout
, getargs
.compression
, getargs
.level
, False, 0, 0, getargs
.checksum
, getargs
.skipchecksum
, [], fnamelist
, getargs
.verbose
, False);
164 elif(rarfile_support
and checkcompressfile
=="rarfile"):
165 pycatfile
.PackArchiveFileFromRarFile(getargs
.input, tempout
, getargs
.compression
, getargs
.level
, getargs
.checksum
, [], fnamelist
, getargs
.verbose
, False);
166 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
167 pycatfile
.PackArchiveFileFromSevenZipFile(getargs
.input, tempout
, getargs
.compression
, getargs
.level
, getargs
.checksum
, [], fnamelist
, getargs
.verbose
, False);
170 getargs
.input = tempout
;
171 pycatfile
.UnPackArchiveFile(getargs
.input, getargs
.output
, False, 0, 0, getargs
.skipchecksum
, fnamelist
, getargs
.verbose
, getargs
.preserve
, getargs
.preserve
, False);
175 checkcompressfile
= pycatfile
.CheckCompressionSubType(getargs
.input, fnamelist
, True);
176 if(checkcompressfile
=="tarfile"):
177 pycatfile
.TarFileListFiles(getargs
.input, getargs
.verbose
, False);
178 elif(checkcompressfile
=="zipfile"):
179 pycatfile
.ZipFileListFiles(getargs
.input, getargs
.verbose
, False);
180 elif(checkcompressfile
=="catfile"):
181 pycatfile
.ArchiveFileListFiles(getargs
.input, 0, 0, getargs
.skipchecksum
, fnamelist
, getargs
.verbose
, False);
182 elif(rarfile_support
and checkcompressfile
=="rarfile"):
183 pycatfile
.RarFileListFiles(getargs
.input, getargs
.verbose
, False);
184 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
185 pycatfile
.SevenZipFileListFiles(getargs
.input, getargs
.verbose
, False);
189 pycatfile
.ArchiveFileListFiles(getargs
.input, 0, 0, getargs
.skipchecksum
, fnamelist
, getargs
.verbose
, False);
191 elif should_validate
:
193 checkcompressfile
= pycatfile
.CheckCompressionSubType(getargs
.input, fnamelist
, True);
195 if(checkcompressfile
=="tarfile"):
196 pycatfile
.PackArchiveFileFromTarFile(getargs
.input, tempout
, getargs
.compression
, getargs
.level
, getargs
.checksum
, [], fnamelist
, getargs
.verbose
, False);
197 elif(checkcompressfile
=="zipfile"):
198 pycatfile
.PackArchiveFileFromZipFile(getargs
.input, tempout
, getargs
.compression
, getargs
.level
, getargs
.checksum
, [], fnamelist
, getargs
.verbose
, False);
199 elif(checkcompressfile
=="catfile"):
200 pycatfile
.RePackArchiveFile(getargs
.input, tempout
, getargs
.compression
, getargs
.level
, False, 0, 0, getargs
.checksum
, getargs
.skipchecksum
, [], fnamelist
, getargs
.verbose
, False);
201 elif(rarfile_support
and checkcompressfile
=="rarfile"):
202 pycatfile
.PackArchiveFileFromRarFile(getargs
.input, tempout
, getargs
.compression
, getargs
.level
, getargs
.checksum
, [], fnamelist
, getargs
.verbose
, False);
203 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
204 pycatfile
.PackArchiveFileFromSevenZipFile(getargs
.input, tempout
, getargs
.compression
, getargs
.level
, getargs
.checksum
, [], fnamelist
, getargs
.verbose
, False);
207 getargs
.input = tempout
;
208 fvalid
= pycatfile
.ArchiveFileValidate(getargs
.input, fnamelist
, getargs
.verbose
, False);
209 if(not getargs
.verbose
):
211 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
213 pycatfile
.VerbosePrintOut("File is valid: \n" + str(getargs
.input));
215 pycatfile
.VerbosePrintOut("File is invalid: \n" + str(getargs
.input));