Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/TorchSharp/Utils/Decompress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Expand Down