C# Unzip class implementation - SharpZipBaseException occurs but not sure why

Whenever I used the attached class, I get a SharpZipBaseException (Unexpected EOF). The exception is thrown at the following line:

while ((theEntry = s.GetNextEntry()) != null)


Can someone please tell me what I am doing wrong? By the way, the calling code looks like this:

Unzipper unzipper = new Unzipper();
unzipper.unZip(textBox1.Text, "testV1");

-----------------------------------
 private void openBtn1_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = this.openFileDialog1.FileName;
            }
        }
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Windows.Forms;
 
 
namespace MyNamespace
{
    //NEW
    //public delegate void ValueChangedDelegate(string _newLabelValue);
 
    class Unzipper
    {
        //New
        //public event ValueChangedDelegate ValueChangedEvent;
 
        public Unzipper()
        {
        }
 
        public void unZip(string zipFileName, string targetFolder)
        {
 
 
            string TargetDirRoot = "c://" + targetFolder + "//"; //directory to unzip stuff in 
            string TargetDir = ""; //This is for a folder that you will create to unzip everything into (extra level)
            if (Directory.Exists(TargetDir))
            {
                //TODO: error path already exists
            }
            else
            {
                Directory.CreateDirectory(TargetDirRoot);
            }
 
            using (ZipInputStream s = new ZipInputStream(File.OpenRead(@zipFileName)))
            {
 
                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
 
                    Console.WriteLine(theEntry.Name);
 
                    //NEW - Here an event is raised.
                    //ValueChangedEvent(theEntry.Name);
                    //Application.DoEvents();
 
                    string directoryName = Path.GetDirectoryName(theEntry.Name);
                    string fileName = Path.GetFileName(theEntry.Name);
 
                    // create directory
                    if (directoryName.Length > 0)
                    {
                        TargetDir = TargetDirRoot + directoryName + "\\";
                        Directory.CreateDirectory(TargetDir);
                    }
 
                    if (fileName != String.Empty)
                    {
                        //using (FileStream streamWriter = File.Create(theEntry.Name))
                        using (FileStream streamWriter = File.Create(Path.Combine(TargetDir, fileName)))
                        {
 
 
                            int size = 2048;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                size = s.Read(data, 0, data.Length);
                                if (size > 0)
                                {
                                    streamWriter.Write(data, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                            streamWriter.Close();
                        }
 
                    }
                    //s.Close();
                }
                s.Close();
            }
 
 
        }
    }
}

Open in new window

LVL 1
jrwalker2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AgariciCommented:
for me your code worked fine...
i called it using
            Unzipper unz = new Unzipper();
            unz.unZip(@"D:\zipFiles\NewZipFile.zip", @"Folder");


it extracted the zip contents correctly.
maybe the zip file you are passing (as first param) is not actually a zip file. it should be a valid zip file. pls try to open it with winzip and see it's beeing opened and extracted correctly.

hth,
A.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Expert1701Commented:
*NOT FOR POINTS*
I suspect that Agarici is correct, and that the zip file you are trying to extract from is invalid.

Can you give this code a try?
  public void unZip(string zipFileName, string targetFolder)
  {
    if (new System.IO.FileInfo(zipFileName).Length == 0)
    {
      MessageBox.Show("Zip file [" + System.IO.Path.GetFullPath(zipFileName) + "] has no data");
    }
    else
    {
      string TargetDirRoot = "c://" + targetFolder + "//"; //directory to unzip stuff in 
      using (ZipInputStream s = new ZipInputStream(File.OpenRead(@zipFileName)))
      {
        ZipEntry theEntry;
        while ((theEntry = s.GetNextEntry()) != null)
        {
          string directoryName = Path.GetDirectoryName(theEntry.Name);
          string fileName = Path.GetFileName(theEntry.Name);
 
          string TargetDir;
          if (directoryName.Length == 0)
            TargetDir = TargetDirRoot;
          else
          {
            TargetDir = Path.Combine(TargetDirRoot, directoryName);
            Directory.CreateDirectory(TargetDir);
          }
 
          if (fileName != String.Empty)
          {
            using (FileStream streamWriter = File.Create(Path.Combine(TargetDir, fileName)))
            {
              int size = 0;
              byte[] data = new byte[2048];
              while (size > 0)
              {
                size = s.Read(data, 0, data.Length);
                streamWriter.Write(data, 0, size);
              }
            }
          }
        }
      }
    }
  }

Open in new window

0
Expert1701Commented:
Sorry, one change, the function should be as follows:
  public void unZip(string zipFileName, string targetFolder)
  {
    if (new FileInfo(zipFileName).Length == 0)
    {
      MessageBox.Show("Zip file [" + System.IO.Path.GetFullPath(zipFileName) + "] has no data");
    }
    else
    {
      string TargetDirRoot = "c://" + targetFolder + "//"; //directory to unzip stuff in 
 
      if (!Directory.Exists(TargetDirRoot))
        Directory.CreateDirectory(TargetDirRoot);
 
      using (ZipInputStream s = new ZipInputStream(File.OpenRead(@zipFileName)))
      {
        ZipEntry theEntry;
        while ((theEntry = s.GetNextEntry()) != null)
        {
          string directoryName = Path.GetDirectoryName(theEntry.Name);
          string fileName = Path.GetFileName(theEntry.Name);
 
          string TargetDir = Path.Combine(TargetDirRoot, directoryName);
 
          if (!Directory.Exists(TargetDir))
            Directory.CreateDirectory(TargetDir);
 
          if (fileName != String.Empty)
          {
            using (FileStream streamWriter = File.Create(Path.Combine(TargetDir, fileName)))
            {
              int size = 0;
              byte[] data = new byte[2048];
              while (size > 0)
              {
                size = s.Read(data, 0, data.Length);
                streamWriter.Write(data, 0, size);
              }
            }
          }
        }
      }
    }
  }

Open in new window

0
jrwalker2Author Commented:
Thanks guys.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.