如何在不解压缩 zip 的情况下将 txt 文件(在 zip 文件中)放入字符串中?
How can I put a txt file (that is in a zip file) in a string, without extracting the zip?
这是我的代码:
using (ZipArchive zip = ZipFile.Open(path, ZipArchiveMode.Read))
foreach (ZipArchiveEntry entry in zip.Entries)
if (entry.Name == "example.txt")
entry.ExtractToFile(???);
那个???是我遇到的麻烦。我希望它转到 string
,而不是磁盘上的文件。
第一个代码将为您提供一个字符串数组,就好像它是“ReadAllLines”一样。第二个会给你一个字符串(就好像它是“ReadAllText”)。
List<string> lines = new List<string>();
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
foreach (ZipArchiveEntry entry in archive.Entries.Where(e_ => e_.Name == "example.txt"))
{
Stream stream = entry.Open();
using (var sr = new StreamReader(stream, Encoding.UTF8))
{
string line;
while ((line = sr.ReadLine()) != null)
{
lines.Add(line);
}
}
}
}
string[] result = lines.ToArray();
string result = "";
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
foreach (ZipArchiveEntry entry in archive.Entries.Where(e_ => e_.Name == "example.txt"))
{
Stream stream = entry.Open();
using (var sr = new StreamReader(stream, Encoding.UTF8))
{
result = sr.ReadToEnd();
}
}
}
这是我的代码:
using (ZipArchive zip = ZipFile.Open(path, ZipArchiveMode.Read))
foreach (ZipArchiveEntry entry in zip.Entries)
if (entry.Name == "example.txt")
entry.ExtractToFile(???);
那个???是我遇到的麻烦。我希望它转到 string
,而不是磁盘上的文件。
第一个代码将为您提供一个字符串数组,就好像它是“ReadAllLines”一样。第二个会给你一个字符串(就好像它是“ReadAllText”)。
List<string> lines = new List<string>();
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
foreach (ZipArchiveEntry entry in archive.Entries.Where(e_ => e_.Name == "example.txt"))
{
Stream stream = entry.Open();
using (var sr = new StreamReader(stream, Encoding.UTF8))
{
string line;
while ((line = sr.ReadLine()) != null)
{
lines.Add(line);
}
}
}
}
string[] result = lines.ToArray();
string result = "";
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
foreach (ZipArchiveEntry entry in archive.Entries.Where(e_ => e_.Name == "example.txt"))
{
Stream stream = entry.Open();
using (var sr = new StreamReader(stream, Encoding.UTF8))
{
result = sr.ReadToEnd();
}
}
}