Create Zip files and unzip using . NET 3.5

Asked

Viewed 60 times

-1

I’m making an online installer for my Github program, I made a Form with some basic controls.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.IO.Compression;
using System.Reflection;

namespace IEIPInstaller
{
    public partial class Main : Form
    {
        private string temp = Path.Combine(Path.GetTempPath(), "IEIPInst");
        private string localProg = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

        public Main()
        {
            if (Directory.Exists(temp))
            {
                if(File.Exists(temp + "\\IEIPv0.2-alpha-Windows.zip"))
                {
                    File.Delete(temp + "\\IEIPv0.2-alpha-Windows.zip");
                }
                Directory.Delete(temp);
            }
            if(File.Exists(localProg + "\\IEIPv0.2-alpha-Windows.zip"))
            {
                MessageBox.Show("Installer cannot be initialized because you have IEIP with this program.", "IEIPInst", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(0);
            }
            else
            {
                InitializeComponent();
                Directory.CreateDirectory(temp);
            }
        }

        private void btnNo_Click(object sender, EventArgs e)
        {
            Directory.Delete(temp, true);
            Application.Exit();
        }

        private void btnYes_Click(object sender, EventArgs e)
        {
            btnYes.Enabled = false;
            btnNo.Enabled = false;
            lblWant.Text = "";
            Complete.Text = "Status: Connecting...";
            startDownload();
        }

        private void startDownload()
        {
                WebClient client = new WebClient();
            
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
                client.DownloadFileAsync(new Uri("https://github.com/JPPlaysGamer/IndustriesExes.Inc/releases/download/ieip-pre2/IEIPv0.2-alpha-Windows.zip"), temp + "\\IEIPv0.2-alpha-Windows.zip");

                

        }
        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            this.BeginInvoke((MethodInvoker)delegate {
                double bytesIn = double.Parse(e.BytesReceived.ToString());
                double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
                double percentage = bytesIn / totalBytes * 100;
                Mbs.Text = (bytesIn / 1024 / 1024).ToString("F2") + " / " + (totalBytes / 1024/ 1024).ToString("F2") + " Mb";
                Complete.Text = "Status: Downloading...";
                IEIPProgress.Value = int.Parse(Math.Truncate(percentage).ToString());
                this.Text = "IEIP Installer - " + IEIPProgress.Value + "%";
                
            });
        }
        void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            this.BeginInvoke((MethodInvoker)delegate {
                if(File.Exists(temp + "\\IEIPv0.2-alpha-Windows.zip"))
                {
                    File.Move(temp + "\\IEIPv0.2-alpha-Windows.zip",  localProg + "\\IEIPv0.2-alpha-Windows.zip");
                }
                
                Complete.Text = "Status: Completed";
                
                btnNo.Text = "Exit";
                btnNo.Enabled = true;
                
            });
        }

    }
}

In JIT:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

This is the structure of this Form and in the client_DownloadFileCompleted function wanted to make you unzip the zip file that shows in the code. But as I am using . NET 3.5 (version embedded in Windows, I think) does not have the class System.IO.Compression.Zipfile and works only on . recent NET. But there’s the Gzipstream class that can handle Zip. Is there any way to zip and unzip files with this class or with some Nuget library (or Github)?

  • according to the documentation, there is no version 3.5: https://docs.microsoft.com/en-us/dotnet/api/system.io.compression.zipfile?view=net-5.0 can use another package for this, a fez q zip is a standard format, for example https://github.com/iccode/SharpZipLib

  • This Sharpziplib can work in this version?

  • look there in the link releases to answer this question :)

  • I have my Visualstudio open and I found the Nuget Pack, and there is no need to test this package

  • Unable to use Sharpziplib :(

1 answer

0


Good afternoon, You can use the library Ionic.Zip which is compatible with . NET Framework 3.5, but has been discontinued. I have successfully used it in old projects.

The documentation is here.

Follow a simple example:

class IonicZip
{
    private string _origem;
    private string _destino;

    public string origem
    {
        set { _origem = value; }
    }
    public string destino
    {
        set { _destino = value; }
    }

    public void Compactar()
    {
        if (File.Exists(_destino))
            File.Delete(_destino);
        ZipFile arquivoZip = new ZipFile(_destino);
        arquivoZip.AddDirectory(@_origem);
        arquivoZip.Save();
        arquivoZip.Dispose();
    }

    public void Descompactar()
    {
        ZipFile arquivoZip = ZipFile.Read(_origem);
        try
        {
            foreach (ZipEntry e in arquivoZip)
            {
                e.Extract(_destino, true);
            }
            arquivoZip.Dispose();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
  • Thanks for the suggestion, now I installed in my Visual Studio project and is working.

  • @Joãopaulo, if you have otherwise solved inform here and close your question, it saves users from wasting time trying to help you. Another detail, clear my answer as "useless" because I answered in the context of your question!

  • I marked as useful this answer but as I have no reputation greater or equal to 15 gets like this. I will solve and try.

Browser other questions tagged

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