I am not able to extract a zip file into a folder

3

The code is giving this error:

  

A first chance exception of type 'Ionic.Zip.ZipException' occurred in Ionic.Zip.dll
  Additional information: Can not read that as a ZipFile

     

If there is a handler for this exception, the program may be continued.

Code:

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 Ionic.Zip;
    using System.Net;

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

            private void Form1_Load(object sender, System.EventArgs e)
            {

            }

            private void button1_Click(object sender, System.EventArgs e)
            {
                string pastaJogo = @"C:\Game\Play.exe";
                System.Diagnostics.Process.Start(pastaJogo);
                MessageBox.Show("Espere Um Pouco");
                Environment.Exit(0);
            }

            public void AUTOALIZAR_Click(object sender, EventArgs e)
            {
                WebClient wc = new WebClient();
                string fileSave = @"C:\Game\Play.zip";
                string zipFileSave = @"C:\Game\Play.zip";
                string zipFileSaveExtract = @"C:\Game";
                string downloadUrl = "https://www.dropbox.com/s/sq8s2bnyj5fqacv/plsay.zip?dl=1";
                MessageBox.Show("Esta Autoalizando...");
                wc.DownloadFileAsync(new Uri(downloadUrl), fileSave);
                wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(c_DownloadProgress);
                ZipFile zip = ZipFile.Read(zipFileSave);
                zip.ExtractAll(zipFileSaveExtract);
                wc.DownloadFileCompleted += new AsyncCompletedEventHandler(c_DownloadFileCompleted);

            } 

            public void c_DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
            {
                int bytesin = int.Parse(e.BytesReceived.ToString());
                int totalbyte = int.Parse(e.TotalBytesToReceive.ToString());
                int kb1 = bytesin / 1024;
                int kb2 = totalbyte / 1024;

                progressBar1.Value = e.ProgressPercentage;
                label1.Text = kb1.ToString() + "KB Faltam " + kb2.ToString() + "KB " + e.ProgressPercentage.ToString() + "%";
            }

            public void c_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
            {
                MessageBox.Show("Complete");
            }

            private void Exit_Click(object sender, EventArgs e)
            {
                Environment.Exit(0);
            }
        }
    }
    
asked by anonymous 26.04.2015 / 17:21

1 answer

3

You do not need to use Ionic.Zip to perform an extraction. .NET itself already supports this:

        public void AUTOALIZAR_Click(object sender, EventArgs e)
        {
            var wc = new WebClient();
            string fileSave = @"C:\Game\Play.zip";
            string zipFileSave = @"C:\Game\Play.zip";
            string zipFileSaveExtract = @"C:\Game";
            string downloadUrl = "https://www.dropbox.com/s/sq8s2bnyj5fqacv/plsay.zip?dl=1";
            MessageBox.Show("Esta Autoalizando...");
            wc.DownloadFileAsync(new Uri(downloadUrl), fileSave);
            wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(c_DownloadProgress);

            System.IO.Compression.ZipFile.CreateFromDirectory(zipFileSaveExtract, zipFileSave);
            System.IO.Compression.ZipFile.ExtractToDirectory(zipFileSaveExtract, zipFileSaveExtract);

            wc.DownloadFileCompleted += new AsyncCompletedEventHandler(c_DownloadFileCompleted);

        } 

You can see more details here . ZipFile is in another namespace .

Another detail is that apparently this code will not work, because DownloadFileAsync occurs asynchronously, which can cause the extraction to occur with a still empty or incomplete file. In its place, I would move the extraction event from Click to Completed , ie:

        private string fileSave = @"C:\Game\Play.zip";
        private string zipFileSave = @"C:\Game\Play.zip";
        private string zipFileSaveExtract = @"C:\Game";
        private string downloadUrl = "https://www.dropbox.com/s/sq8s2bnyj5fqacv/plsay.zip?dl=1";

        public void AUTOALIZAR_Click(object sender, EventArgs e)
        {
            var wc = new WebClient();

            MessageBox.Show("Esta Autoalizando...");
            wc.DownloadFileAsync(new Uri(downloadUrl), fileSave);
            wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(c_DownloadProgress);

            wc.DownloadFileCompleted += new AsyncCompletedEventHandler(c_DownloadFileCompleted);

        } 

        public void c_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            System.IO.Compression.ZipFile.CreateFromDirectory(zipFileSaveExtract, zipFileSave);
            System.IO.Compression.ZipFile.ExtractToDirectory(zipFileSaveExtract, zipFileSaveExtract);

            MessageBox.Show("Complete");
        }
    
26.04.2015 / 21:03