From 21df9c48197a6707da3a5921b5a234682b5da59d Mon Sep 17 00:00:00 2001 From: alinpahontu2912 Date: Fri, 27 Feb 2026 12:43:49 +0200 Subject: [PATCH] Fix GZip decompression when server transparently decompresses .gz files Check for GZIP magic bytes (0x1F, 0x8B) before attempting decompression. If the file is not actually gzip-compressed (e.g., the HTTP server or CDN transparently decompressed it during transfer), copy it as-is instead of throwing a GZipException. Fixes TestMNISTDownload failure: GZipException: Error GZIP header, first magic byte doesn't match Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/TorchSharp/Utils/Decompress.cs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/TorchSharp/Utils/Decompress.cs b/src/TorchSharp/Utils/Decompress.cs index df0cb5404..c75677f87 100644 --- a/src/TorchSharp/Utils/Decompress.cs +++ b/src/TorchSharp/Utils/Decompress.cs @@ -23,14 +23,26 @@ public static class Decompress public static void DecompressGZipFile(string gzipFileName, string targetDir) { byte[] buf = new byte[4096]; + string fnOut = Path.Combine(targetDir, Path.GetFileNameWithoutExtension(gzipFileName)); - using (var fs = File.OpenRead(gzipFileName)) - using (var gzipStream = new GZipInputStream(fs)) { + using (var fs = File.OpenRead(gzipFileName)) { + // Check for GZIP magic bytes (0x1F, 0x8B) to detect whether the + // file is actually gzip-compressed. Some HTTP servers or CDNs + // transparently decompress .gz files during transfer, leaving + // an uncompressed file with a .gz extension. + var b1 = fs.ReadByte(); + var b2 = fs.ReadByte(); + fs.Position = 0; - string fnOut = Path.Combine(targetDir, Path.GetFileNameWithoutExtension(gzipFileName)); - - using (var fsOut = File.Create(fnOut)) { - StreamUtils.Copy(gzipStream, fsOut, buf); + if (b1 == 0x1F && b2 == 0x8B) { + using (var gzipStream = new GZipInputStream(fs)) + using (var fsOut = File.Create(fnOut)) { + StreamUtils.Copy(gzipStream, fsOut, buf); + } + } else { + using (var fsOut = File.Create(fnOut)) { + StreamUtils.Copy(fs, fsOut, buf); + } } } }