JHarley1
JHarley1

Reputation: 2112

C# Listing ZIP file contents - using ZipArchive Class

I would like to obtain a list of contents in a ZIP file using the 'ZipArchive' class.

I am using an example from MSDN:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Compression;
using System.IO.Compression.dll;

namespace Zip_Extractor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (FileStream zipToOpen = new FileStream(@"c:\users\exampleuser\release.zip", FileMode.Open))
            {
                using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                {
                    ZipArchiveEntry readmeEntry = archive.CreateEntry("Readme.txt");
                    using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
                    {
                            writer.WriteLine("Information about this package.");
                            writer.WriteLine("========================");
                    }
                }
            }
        }
    }
}

However, I get the error

'Namespace for 'ZipArchive could not be found'.

I can confirm the 'Target framework' for the application is .NET Framework 4.5.

Can anyone point me in the right direction?

Upvotes: 3

Views: 5991

Answers (4)

Yusubov
Yusubov

Reputation: 5843

I had the same problem and looked for these namespaces in Assemblies -> Framework:
Just add the namespaces: System.IO.Compression & System.IO.Compression.FileSystem.

Here is the screen shot to make understanding/things easier:

enter image description here

Upvotes: 0

pStan
pStan

Reputation: 1104

I'm late on this but it may help someone...

Add references to the files mentioned in other answers:

  • System.IO.Compression
  • System.IO.Compression.FileSystem

It probably will not work after adding those, so go into their properties and mark each ones "copy local" setting to "True".

Did the trick for me. :)

Upvotes: 0

Alex
Alex

Reputation: 699

Right click references, add a reference to framework assemblies System.IO.Compression and System.IO.Compression.FileSystem. Include using System.IO.Compression; and ensure you're using .NET Framework 4.5.

Upvotes: 0

Obama
Obama

Reputation: 2612

Add references to System.IO.Compression and System.IO.Compression.FileSystem.

Check David Anderson's blog: http://www.danderson.me/dotnet/zipfile-class-system-io-compression-filesystem/

Hope this Helps!

Upvotes: 3

Related Questions