在 .NET 7 以後有內建 System.Formats.Tar ,不需要額外的套件就可以壓縮和解壓縮 tar 檔案
解壓縮:
文章撰寫中...請稍後...
參考資料:
Microsoft.Learn - System.Formats.Tar Namespace
GitHub - [API Proposal]: APIs to support tar archives #65951
using System.Formats.Tar;
// 建立 tar 檔案
var tarFilePath = "output.tar";
using var tarStream = File.Create(tarFilePath);
using var writer = new TarWriter(tarStream, leaveOpen: false);
// 寫入檔案
var sourceFilePath = "C:\\Users\\ruyut\\Downloads\\my.msi";
// 實際檔案路徑, tar 檔案內的路徑
writer.WriteEntry(sourceFilePath, Path.GetFileName(sourceFilePath));
解壓縮:
using System.Formats.Tar;
// 解壓縮 tar 檔案
using var tarStream = File.OpenRead("output.tar");
using var reader = new TarReader(tarStream);
TarEntry? entry;
while ((entry = reader.GetNextEntry()) is not null)
{
var outputPath = Path.Combine("output", entry.Name);
Directory.CreateDirectory(Path.GetDirectoryName(outputPath)!);
if (entry.DataStream is not null)
{
using var outputFile = File.Create(outputPath);
entry.DataStream.CopyTo(outputFile);
}
}
文章撰寫中...請稍後...
參考資料:
Microsoft.Learn - System.Formats.Tar Namespace
GitHub - [API Proposal]: APIs to support tar archives #65951
留言
張貼留言
如果有任何問題、建議、想說的話或文章題目推薦,都歡迎留言或來信: a@ruyut.com