Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
File Comparer - CompareFilesForm.vb

CompareFilesForm.vb

Caricato da: Totem
Scarica il programma completo

  1. Public Class CompareFilesForm
  2.     Private Structure CompareResult
  3.         Public Message As String
  4.         Public Graph As Image
  5.     End Structure
  6.  
  7.     Private Colors As New List(Of Color)
  8.  
  9.     Private Sub CreateColors()
  10.         Dim R, G, B As Int16
  11.  
  12.         R = 0
  13.         G = 0
  14.         B = 0
  15.  
  16.         R = 255
  17.         For G = 0 To 255
  18.             Colors.Add(Color.FromArgb(255, R, G, B))
  19.         Next
  20.  
  21.         G = 255
  22.         For R = 255 To 1 Step -1
  23.             Colors.Add(Color.FromArgb(255, R, G, B))
  24.         Next
  25.  
  26.         G = 255
  27.         For B = 0 To 255
  28.             Colors.Add(Color.FromArgb(255, R, G, B))
  29.         Next
  30.  
  31.         B = 255
  32.         For G = 255 To 1 Step -1
  33.             Colors.Add(Color.FromArgb(255, R, G, B))
  34.         Next
  35.     End Sub
  36.  
  37.     Private Function CreateLegend() As Image
  38.         Dim Result As New Bitmap(Colors.Count, 21)
  39.         Dim G As Graphics = Graphics.FromImage(Result)
  40.  
  41.         For I As Int16 = 0 To Colors.Count - 1
  42.             G.DrawLine(New Pen(Colors(I)), I, 0, I, 21)
  43.         Next
  44.  
  45.         Return Result
  46.     End Function
  47.  
  48.     Sub New()
  49.         Me.InitializeComponent()
  50.         CreateColors()
  51.         imgLegend.Image = CreateLegend()
  52.     End Sub
  53.  
  54.     Private Sub btnFirstBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirstBrowse.Click, btnSecondBrowse.Click
  55.         Dim Open As New OpenFileDialog
  56.         Open.Filter = "Tutti i file|*.*"
  57.         If Open.ShowDialog = Windows.Forms.DialogResult.OK Then
  58.             If sender Is btnFirstBrowse Then
  59.                 txtFirst.Text = Open.FileName
  60.             Else
  61.                 txtSecond.Text = Open.FileName
  62.             End If
  63.         End If
  64.     End Sub
  65.  
  66.     Private Sub btnCompare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompare.Click
  67.         If btnCompare.Text = "Compara" Then
  68.             If (Not IO.File.Exists(txtFirst.Text)) Or (Not IO.File.Exists(txtSecond.Text)) Then
  69.                 MessageBox.Show("Inserire due file validi ed esistenti!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  70.                 Exit Sub
  71.             End If
  72.  
  73.             btnCompare.Text = "Ferma"
  74.             btnFirstBrowse.Enabled = False
  75.             btnSecondBrowse.Enabled = False
  76.  
  77.             Dim Files() As String
  78.             Files = New String() {txtFirst.Text, txtSecond.Text, nudBlockSize.Value}
  79.  
  80.             bgCompare.RunWorkerAsync(Files)
  81.         Else
  82.             bgCompare.CancelAsync()
  83.             btnCompare.Text = "Compara"
  84.             btnSecondBrowse.Enabled = True
  85.             btnFirstBrowse.Enabled = True
  86.         End If
  87.     End Sub
  88.  
  89.     Private Sub bgCompare_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgCompare.DoWork
  90.         Dim BiggerFile As String
  91.         Dim SmallerFile As String
  92.  
  93.         If FileLen(e.Argument(0)) > FileLen(e.Argument(1)) Then
  94.             BiggerFile = e.Argument(0)
  95.             SmallerFile = e.Argument(1)
  96.         Else
  97.             BiggerFile = e.Argument(1)
  98.             SmallerFile = e.Argument(0)
  99.         End If
  100.  
  101.         Dim Reader1 As New IO.FileStream(BiggerFile, IO.FileMode.Open)
  102.         Dim Reader2 As New IO.FileStream(SmallerFile, IO.FileMode.Open)
  103.         Dim B1, B2 As Byte
  104.         Dim SameBytes, DifferentBytes As Int32
  105.         Dim BlockSameBytes, BlockDifferentBytes As Int32
  106.         Dim Percentages As New List(Of Single)
  107.         Dim BlockSize As Int32 = CInt(e.Argument(2))
  108.  
  109.         Do While (Reader2.Position < Reader2.Length - 1)
  110.             B1 = Reader1.ReadByte
  111.             B2 = Reader2.ReadByte
  112.             If B1 = B2 Then
  113.                 SameBytes += 1
  114.                 BlockSameBytes += 1
  115.             Else
  116.                 DifferentBytes += 1
  117.                 BlockDifferentBytes += 1
  118.             End If
  119.             If Reader1.Position Mod BlockSize = 0 Then
  120.                 Percentages.Add(100 * BlockSameBytes / (BlockSameBytes + BlockDifferentBytes))
  121.                 BlockSameBytes = 0
  122.                 BlockDifferentBytes = 0
  123.             End If
  124.             bgCompare.ReportProgress(100 * Reader2.Position / Reader2.Length)
  125.         Loop
  126.  
  127.         If Reader2.Length < Reader1.Length Then
  128.             For I As Int16 = 1 To Math.Ceiling((Reader1.Length - Reader2.Length) / BlockSize)
  129.                 Percentages.Add(0)
  130.             Next
  131.         End If
  132.  
  133.         Reader1.Close()
  134.         Reader2.Close()
  135.  
  136.         Dim Graph As New Bitmap(Percentages.Count, 53)
  137.         Dim G As Graphics = Graphics.FromImage(Graph)
  138.  
  139.         For I As Int32 = 0 To Percentages.Count - 1
  140.             G.DrawLine(New Pen(Colors((Colors.Count - 1) * Percentages(I) / 100)), I, 0, I, 53)
  141.         Next
  142.  
  143.         Dim Result As New CompareResult
  144.         Result.Message = String.Format("Risultato: {0:N2}% dei file uguali", 100 * SameBytes / (SameBytes + DifferentBytes))
  145.         Result.Graph = Graph
  146.         e.Result = Result
  147.     End Sub
  148.  
  149.     Private Sub bgCompare_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgCompare.ProgressChanged
  150.         prgProgress.Value = e.ProgressPercentage
  151.     End Sub
  152.  
  153.     Private Sub bgCompare_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgCompare.RunWorkerCompleted
  154.         If e.Cancelled Then
  155.             MessageBox.Show("Comparazione annullata!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  156.         Else
  157.             Dim Result As CompareResult = e.Result
  158.             lblResult.Text = Result.Message
  159.             imgCompared.Image = Result.Graph
  160.             MessageBox.Show("Comparazione completata!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
  161.         End If
  162.  
  163.         btnCompare.Text = "Compara"
  164.         btnSecondBrowse.Enabled = True
  165.         btnFirstBrowse.Enabled = True
  166.     End Sub
  167.  
  168.     Private Sub imgCompared_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imgCompared.Click
  169.         Dim Save As New SaveFileDialog
  170.         Save.Filter = "File immagine|*.png;*.jpg;*.gif;*.bmp;*.wmf"
  171.         If Save.ShowDialog = Windows.Forms.DialogResult.OK Then
  172.             imgCompared.Image.Save(Save.FileName)
  173.         End If
  174.     End Sub
  175. End Class