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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
AgariciConnect With a Mentor Commented:
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
 
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
 
Expert1701Connect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.