1. 程式人生 > 其它 >.net5 提取壓縮包內指定檔案內容無需解壓

.net5 提取壓縮包內指定檔案內容無需解壓

using System.IO;
using System;
using System.IO.Compression;

/// <summary>
///提取壓縮包內指定文字內容,無需解壓
/// </summary>
/// <param name="fileName">壓縮檔案路徑</param>

/// <param name="modelDefectFileName">txt檔名</param>
/// <returns></returns>
public static string GetZipTxt(string fileName,stringtxtFileName)
{
//var fileName = @$"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\123.zip";
string content = string.Empty;
using (FileStream zipToOpen = new FileStream(fileName, FileMode.Open))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Read))
{
ZipArchiveEntry readmeEntry = archive.GetEntry(txtFileName);
using (var reader = new StreamReader(readmeEntry.Open()))
{
content = reader.ReadLine();
Console.WriteLine(content);
}
}
}
return content;
}

================================

參考網址:

https://docs.microsoft.com/zh-cn/dotnet/standard/io/how-to-compress-and-extract-files

==========================