-1

Is it possible to use System.IO.File.ReadAllLines on a zipped txt file without extracting it?

I found a few solutions that include extracting and deleting the file, but I would really prefer to avoid this solution.

mwfearnley
  • 3,303
  • 2
  • 34
  • 35
Ned
  • 355
  • 2
  • 9
  • 24
  • Short answer, You can't do that without extracting. but may be you can do it without creating a temp file. – Sriram Sakthivel Apr 27 '14 at 08:47
  • are you using 4 or 4.5? In 4.5 there are new classes that may help: ZipArchive - http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive.aspx. In earlier versions there is the ZipPackage: http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage%28v=vs.100%29.aspx – Gavin Apr 27 '14 at 09:37

2 Answers2

1

here is a console app that should point you on your way:

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());
        }
    }
}

}

Eugene Mayevski 'Callback
  • 45,135
  • 8
  • 71
  • 121
Gavin
  • 491
  • 3
  • 5
  • That involves a temporary file which the OP would like to avoid. – Eugene Mayevski 'Callback Apr 27 '14 at 11:57
  • sorry - I don't actually use the ms filestream - I should have removed that from my sample – Gavin Apr 27 '14 at 12:13
  • I didn't find any solution without extracting the file so i wrote a piece of code that extracts , reads and deletes the file/temp folder so therefore your solution was very useful. thanks. – Ned Apr 27 '14 at 12:55
0

You can do it with BoxedApp SDK to create a virtual directory and extract the contents of the zip file to that directory. No files would be written on disk. There is also NI.Vfs library, but I have not used it.

Harsha Alva
  • 394
  • 1
  • 3
  • 18