Skip to content

Commit

Permalink
add tar.zst
Browse files Browse the repository at this point in the history
  • Loading branch information
AigioL committed Nov 16, 2021
1 parent 3b6e96a commit 9523512
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 286 deletions.
315 changes: 158 additions & 157 deletions Directory.Packages.props

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ Read what we [milestones](https://github.com/SteamTools-Team/SteamTools/mileston
* [TaskScheduler](https://github.com/dahall/taskscheduler)
* [SharpZipLib](https://github.com/icsharpcode/SharpZipLib)
* [SevenZipSharp](https://github.com/squid-box/SevenZipSharp)
* [ZstdNet](https://github.com/skbkontur/ZstdNet)
* [Depressurizer](https://github.com/Depressurizer/Depressurizer)
* [NLog](https://github.com/nlog/NLog)
* [NUnit](https://github.com/nunit/nunit)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ OS | Version | Architectures |
* [TaskScheduler](https://github.com/dahall/taskscheduler)
* [SharpZipLib](https://github.com/icsharpcode/SharpZipLib)
* [SevenZipSharp](https://github.com/squid-box/SevenZipSharp)
* [ZstdNet](https://github.com/skbkontur/ZstdNet)
* [Depressurizer](https://github.com/Depressurizer/Depressurizer)
* [NLog](https://github.com/nlog/NLog)
* [NUnit](https://github.com/nunit/nunit)
Expand Down
4 changes: 4 additions & 0 deletions src/Common.CoreLib/FileEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public static class FileEx

public const string MSI = ".msi";

public const string TAR_XZ = ".tar.xz";

public const string TAR_ZST = ".tar.zst";

public static string Clean(string extension, bool trimLeadingPeriod = false)
{
if (string.IsNullOrWhiteSpace(extension))
Expand Down
3 changes: 2 additions & 1 deletion src/ST.Tools.Publish/ST.Tools.Publish.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down Expand Up @@ -67,6 +67,7 @@
<PackageReference Include="TextCopy" />
<PackageReference Include="SharpZipLib" />
<PackageReference Include="Portable.BouncyCastle" />
<PackageReference Include="ZstdNet" />
</ItemGroup>

<ItemGroup>
Expand Down
216 changes: 88 additions & 128 deletions src/ST.Tools.Publish/Steps/Step_tgz_7z_tbr.cs
Original file line number Diff line number Diff line change
@@ -1,156 +1,116 @@
using System.Application.Models;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using static System.Application.Utils;

namespace System.Application.Steps
{
internal static class Step8_tar_gz
internal static class Step8_tar_gz // tgz
{
public static void Add(RootCommand command)
{
// tgz
var tgz = new Command("tgz", "8. (本地)读取上一步操作后的 Publish.json 生成压缩包并计算哈希值写入 Publish.json");
tgz.Handler = CommandHandler.Create(() =>
{
var publish_json_path = PublishJsonFilePath;
var publish_json_str = File.ReadAllText(publish_json_path);
var dirNames = Serializable.DJSON<PublishDirInfo[]>(publish_json_str);

if (!dirNames.Any_Nullable())
{
Console.WriteLine($"错误:发布配置文件读取失败!{publish_json_path}");
return;
}

dirNames = dirNames.ThrowIsNull(nameof(dirNames));

foreach (var item in dirNames)
{
var packPath = GetPackPath(item, FileEx.TAR_GZ);
Console.WriteLine($"正在生成压缩包:{packPath}");
IOPath.FileIfExistsItDelete(packPath);

CreatePack(packPath, item.Files);

using var fileStream = File.OpenRead(packPath);
var sha256 = Hashs.String.SHA256(fileStream);

if (item.BuildDownloads.ContainsKey(AppDownloadType.Compressed_GZip))
{
item.BuildDownloads[AppDownloadType.Compressed_GZip] = new PublishFileInfo { SHA256 = sha256, Length = fileStream.Length };
}
else
{
item.BuildDownloads.Add(AppDownloadType.Compressed_GZip, new PublishFileInfo { SHA256 = sha256, Length = fileStream.Length });
}
}

SavePublishJson(dirNames, removeFiles: true);

Console.WriteLine("完成");
});
command.AddCommand(tgz);
}
=> Step8.Add(command,
AppDownloadType.Compressed_GZip,
"tgz",
FileEx.TAR_GZ,
CreatePack);
}

internal static class Step8_7z
internal static class Step8_7z // 7z(Lzma2) 64,809,214 字节
{
public static void Add(RootCommand command)
{
// 7z
var _7z = new Command("7z", "8. (本地)读取上一步操作后的 Publish.json 生成压缩包并计算哈希值写入 Publish.json");
_7z.Handler = CommandHandler.Create(() =>
{
var publish_json_path = PublishJsonFilePath;
var publish_json_str = File.ReadAllText(publish_json_path);
var dirNames = Serializable.DJSON<PublishDirInfo[]>(publish_json_str);

if (!dirNames.Any_Nullable())
{
Console.WriteLine($"错误:发布配置文件读取失败!{publish_json_path}");
return;
}

dirNames = dirNames.ThrowIsNull(nameof(dirNames));

foreach (var item in dirNames)
{
var packPath = GetPackPath(item, FileEx._7Z);
Console.WriteLine($"正在生成压缩包:{packPath}");
IOPath.FileIfExistsItDelete(packPath);

CreateSevenZipPack(packPath, item.Files);

using var fileStream = File.OpenRead(packPath);
var sha256 = Hashs.String.SHA256(fileStream);

if (item.BuildDownloads.ContainsKey(AppDownloadType.Compressed_7z))
{
item.BuildDownloads[AppDownloadType.Compressed_7z] = new PublishFileInfo { SHA256 = sha256, Length = fileStream.Length };
}
else
{
item.BuildDownloads.Add(AppDownloadType.Compressed_7z, new PublishFileInfo { SHA256 = sha256, Length = fileStream.Length });
}
}
=> Step8.Add(command,
AppDownloadType.Compressed_7z,
"7z",
FileEx._7Z,
CreateSevenZipPack);
}

SavePublishJson(dirNames, removeFiles: true);
internal static class Step8_tar_br // tbr 70,278,650 字节
{
public static void Add(RootCommand command)
=> Step8.Add(command,
AppDownloadType.Compressed_Br,
"tbr",
FileEx.TAR_BR,
CreateBrotliPack);
}

Console.WriteLine("完成");
});
command.AddCommand(_7z);
}
internal static class Step8_tar_xz // tar.xz XZOutputStream.Flush NotSupportedException
{
public static void Add(RootCommand command)
=> Step8.Add(command,
AppDownloadType.Compressed_XZ,
"xz",
FileEx.TAR_XZ,
CreateXZPack);
}

internal static class Step8_tar_br
internal static class Step8_tar_zst // tar.zst 71,854,499 字节
{
public static void Add(RootCommand command)
=> Step8.Add(command,
AppDownloadType.Compressed_Zstd,
"zst",
FileEx.TAR_ZST,
CreateZstdPack);
}
}

namespace System.Application
{
partial class Utils
{
public static class Step8
{
// tbr
var tar_br = new Command("tbr", "8. (本地)读取上一步操作后的 Publish.json 生成压缩包并计算哈希值写入 Publish.json");
tar_br.Handler = CommandHandler.Create(() =>
public static void Add(RootCommand command, AppDownloadType type, string name, string fileEx, Action<string, IEnumerable<PublishFileInfo>> createPack)
{
var publish_json_path = PublishJsonFilePath;
var publish_json_str = File.ReadAllText(publish_json_path);
var dirNames = Serializable.DJSON<PublishDirInfo[]>(publish_json_str);

if (!dirNames.Any_Nullable())
{
Console.WriteLine($"错误:发布配置文件读取失败!{publish_json_path}");
return;
}

dirNames = dirNames.ThrowIsNull(nameof(dirNames));

foreach (var item in dirNames)
var comm = new Command(name, "8. (本地)读取上一步操作后的 Publish.json 生成压缩包并计算哈希值写入 Publish.json")
{
var packPath = GetPackPath(item, FileEx.TAR_BR);
Console.WriteLine($"正在生成压缩包:{packPath}");
IOPath.FileIfExistsItDelete(packPath);

CreateBrotliPack(packPath, item.Files);

using var fileStream = File.OpenRead(packPath);
var sha256 = Hashs.String.SHA256(fileStream);

if (item.BuildDownloads.ContainsKey(AppDownloadType.Compressed_Br))
Handler = CommandHandler.Create(() =>
{
item.BuildDownloads[AppDownloadType.Compressed_Br] = new PublishFileInfo { SHA256 = sha256, Length = fileStream.Length };
}
else
{
item.BuildDownloads.Add(AppDownloadType.Compressed_Br, new PublishFileInfo { SHA256 = sha256, Length = fileStream.Length });
}
}

SavePublishJson(dirNames, removeFiles: true);

Console.WriteLine("完成");
});
command.AddCommand(tar_br);
var publish_json_path = PublishJsonFilePath;
var publish_json_str = File.ReadAllText(publish_json_path);
var dirNames = Serializable.DJSON<PublishDirInfo[]>(publish_json_str);

if (!dirNames.Any_Nullable())
{
Console.WriteLine($"错误:发布配置文件读取失败!{publish_json_path}");
return;
}

dirNames = dirNames.ThrowIsNull(nameof(dirNames));

foreach (var item in dirNames)
{
var packPath = GetPackPath(item, fileEx);
Console.WriteLine($"正在生成压缩包:{packPath}");
IOPath.FileIfExistsItDelete(packPath);

createPack(packPath, item.Files);

using var fileStream = File.OpenRead(packPath);
var sha256 = Hashs.String.SHA256(fileStream);

if (item.BuildDownloads.ContainsKey(type))
{
item.BuildDownloads[type] = new PublishFileInfo { SHA256 = sha256, Length = fileStream.Length };
}
else
{
item.BuildDownloads.Add(type, new PublishFileInfo { SHA256 = sha256, Length = fileStream.Length });
}
}

SavePublishJson(dirNames, removeFiles: false);

Console.WriteLine("完成");
})
};
command.AddCommand(comm);
}
}
}
}
31 changes: 31 additions & 0 deletions src/ST.Tools.Publish/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Properties;
using System.Security.Cryptography;
using System.Text;
using ZstdNet;
using NCompressionMode = System.IO.Compression.CompressionMode;

namespace System.Application
Expand Down Expand Up @@ -130,6 +131,21 @@ public static void CreatePack(string packPath, IEnumerable<PublishFileInfo> file
}
}

public static void CreateXZPack(string packPath, IEnumerable<PublishFileInfo> files)
{
using var fs = File.Create(packPath);
using var s = new XZOutputStream(fs);
using var archive = TarArchive.CreateOutputTarArchive(s,
TarBuffer.DefaultBlockFactor, Encoding.UTF8);
foreach (var file in files)
{
Console.WriteLine($"正在压缩:{file.Path}");
var entry = TarEntry.CreateEntryFromFile(file.Path);
entry.Name = file.RelativePath;
archive.WriteEntry(entry, false);
}
}

public static void CreateBrotliPack(string packPath, IEnumerable<PublishFileInfo> files)
{
using var fs = File.Create(packPath);
Expand Down Expand Up @@ -162,6 +178,21 @@ public static void CreateSevenZipPack(string packPath, IEnumerable<PublishFileIn
compressor.CompressFileDictionary(dict, packPath);
}

public static void CreateZstdPack(string packPath, IEnumerable<PublishFileInfo> files)
{
using var fs = File.Create(packPath);
using var s = new CompressionStream(fs, new CompressionOptions(CompressionOptions.MaxCompressionLevel));
using var archive = TarArchive.CreateOutputTarArchive(s,
TarBuffer.DefaultBlockFactor, Encoding.UTF8);
foreach (var file in files)
{
Console.WriteLine($"正在压缩:{file.Path}");
var entry = TarEntry.CreateEntryFromFile(file.Path);
entry.Name = file.RelativePath;
archive.WriteEntry(entry, false);
}
}

public static void SavePublishJson(IEnumerable<PublishDirInfo> dirNames, bool removeFiles = false)
{
if (removeFiles)
Expand Down
10 changes: 10 additions & 0 deletions src/ST/AppDownloadType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,15 @@ public enum AppDownloadType : byte
/// 压缩包(Tar/Brotli)
/// </summary>
Compressed_Br,

/// <summary>
/// 压缩包(Tar/XZ)
/// </summary>
Compressed_XZ,

/// <summary>
/// 压缩包(Tar/Zstd)
/// </summary>
Compressed_Zstd,
}
}

0 comments on commit 9523512

Please sign in to comment.