Bonjour à tous,

Alors voilà, j'ai fais un suivi de progression de l'extraction d'un fichier zip, progression total et progression par fichier contenu dans le zip. Mais je voudrais afficher la valeur en pourcentage, car pour l'instant l'une de mes progressBar à comme maximum la taille totale du fichier décompressé.

Voici mon code : (Il s'exécute quand je clic sur un bouton, la fenêtre ProgressDialog s'ouvre à ce moment et le worker commence)

À mon avis il y a moyen de refactorer tout ça (je débute en C#)

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
 
using Ionic.Zip;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace Flipbook
{
    public partial class ProgressDialog : Form
    {
        private string zipFile;
        private string output;
 
 
        int fileCount = 0;
        long totalSize = 0, total = 0, lastVal = 0, sum = 0;
 
        public ProgressDialog(string zip, string output)
        {
            this.zipFile = zip;
            this.output = output;
            InitializeComponent();
 
            workerZip.RunWorkerAsync();
        }
 
        // Total progression
        private void TotalExtractProgress(object sender, ExtractProgressEventArgs e)
        {
            if (total != e.TotalBytesToTransfer)
            {
                sum += total - lastVal + e.BytesTransferred;
                total = e.TotalBytesToTransfer;
            }
            else
            {
                sum += e.BytesTransferred - lastVal;
            }
            lastVal = e.BytesTransferred;
 
 
 
            Console.WriteLine(lastVal);
 
            this.Invoke(new MethodInvoker(delegate { progressBarTotal.Value = (Int32)sum; }));
        }
 
        // File progression
        public void FileExtractProgress(object sender, ExtractProgressEventArgs e)
        {
            if (e.EventType == ZipProgressEventType.Extracting_EntryBytesWritten)
            {
 
                //Console.Write("   {0}/{1} ({2:N0}%)", e.BytesTransferred, e.TotalBytesToTransfer, e.BytesTransferred / (0.01 * e.TotalBytesToTransfer));
 
                workerZip.ReportProgress(Convert.ToInt32(e.BytesTransferred / (0.01 * e.TotalBytesToTransfer)));
            }
            else if (e.EventType == ZipProgressEventType.Extracting_BeforeExtractEntry)
            {
                this.Invoke(new MethodInvoker(delegate { lblFile.Text = e.CurrentEntry.FileName; }));
            }
        }
 
        private void workerZip_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBarCurrentFile.Value = e.ProgressPercentage;
        }
 
        private void workerZip_DoWork(object sender, DoWorkEventArgs e)
        {
            Directory.CreateDirectory(output + "test");
 
            try
            {
                fileCount = 0;
                using(ZipFile zip = ZipFile.Read(zipFile))
                {
                    foreach(var entry in zip)
                    {
                        totalSize += entry.UncompressedSize;
                    }
 
                    this.Invoke(new MethodInvoker(delegate { progressBarTotal.Maximum = (Int32)totalSize; }));
                    zip.ExtractProgress += new EventHandler<ExtractProgressEventArgs>(FileExtractProgress);
                    zip.ExtractProgress += new EventHandler<ExtractProgressEventArgs>(TotalExtractProgress);
 
                    zip.ExtractAll(output + "test", ExtractExistingFileAction.OverwriteSilently);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
 
 
        private void workerZip_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //this.Close();
        }
    }
}

Merci d'avance.