Cannot unzip same file twice?

697 views Asked by At

I have a "work list style" application where the user selects a zip file from a list, then clicks a button to unzip it to a local folder.

Here is an extract from the code used:

ZipFile zip = Ionic.Zip.ZipFile.Read(sourcePackage);
zip.ExtractAll(destination);
zip.Dispose();

Everything works fine the first time but if the user tries to unzip the same file again (even after unzipping a few others), it goes too quick and all that is created in the destination folder is what looks like a temp file (e.g. 'x2hiex0z.pj0').

It's as if Ionic.Zip.ZipFile.Read is creating a cache of previously unzipped file names.

If so, how do I clear it so that I can force it to unzip the file again?

1

There are 1 answers

4
Flocke On

I gues you get some kind of a "File Exist"-Exception

Try:

OpenFileDialog open = new OpenFileDialog();
open.Filter = "zip Datei (.zip)|*.zip";
open.RestoreDirectory = true;

if (open.ShowDialog() == DialogResult.OK)
{
  try
  {
    ZipFile zip = Ionic.Zip.ZipFile.Read(open.FileName);
    zip.ExtractAll(".\\");
    zip.Dispose();
  }
  catch (ZipException zex)
  {
    MessageBox.Show(zex.Message);
  }
}

or with a Thread:

private void open()
{
    OpenFileDialog open = new OpenFileDialog();
    open.Filter = "zip Datei (.zip)|*.zip";
    open.RestoreDirectory = true;

    if (open.ShowDialog() == DialogResult.OK)
    {

            Thread t1 = new Thread
            (delegate()
            {
                try
                {
                    using (ZipFile zip = Ionic.Zip.ZipFile.Read(open.FileName))
                    {
                        zip.ExtractProgress += zip_ExtractProgress;
                        zip.ExtractAll(".\\", ExtractExistingFileAction.OverwriteSilently);
                    }
                }
                catch (ZipException zex)
                {
                    error(zex.Message);
                }
             });
            t1.IsBackground = true;
            t1.Start();

    }
}


private void zip_ExtractProgress(object sender, ExtractProgressEventArgs args)
{
     update(args.TotalBytesToTransfer, args.BytesTransferred);
}



private void update(long ueTotal, long done)
{

    if (this.InvokeRequired)
    {
        this.Invoke(new MethodInvoker(() => { update(ueTotal, done); }));
    }
    else
    {
        if (ueTotal > 0)
        {
            double prz = (100d / ueTotal) * done;
            lblProz.Text = prz.ToString("###.##");
        }
    }
}