-1

Estou fazendo um instalador online para meu programa do GitHub, eu fiz um Form com alguns controles básicos.

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;
                
            });
        }

    }
}

Em JIT:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Esse é a estrutura desse Form e na função client_DownloadFileCompleted queria fazer com que descompacta o arquivo zip que mostra no código. Mas como estou usando .NET 3.5 (versão embutida no Windows, eu acho) não tem a classe System.IO.Compression.ZipFile e funciona apenas em .NET recente. Mas tem a classe GZipStream que pode fazer manipulação do Zip. Existe alguma forma de zipar e descompactar arquivos com essa classe ou com alguma biblioteca do NuGet (ou GitHub)?

5

1 Resposta 1

0

Boa tarde, Você pode utilizar a biblioteca Ionic.Zip que é compatível com .NET Framework 3.5, porém a mesma foi descontinuada. Já utilizei com sucesso em projetos antigos.

A documentação está aqui.

Segue um simples exemplo:

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;
        }
    }
}
3
  • Obrigado pela sugestão, agora eu instalei no meu projeto do Visual Studio e está funcionando. 25/05/2021 às 18:23
  • @JoãoPaulo, se você resolveu de outra forma informe aqui e feche a sua pergunta, isso evita que usuários percam tempo tentando te ajudar. Outro detalhe, desmarque a minha resposta como "inútil" pois respondi no contexto da tua pergunta! 25/05/2021 às 19:04
  • Eu marquei como útil esta resposta mas como não tenho reputação maior ou igual a 15 fica assim. Vou resolver e tentar. 26/05/2021 às 12:15

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .