支持GZip自动解压

This commit is contained in:
nilaoda 2022-12-02 14:29:02 +08:00
parent 02bf9e94a5
commit 6e93bbd698
5 changed files with 39 additions and 9 deletions

View File

@ -11,10 +11,7 @@ using N_m3u8DL_RE.Parser;
using N_m3u8DL_RE.Parser.Mp4; using N_m3u8DL_RE.Parser.Mp4;
using N_m3u8DL_RE.Util; using N_m3u8DL_RE.Util;
using Spectre.Console; using Spectre.Console;
using Spectre.Console.Rendering;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Text; using System.Text;
namespace N_m3u8DL_RE.DownloadManager namespace N_m3u8DL_RE.DownloadManager
@ -48,12 +45,12 @@ namespace N_m3u8DL_RE.DownloadManager
private string? ReadInit(string output) private string? ReadInit(string output)
{ {
var header = new byte[4096]; //4KB
using (var fs = File.OpenRead(output)) using (var fs = File.OpenRead(output))
{ {
var header = new byte[4096]; //4KB
fs.Read(header); fs.Read(header);
return ReadInit(header);
} }
return ReadInit(header);
} }
//从文件读取KEY //从文件读取KEY
@ -173,8 +170,7 @@ namespace N_m3u8DL_RE.DownloadManager
//读取mp4信息 //读取mp4信息
if (result != null && result.Success) if (result != null && result.Success)
{ {
var data = File.ReadAllBytes(result.ActualFilePath); currentKID = ReadInit(result.ActualFilePath);
currentKID = ReadInit(data);
//从文件读取KEY //从文件读取KEY
await SearchKeyAsync(currentKID); await SearchKeyAsync(currentKID);
//实时解密 //实时解密

View File

@ -67,6 +67,11 @@ namespace N_m3u8DL_RE.Downloader
{ {
await ImageHeaderUtil.ProcessAsync(dResult.ActualFilePath); await ImageHeaderUtil.ProcessAsync(dResult.ActualFilePath);
} }
//Gzip解压
if (dResult.GzipHeader)
{
await OtherUtil.DeGzipFileAsync(dResult.ActualFilePath);
}
} }
return dResult; return dResult;
} }

View File

@ -13,6 +13,7 @@ namespace N_m3u8DL_RE.Entity
public long? RespContentLength { get; set; } public long? RespContentLength { get; set; }
public long? ActualContentLength { get; set; } public long? ActualContentLength { get; set; }
public bool ImageHeader { get; set; } = false; //图片伪装 public bool ImageHeader { get; set; } = false; //图片伪装
public bool GzipHeader { get; set; } = false; //GZip压缩
public required string ActualFilePath { get; set; } public required string ActualFilePath { get; set; }
} }
} }

View File

@ -108,11 +108,13 @@ namespace N_m3u8DL_RE.Util
var buffer = new byte[16 * 1024]; var buffer = new byte[16 * 1024];
var size = 0; var size = 0;
//检测imageHeader
size = await responseStream.ReadAsync(buffer, cancellationTokenSource.Token); size = await responseStream.ReadAsync(buffer, cancellationTokenSource.Token);
speedContainer.Add(size); speedContainer.Add(size);
await stream.WriteAsync(buffer, 0, size); await stream.WriteAsync(buffer, 0, size);
//检测imageHeader
bool imageHeader = ImageHeaderUtil.IsImageHeader(buffer); bool imageHeader = ImageHeaderUtil.IsImageHeader(buffer);
//检测GZipFor DDP Audio
bool gZipHeader = buffer.Length > 2 && buffer[0] == 0x1f && buffer[1] == 0x8b;
while ((size = await responseStream.ReadAsync(buffer, cancellationTokenSource.Token)) > 0) while ((size = await responseStream.ReadAsync(buffer, cancellationTokenSource.Token)) > 0)
{ {
@ -125,7 +127,8 @@ namespace N_m3u8DL_RE.Util
ActualContentLength = stream.Length, ActualContentLength = stream.Length,
RespContentLength = contentLength, RespContentLength = contentLength,
ActualFilePath = path, ActualFilePath = path,
ImageHeader= imageHeader ImageHeader= imageHeader,
GzipHeader = gZipHeader
}; };
} }
catch (OperationCanceledException oce) when (oce.CancellationToken == cancellationTokenSource.Token) catch (OperationCanceledException oce) when (oce.CancellationToken == cancellationTokenSource.Token)

View File

@ -2,6 +2,7 @@
using N_m3u8DL_RE.Common.Log; using N_m3u8DL_RE.Common.Log;
using N_m3u8DL_RE.Enum; using N_m3u8DL_RE.Enum;
using System.CommandLine; using System.CommandLine;
using System.IO.Compression;
using System.Text; using System.Text;
namespace N_m3u8DL_RE.Util namespace N_m3u8DL_RE.Util
@ -110,5 +111,29 @@ namespace N_m3u8DL_RE.Util
} }
SafeDeleteDir(parent); SafeDeleteDir(parent);
} }
/// <summary>
/// 解压并替换原文件
/// </summary>
/// <param name="filePath"></param>
public static async Task DeGzipFileAsync(string filePath)
{
string deGzipFile = Path.ChangeExtension(filePath, ".tmp");
try
{
using (var fileToDecompressAsStream = File.OpenRead(filePath))
{
using var decompressedStream = File.Create(deGzipFile);
using var decompressionStream = new GZipStream(fileToDecompressAsStream, CompressionMode.Decompress);
await decompressionStream.CopyToAsync(decompressedStream);
}
File.Delete(filePath);
File.Move(deGzipFile, filePath);
}
catch
{
if (File.Exists(deGzipFile)) File.Delete(deGzipFile);
}
}
} }
} }