是否可以使用System。IO.File.ReadAllLines在压缩的txt文件上而不提取它?
我找到了一些解决方案,包括提取和删除文件,但我真的更喜欢避免这种解决方案。
这里有一个控制台应用程序,它应该为您指明方向:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;
namespace ZipExploration
{
class Program
{
static void Main(string[] args)
{
getFileList(@"C:errorsmyfiles.zip");
Console.ReadLine();
}
static List<string> getFileList(string zip)
{
List<string> retVal = new List<string>();
try
{
if (!File.Exists(zip))
{
Console.WriteLine(@"File does not exist");
}
//FileInfo fi = new FileInfo(zip);
//using (MemoryStream ms = new MemoryStream())
using( FileStream ms = File.Create(@"C:Errorsmyfile.txt"))
{
using (FileStream fs = new FileStream(zip,FileMode.Open))
{
using (ZipArchive arc = new ZipArchive(fs,ZipArchiveMode.Read))
{
foreach (ZipArchiveEntry e in arc.Entries)
{
retVal.Add(e.FullName);
Console.WriteLine(e.FullName);
getContents(e);
}
}
}
ms.Close();
//byte[] buffer = ms.ToArray();
//Console.WriteLine(buffer.Length);
//Console.WriteLine(System.Text.Encoding.UTF8.GetString(buffer));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return retVal;
}
static void getContents(ZipArchiveEntry e)
{
using (StreamReader stm = new StreamReader( e.Open()))
{
Console.WriteLine(stm.ReadToEnd());
}
}
}
}
您可以使用BoxedApp SDK创建一个虚拟目录,并将zip文件的内容提取到该目录。磁盘上不会写入任何文件。还有NI.Vfs库,但我没有使用过。