-
Notifications
You must be signed in to change notification settings - Fork 11
/
unbingz
executable file
·89 lines (67 loc) · 2.35 KB
/
unbingz
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
#!/usr/bin/env python3
#
# UnBinGz - Extract a Final Fantasy VII kernel archive file
#
# Copyright (C) Christian Bauer <www.cebix.net>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
__version__ = "1.4"
import sys
import os
import ff7
# Print usage information and exit.
def usage(exitcode, error = None):
print("Usage: %s [OPTION...] <file>" % os.path.basename(sys.argv[0]))
print(" -l, --list List files in archive")
print(" -V, --version Display version information and exit")
print(" -?, --help Show this help message")
if error is not None:
print("\nError:", error, file=sys.stderr)
sys.exit(exitcode)
# Parse command line arguments
inputFileName = None
listFiles = False
for arg in sys.argv[1:]:
if arg == "--version" or arg == "-V":
print("UnBinGz", __version__)
sys.exit(0)
elif arg == "--help" or arg == "-?":
usage(0)
elif arg == "--list" or arg == "-l":
listFiles = True
elif arg[0] == "-":
usage(64, "Invalid option '%s'" % arg)
else:
if inputFileName is None:
inputFileName = arg
else:
usage(64, "Unexpected extra argument '%s'" % arg)
if inputFileName is None:
usage(64, "No input file specified")
# Read the input archive
try:
inputFile = open(inputFileName, "rb")
except IOError as e:
print("Error opening file '%s': %s" % (inputFileName, e.strerror), file=sys.stderr)
sys.exit(1)
baseName = os.path.splitext(os.path.basename(inputFileName))[0]
# Process all files
archive = ff7.kernel.Archive(inputFile)
for f in archive.getFiles():
if listFiles:
# Print file information
print("%d/%d: %d bytes" % (f.dirID, f.index, f.rawDataSize))
else:
# Decompress file
outputFileName = "%s_%d_%d.data" % (baseName, f.dirID, f.index)
try:
outputFile = open(outputFileName, "wb")
except IOError as e:
print("Cannot create file '%s': %s" % (outputFileName, e.strerror), file=sys.stderr)
sys.exit(1)
outputFile.write(f.getData())
outputFile.close()
print("Written '%s'" % outputFileName)