Skip to content

Commit

Permalink
Zip (including APK) Loading (#902)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
K0lb3 authored Dec 27, 2021
1 parent e61a317 commit 8d193a6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
1 change: 1 addition & 0 deletions AssetStudio/AssetStudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
<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" />
</ItemGroup>

Expand Down
46 changes: 46 additions & 0 deletions AssetStudio/AssetsManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using static AssetStudio.ImportHelper;
Expand Down Expand Up @@ -84,6 +85,9 @@ private void LoadFile(FileReader reader)
case FileType.BrotliFile:
LoadFile(DecompressBrotli(reader));
break;
case FileType.ZipFile:
LoadZipFile(reader);
break;
}
}

Expand Down Expand Up @@ -234,6 +238,48 @@ private void LoadWebFile(FileReader reader)
}
}

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)
{
if (assetsFile.IsVersionStripped && string.IsNullOrEmpty(SpecifyUnityVersion))
Expand Down
13 changes: 8 additions & 5 deletions AssetStudio/FileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class FileReader : EndianBinaryReader

private static readonly byte[] gzipMagic = { 0x1f, 0x8b };
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)) { }

Expand All @@ -36,7 +38,7 @@ private FileType CheckFileType()
return FileType.WebFile;
default:
{
var magic = ReadBytes(2);
byte[] magic = ReadBytes(2);
Position = 0;
if (gzipMagic.SequenceEqual(magic))
{
Expand All @@ -53,10 +55,11 @@ private FileType CheckFileType()
{
return FileType.AssetsFile;
}
else
{
return FileType.ResourceFile;
}
magic = ReadBytes(4);
Position = 0;
if (zipMagic.SequenceEqual(magic) || zipSpannedMagic.SequenceEqual(magic))
return FileType.ZipFile;
return FileType.ResourceFile;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion AssetStudio/FileType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum FileType
WebFile,
ResourceFile,
GZipFile,
BrotliFile
BrotliFile,
ZipFile
}
}

0 comments on commit 8d193a6

Please sign in to comment.