Skip to content

Instantly share code, notes, and snippets.

@paulsinnett
Last active May 16, 2018 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulsinnett/aae1924c0ef21be0dcfee2c3a461f155 to your computer and use it in GitHub Desktop.
Save paulsinnett/aae1924c0ef21be0dcfee2c3a461f155 to your computer and use it in GitHub Desktop.
unitypackage unpacker
import argparse
import tarfile
import os
import io
class UnityAsset:
pass
parser = argparse.ArgumentParser(description='Unpack a .unitypackage')
parser.add_argument('INPUT_PACKAGE', help='path to a .unitypackage file')
parser.add_argument('-o', '--output', help='output directory (defaults to current directory)')
args = parser.parse_args()
print('.unitypackage file = ' + args.INPUT_PACKAGE)
if (args.output):
output_directory = args.output
else:
output_directory = '.'
paths = {}
with tarfile.open(args.INPUT_PACKAGE, 'r|gz') as archive:
for entry in archive.getmembers():
guid = os.path.dirname(entry.name)
if (guid not in paths):
paths[guid] = UnityAsset()
if (entry.isfile() and os.path.basename(entry.name) == 'pathname'):
paths[guid].pathname_file = entry
elif (os.path.basename(entry.name) == 'asset.meta'):
paths[guid].meta_file = entry
elif (os.path.basename(entry.name) == 'asset'):
paths[guid].asset_file = entry
for entry in paths:
if (hasattr(paths[entry], 'pathname_file')):
pathname = output_directory
pathname_file = paths[entry].pathname_file
with tarfile.open(args.INPUT_PACKAGE, 'r|gz') as archive:
with archive.extractfile(pathname_file) as pathfile:
with io.TextIOWrapper(io.BytesIO(pathfile.read())) as path:
pathname = os.path.join(output_directory, path.readline().strip())
os.makedirs(os.path.dirname(pathname), exist_ok=True)
if (hasattr(paths[entry], 'meta_file')):
print(pathname + '.meta')
with tarfile.open(args.INPUT_PACKAGE, 'r|gz') as archive:
with archive.extractfile(paths[entry].meta_file) as meta_file:
with open(pathname + '.meta', 'wb') as output_file:
output_file.write(meta_file.read())
if (hasattr(paths[entry], 'asset_file')):
print(pathname)
with tarfile.open(args.INPUT_PACKAGE, 'r|gz') as archive:
with archive.extractfile(paths[entry].asset_file) as asset_file:
with open(pathname, 'wb') as output_file:
output_file.write(asset_file.read())
@paulsinnett
Copy link
Author

extract file names

@paulsinnett
Copy link
Author

output files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment