none
Read Contents of ZipArchiveEntry from System.IO.Compression RRS feed

  • Question

  • How do you read the contents of a zipped file using ZipArchiveEntry in the new System.IO.Compression namespace?

    I want to read the contents ( i.e. string, byte[], etc. ) without extracting to an actual file.

    Any help is appreciated.

    Tuesday, September 11, 2012 2:53 AM

Answers

  • Use ZipArchiveEntry.Open method to get a stream:

                using (var zipStream = new FileStream("some.zip", FileMode.Open))
                using (var archive = new ZipArchive(zipStream, ZipArchiveMode.Read)) {
                    foreach (var entry in archive.Entries) {
                        using (var stream = entry.Open())
                        using (var reader = new StreamReader(stream)) {
                            Console.WriteLine(reader.ReadToEnd());
                        }
                    }
                }
    

    • Marked as answer by shaggygi Tuesday, September 11, 2012 12:11 PM
    Tuesday, September 11, 2012 3:56 AM
    Moderator

All replies

  • Use ZipArchiveEntry.Open method to get a stream:

                using (var zipStream = new FileStream("some.zip", FileMode.Open))
                using (var archive = new ZipArchive(zipStream, ZipArchiveMode.Read)) {
                    foreach (var entry in archive.Entries) {
                        using (var stream = entry.Open())
                        using (var reader = new StreamReader(stream)) {
                            Console.WriteLine(reader.ReadToEnd());
                        }
                    }
                }
    

    • Marked as answer by shaggygi Tuesday, September 11, 2012 12:11 PM
    Tuesday, September 11, 2012 3:56 AM
    Moderator
  • Hi,

    You can read entry within zip file without extracting it.

    Below is sample code which just read file fullname from zip.

    using System;
    using System.IO;
    using System.IO.Compression;
    namespace MyZipArchieveExample
    {
        class Program
        {
            static void Main(string[] args)
            {
                string zipPath = @"c:\example\sample.zip";
                using (ZipArchive archive = ZipFile.OpenRead(zipPath))
                {
                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
    		    var entryfilename=entry.FullName;
                    }
                } 
            }
        }
    }

    and to read entire content you can use

     using (var zipStream = new FileStream("sample.zip", FileMode.Open))
                using (var archive = new ZipArchive(zipStream, ZipArchiveMode.Read)) {
                    foreach (var entry in archive.Entries) {
                        using (var stream = entry.Open())
                        using (var reader = new StreamReader(stream)) {
                            Console.WriteLine(reader.ReadToEnd());
                        }
                    }
                }
    Hope, this will help you out. Feel free to contact in case of query.

    If this post answers your question, please click Mark As Answer. If this post is helpful please click Mark as Helpful.


    • Edited by Tejas T Tuesday, September 11, 2012 4:30 AM
    Tuesday, September 11, 2012 3:57 AM
  • worked perfectly for me for core 2. working on zip -> insert for DB. From here im using a bulk import here: https://weblogs.sqlteam.com/mladenp/archive/2006/08/26/11368.aspx
    Wednesday, October 10, 2018 12:56 PM