forked from GameMaker2k/PyCatFile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchivefile.py
executable file
·248 lines (233 loc) · 13.6 KB
/
archivefile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
This program is free software; you can redistribute it and/or modify
it under the terms of the Revised BSD License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Revised BSD License for more details.
Copyright 2018-2024 Cool Dude 2k - http://idb.berlios.de/
Copyright 2018-2024 Game Maker 2k - http://intdb.sourceforge.net/
Copyright 2018-2024 Kazuki Przyborowski - https://github.com/KazukiPrzyborowski
$FileInfo: archivefile.py - Last Update: 1/31/2025 Ver. 0.18.0 RC 1 - Author: cooldude2k $
'''
from __future__ import absolute_import, division, print_function, unicode_literals, generators, with_statement, nested_scopes
import os
import sys
import argparse
import pyarchivefile
import binascii
# Conditional import and signal handling for Unix-like systems
if os.name != 'nt': # Not Windows
import signal
def handler(signum, frame):
pyarchivefile.VerbosePrintOut(
"Received SIGPIPE, exiting gracefully.", "info")
sys.exit(0)
signal.signal(signal.SIGPIPE, handler)
rarfile_support = pyarchivefile.rarfile_support
py7zr_support = pyarchivefile.py7zr_support
if(sys.version[0] == "2"):
try:
from io import StringIO, BytesIO
except ImportError:
try:
from cStringIO import StringIO
from cStringIO import StringIO as BytesIO
except ImportError:
from StringIO import StringIO
from StringIO import StringIO as BytesIO
elif(sys.version[0] >= "3"):
from io import StringIO, BytesIO
else:
teststringio = 0
if(teststringio <= 0):
try:
from cStringIO import StringIO as BytesIO
teststringio = 1
except ImportError:
teststringio = 0
if(teststringio <= 0):
try:
from StringIO import StringIO as BytesIO
teststringio = 2
except ImportError:
teststringio = 0
if(teststringio <= 0):
try:
from io import BytesIO
teststringio = 3
except ImportError:
teststringio = 0
__project__ = pyarchivefile.__project__
__program_name__ = pyarchivefile.__program_name__
__file_format_name__ = pyarchivefile.__file_format_name__
__file_format_magic__ = pyarchivefile.__file_format_magic__
__file_format_len__ = pyarchivefile.__file_format_len__
__file_format_hex__ = pyarchivefile.__file_format_hex__
__file_format_delimiter__ = pyarchivefile.__file_format_delimiter__
__file_format_dict__ = pyarchivefile.__file_format_dict__
__file_format_default__ = pyarchivefile.__file_format_default__
__file_format_multi_dict__ = pyarchivefile.__file_format_multi_dict__
__use_new_style__ = pyarchivefile.__use_new_style__
__use_advanced_list__ = pyarchivefile.__use_advanced_list__
__use_alt_inode__ = pyarchivefile.__use_alt_inode__
__project_url__ = pyarchivefile.__project_url__
__version_info__ = pyarchivefile.__version_info__
__version_date_info__ = pyarchivefile.__version_date_info__
__version_date__ = pyarchivefile.__version_date__
__version_date_plusrc__ = pyarchivefile.__version_date_plusrc__
__version__ = pyarchivefile.__version__
# Initialize the argument parser
argparser = argparse.ArgumentParser(
description="Manipulate archive files.", conflict_handler="resolve", add_help=True)
# Version information
argparser.add_argument("-V", "--version", action="version",
version=__program_name__ + " " + __version__)
# Input and output specifications
argparser.add_argument(
"-i", "--input", nargs="+", help="Specify the file(s) to concatenate or the archive file to extract.", required=True)
argparser.add_argument("-o", "--output", default=None,
help="Specify the name for the extracted or output archive files.")
# Operations
argparser.add_argument("-c", "--create", action="store_true",
help="Perform only the concatenation operation.")
argparser.add_argument("-e", "--extract", action="store_true",
help="Perform only the extraction operation.")
argparser.add_argument("-t", "--convert", action="store_true",
help="Convert a tar/zip/rar/7zip file to a archive file.")
argparser.add_argument("-r", "--repack", action="store_true",
help="Re-concatenate files, fixing checksum errors if any.")
# File manipulation options
argparser.add_argument(
"-F", "--format", default="auto", help="Specify the format to use.")
argparser.add_argument(
"-D", "--delimiter", default=__file_format_dict__['format_delimiter'], help="Specify the delimiter to use.")
argparser.add_argument(
"-m", "--formatver", default=__file_format_dict__['format_ver'], help="Specify the format version.")
argparser.add_argument("-l", "--list", action="store_true",
help="List files included in the archive file.")
# Compression options
argparser.add_argument("-P", "--compression", default="auto",
help="Specify the compression method to use for concatenation.")
argparser.add_argument("-L", "--level", default=None,
help="Specify the compression level for concatenation.")
argparser.add_argument("-W", "--wholefile", action="store_true",
help="Whole file compression method to use for concatenation.")
# Checksum and validation
argparser.add_argument("-v", "--validate", action="store_true",
help="Validate archive file checksums.")
argparser.add_argument("-C", "--checksum", default="crc32",
help="Specify the type of checksum to use. The default is crc32.")
argparser.add_argument("-s", "--skipchecksum", action="store_true",
help="Skip the checksum check of files.")
# Permissions and metadata
argparser.add_argument("-p", "--preserve", action="store_false",
help="Do not preserve permissions and timestamps of files.")
# Miscellaneous
argparser.add_argument("-d", "--verbose", action="store_true",
help="Enable verbose mode to display various debugging information.")
argparser.add_argument("-T", "--text", action="store_true",
help="Read file locations from a text file.")
# Parse the arguments
getargs = argparser.parse_args()
fname = getargs.format
if(getargs.format=="auto"):
fnamedict = __file_format_multi_dict__
__file_format_default__ = getargs.format
else:
fnamemagic = fname
fnamelen = len(fname)
fnamehex = binascii.hexlify(fname.encode("UTF-8")).decode("UTF-8")
__file_format_default__ = fnamemagic
fnamesty = __use_new_style__
fnamelst = __use_advanced_list__
fnameino = __use_alt_inode__
fnamedict = {'format_name': fname, 'format_magic': fnamemagic, 'format_len': fnamelen, 'format_hex': fnamehex,
'format_delimiter': getargs.delimiter, 'format_ver': getargs.formatver, 'new_style': fnamesty, 'use_advanced_list': fnamelst, 'use_alt_inode': fnameino}
# Determine the primary action based on user input
actions = ['create', 'extract', 'list', 'repack', 'validate']
active_action = next(
(action for action in actions if getattr(getargs, action)), None)
input_file = getargs.input[0]
# Execute the appropriate functions based on determined actions and arguments
if active_action:
if active_action == 'create':
if getargs.convert:
checkcompressfile = pyarchivefile.CheckCompressionSubType(
input_file, fnamedict, True)
if((IsNestedDict(fnamedict) and compresscheck in fnamedict) or (IsSingleDict(fnamedict) and compresscheck==fnamedict['format_magic'])):
tmpout = pyarchivefile.RePackArchiveFile(input_file, getargs.output, getargs.compression, getargs.wholefile, getargs.level, pyarchivefile.compressionlistalt, False, 0, 0, [getargs.checksum, getargs.checksum, getargs.checksum], getargs.skipchecksum, [], fnamedict, getargs.verbose, False)
else:
tmpout = pyarchivefile.PackArchiveFileFromInFile(
input_file, getargs.output, __file_format_default__, getargs.compression, getargs.wholefile, getargs.level, pyarchivefile.compressionlistalt, [getargs.checksum, getargs.checksum, getargs.checksum], [], fnamedict, getargs.verbose, False)
if(not tmpout):
sys.exit(1)
else:
pyarchivefile.PackArchiveFile(getargs.input, getargs.output, getargs.text, __file_format_default__, getargs.compression, getargs.wholefile, getargs.level, pyarchivefile.compressionlistalt, False, [getargs.checksum, getargs.checksum, getargs.checksum], [], fnamedict, getargs.verbose, False)
elif active_action == 'repack':
if getargs.convert:
checkcompressfile = pyarchivefile.CheckCompressionSubType(
input_file, fnamedict, True)
if((IsNestedDict(fnamedict) and compresscheck in fnamedict) or (IsSingleDict(fnamedict) and compresscheck==fnamedict['format_magic'])):
pyarchivefile.RePackArchiveFile(input_file, getargs.output, getargs.compression, getargs.wholefile, getargs.level, pyarchivefile.compressionlistalt,
False, 0, 0, [getargs.checksum, getargs.checksum, getargs.checksum], getargs.skipchecksum, [], fnamedict, getargs.verbose, False)
else:
pyarchivefile.PackArchiveFileFromInFile(input_file, getargs.output, __file_format_default__, getargs.compression, getargs.wholefile, getargs.level, pyarchivefile.compressionlistalt, [getargs.checksum, getargs.checksum, getargs.checksum], [], fnamedict, getargs.verbose, False)
if(not tmpout):
sys.exit(1)
else:
pyarchivefile.RePackArchiveFile(input_file, getargs.output, getargs.compression, getargs.wholefile, getargs.level, pyarchivefile.compressionlistalt,
False, 0, 0, [getargs.checksum, getargs.checksum, getargs.checksum], getargs.skipchecksum, [], fnamedict, getargs.verbose, False)
elif active_action == 'extract':
if getargs.convert:
checkcompressfile = pyarchivefile.CheckCompressionSubType(
input_file, fnamedict, True)
tempout = BytesIO()
if((IsNestedDict(fnamedict) and compresscheck in fnamedict) or (IsSingleDict(fnamedict) and compresscheck==fnamedict['format_magic'])):
tmpout = pyarchivefile.RePackArchiveFile(input_file, tempout, getargs.compression, getargs.wholefile, getargs.level, pyarchivefile.compressionlistalt, False, 0, 0, [getargs.checksum, getargs.checksum, getargs.checksum], getargs.skipchecksum, [], fnamedict, False, False)
else:
tmpout = pyarchivefile.PackArchiveFileFromInFile(
input_file, tempout, __file_format_default__, getargs.compression, getargs.wholefile, getargs.level, pyarchivefile.compressionlistalt, [getargs.checksum, getargs.checksum, getargs.checksum], [], fnamedict, False, False)
if(not tmpout):
sys.exit(1)
input_file = tempout
pyarchivefile.UnPackArchiveFile(input_file, getargs.output, False, 0, 0, getargs.skipchecksum,
fnamedict, getargs.verbose, getargs.preserve, getargs.preserve, False)
elif active_action == 'list':
if getargs.convert:
checkcompressfile = pyarchivefile.CheckCompressionSubType(
input_file, fnamedict, True)
if((IsNestedDict(fnamedict) and compresscheck in fnamedict) or (IsSingleDict(fnamedict) and compresscheck==fnamedict['format_magic'])):
tmpout = pyarchivefile.ArchiveFileListFiles(input_file, "auto", 0, 0, getargs.skipchecksum, fnamedict, getargs.verbose, False)
else:
tmpout = pyarchivefile.InFileListFiles(input_file, getargs.verbose, fnamedict, False)
if(not tmpout):
sys.exit(1)
else:
pyarchivefile.ArchiveFileListFiles(input_file, "auto", 0, 0, getargs.skipchecksum, fnamedict, getargs.verbose, False)
elif active_action == 'validate':
if getargs.convert:
checkcompressfile = pyarchivefile.CheckCompressionSubType(
input_file, fnamedict, True)
tempout = BytesIO()
if((IsNestedDict(fnamedict) and compresscheck in fnamedict) or (IsSingleDict(fnamedict) and compresscheck==fnamedict['format_magic'])):
tmpout = pyarchivefile.RePackArchiveFile(input_file, tempout, getargs.compression, getargs.wholefile, getargs.level, pyarchivefile.compressionlistalt, False, 0, 0, [getargs.checksum, getargs.checksum, getargs.checksum], getargs.skipchecksum, [], fnamedict, False, False)
else:
tmpout = pyarchivefile.PackArchiveFileFromInFile(
input_file, tempout, __file_format_default__, getargs.compression, getargs.wholefile, getargs.level, pyarchivefile.compressionlistalt, [getargs.checksum, getargs.checksum, getargs.checksum], [], fnamedict, False, False)
input_file = tempout
if(not tmpout):
sys.exit(1)
fvalid = pyarchivefile.ArchiveFileValidate(
input_file, "auto", fnamedict, getargs.verbose, False)
if(not getargs.verbose):
import sys
import logging
logging.basicConfig(format="%(message)s",
stream=sys.stdout, level=logging.DEBUG)
if(fvalid):
pyarchivefile.VerbosePrintOut("File is valid: \n" + str(input_file))
else:
pyarchivefile.VerbosePrintOut("File is invalid: \n" + str(input_file))