.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,string txtFileName)
{
//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

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

posted @ 2021-12-10 11:18  順萁洎嘫  阅读(171)  评论(0编辑  收藏  举报