in src/managed/DiffGen/archives/ZipArchives/GzArchive.cs [54:127]
public override bool TryTokenize(ItemDefinition archiveItem, out ArchiveTokenization tokens)
{
// Don't bother doing this for very small zips
if (archiveItem.Length < 10 * 1024 * 1024)
{
tokens = null;
return false;
}
ItemDefinition zippedItem = GetItemFromStream(Context.Stream);
if (!string.IsNullOrEmpty(Context.OriginalArchiveFileName))
{
zippedItem = zippedItem.WithName(Context.OriginalArchiveFileName);
}
var newTokens = new ArchiveTokenization("zip", "gz");
newTokens.ArchiveItem = zippedItem;
string unzippedPath;
ItemDefinition unzippedItem;
try
{
if (Context.UseCase == ArchiveUseCase.DiffSource)
{
unzippedItem = GetUnzippedItemFromStream(Context.Stream);
}
else
{
unzippedPath = zippedItem.GetExtractionPath(Context.WorkingFolder) + ".unzipped";
if (!File.Exists(unzippedPath))
{
var tmpPath = unzippedPath + ".tmp";
UnzipToFile(Context.Stream, tmpPath);
File.Move(tmpPath, unzippedPath, true);
}
unzippedItem = ItemDefinition.FromFile(unzippedPath);
if (TryDetermineGzRecipe(unzippedPath, unzippedItem, zippedItem, out var gzRecipe))
{
newTokens.AddForwardRecipe(gzRecipe);
}
else
{
Context.Logger?.LogWarning("Couldn't determine compression recipe for item: {unzippedItem}", unzippedItem);
}
}
}
catch (Exception e)
{
if (Context.Logger != null)
{
Context.Logger.Log(Context.LogLevel, $"[Failed to load stream as GZip. Error: {e.Message}]");
}
tokens = null;
return false;
}
Recipe unzipRecipe = new(RecipeType.ZlibDecompression, unzippedItem, new() { ZlibGzInitType }, new() { zippedItem });
newTokens.AddReverseRecipe(unzipRecipe);
if (!string.IsNullOrEmpty(Context.OriginalArchiveFileName))
{
string uncompressedFileName = Path.GetFileNameWithoutExtension(Context.OriginalArchiveFileName);
newTokens.AddRootPayload(uncompressedFileName, unzippedItem);
}
tokens = newTokens;
return true;
}