I am unable to extract a zip file to a folder

Asked

Viewed 505 times

3

The code is giving this error:

A first chance Exception of type 'Ionic.zip.Zipexception' occurred in Ionic.zip.dll
Additional information: Cannot read that as a Zipfile

If there is a Handler for this Exception, the program may be Safely 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);
            }
        }
    }

1 answer

3


You don’t need to use Ionic.Zip to perform an extraction. The . NET itself already has support for 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 file still empty or incomplete. In its place, I would move the extraction event from the Click to the Completed, that is to say:

        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");
        }
  • Thank you, it’s working, but is it a normal mistake? error: Error 1 The type or namespace name 'Zipfile' does not exist in the namespace 'System.IO.Compression' (are you Missing an Assembly Reference?)

  • 1

    See here the specification of ZipFile. Maybe we missed some using.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.