Zip (including APK) Loading (#902)

* load ZipFile

makes it possible to directly load apk files

* use LoadFile for recursive zip opening

* set System.IO.Compression version

* keep identical format in AssetStudio.csproj

* try/catch the loading of each zip entry

* remove extra new line in FileReader.cs

* apply requested changes
This commit is contained in:
Rudolf Kolbe 2021-12-27 08:59:18 +01:00 committed by GitHub
parent e61a317185
commit 8d193a63cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 6 deletions

View File

@ -14,6 +14,7 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'net472' "> <ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
<PackageReference Include="System.Memory" Version="4.5.4" /> <PackageReference Include="System.Memory" Version="4.5.4" />
<PackageReference Include="System.IO.Compression" Version="4.0.0" />
<PackageReference Include="K4os.Compression.LZ4" Version="1.1.11" /> <PackageReference Include="K4os.Compression.LZ4" Version="1.1.11" />
</ItemGroup> </ItemGroup>

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using static AssetStudio.ImportHelper; using static AssetStudio.ImportHelper;
@ -84,6 +85,9 @@ namespace AssetStudio
case FileType.BrotliFile: case FileType.BrotliFile:
LoadFile(DecompressBrotli(reader)); LoadFile(DecompressBrotli(reader));
break; break;
case FileType.ZipFile:
LoadZipFile(reader);
break;
} }
} }
@ -234,6 +238,48 @@ namespace AssetStudio
} }
} }
private void LoadZipFile(FileReader reader)
{
Logger.Info("Loading " + reader.FileName);
try
{
using (ZipArchive archive = new ZipArchive(reader.BaseStream, ZipArchiveMode.Read))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
try
{
string dummyPath = Path.Combine(Path.GetDirectoryName(reader.FullPath), reader.FileName, entry.FullName);
// create a new stream
// - to store the deflated stream in
// - to keep the data for later extraction
Stream streamReader = new MemoryStream();
using (Stream entryStream = entry.Open())
{
entryStream.CopyTo(streamReader);
}
streamReader.Position = 0;
FileReader entryReader = new FileReader(dummyPath, streamReader);
LoadFile(entryReader);
}
catch (Exception e)
{
Logger.Error($"Error while reading zip entry {entry.FullName}", e);
}
}
}
}
catch (Exception e)
{
Logger.Error($"Error while reading zip file {reader.FileName}", e);
}
finally
{
reader.Dispose();
}
}
public void CheckStrippedVersion(SerializedFile assetsFile) public void CheckStrippedVersion(SerializedFile assetsFile)
{ {
if (assetsFile.IsVersionStripped && string.IsNullOrEmpty(SpecifyUnityVersion)) if (assetsFile.IsVersionStripped && string.IsNullOrEmpty(SpecifyUnityVersion))

View File

@ -11,6 +11,8 @@ namespace AssetStudio
private static readonly byte[] gzipMagic = { 0x1f, 0x8b }; private static readonly byte[] gzipMagic = { 0x1f, 0x8b };
private static readonly byte[] brotliMagic = { 0x62, 0x72, 0x6F, 0x74, 0x6C, 0x69 }; private static readonly byte[] brotliMagic = { 0x62, 0x72, 0x6F, 0x74, 0x6C, 0x69 };
private static readonly byte[] zipMagic = { 0x50, 0x4B, 0x03, 0x04 };
private static readonly byte[] zipSpannedMagic = { 0x50, 0x4B, 0x07, 0x08 };
public FileReader(string path) : this(path, File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { } public FileReader(string path) : this(path, File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { }
@ -36,7 +38,7 @@ namespace AssetStudio
return FileType.WebFile; return FileType.WebFile;
default: default:
{ {
var magic = ReadBytes(2); byte[] magic = ReadBytes(2);
Position = 0; Position = 0;
if (gzipMagic.SequenceEqual(magic)) if (gzipMagic.SequenceEqual(magic))
{ {
@ -53,13 +55,14 @@ namespace AssetStudio
{ {
return FileType.AssetsFile; return FileType.AssetsFile;
} }
else magic = ReadBytes(4);
{ Position = 0;
if (zipMagic.SequenceEqual(magic) || zipSpannedMagic.SequenceEqual(magic))
return FileType.ZipFile;
return FileType.ResourceFile; return FileType.ResourceFile;
} }
} }
} }
}
private bool IsSerializedFile() private bool IsSerializedFile()
{ {

View File

@ -13,6 +13,7 @@ namespace AssetStudio
WebFile, WebFile,
ResourceFile, ResourceFile,
GZipFile, GZipFile,
BrotliFile BrotliFile,
ZipFile
} }
} }