我可以从压缩的txt文件中读取AllAllLines而不提取吗?

时间:2014-04-27 08:39:25

标签: .net vb.net visual-studio zip dotnetzip

是否可以在压缩的txt文件上使用System.IO.File.ReadAllLines而不提取它?

我找到了一些解决方案,包括提取和删除文件,但我真的更愿意避免这种解决方案。

2 个答案:

答案 0 :(得分:0)

这是一个控制台应用程序,应该指明你的方式:

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:\errors\myfiles.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:\Errors\myfile.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());
        }
    }
}

}

答案 1 :(得分:0)

您可以使用BoxedApp SDK创建虚拟目录并将zip文件的内容解压缩到该目录。没有文件会写在磁盘上。还有NI.Vfs库,但我没有使用它。