Advertisement
Guest User

Untitled

a guest
May 27th, 2015
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.36 KB | None | 0 0
  1. Public NotInheritable Class Form1
  2.  
  3.     ''' <summary>
  4.     ''' Handles the <see cref="FileSplitter"/> instance.
  5.     ''' </summary>
  6.     Private WithEvents splitter As New FileSplitter() With {.BufferSize = .BufferSize}
  7.  
  8.     ' Splitter settings.
  9.     Private ReadOnly workingDir As String = "C:\Test"
  10.     Private ReadOnly fileName As String = "File.mp3"
  11.     Private ReadOnly filePath As String = IO.Path.Combine(workingDir, fileName)
  12.     Private ReadOnly chunkName As String = "File.Part"
  13.     Private ReadOnly chunkExt As String = "fs"
  14.  
  15.     ' Merger settings.
  16.     Private ReadOnly firstChunkPath As String = String.Format("{0}\{1}.01.{2}", workingDir, chunkName, chunkExt)
  17.     Private ReadOnly targetFileName As String = String.Format("{0}\{1}.{2}", workingDir, "Merged", IO.Path.GetExtension(fileName))
  18.  
  19.     ' Some default chunk sizes to split a file.
  20.     Private ReadOnly kilobyte As Integer = 1024
  21.     Private ReadOnly megabyte As Integer = 1048576
  22.     Private ReadOnly gigabyte As Integer = 1073741824
  23.     Private ReadOnly halfFileSize As Integer = CInt(New IO.FileInfo(filePath).Length / 2)
  24.  
  25.     ' The label controls that will display the progress.
  26.     Private labelSplit1, labelSplit2, labelSplit3 As New Label
  27.     Private labelMerge1, labelMerge2, labelMerge3 As New Label
  28.  
  29.     ' The button controls to start a split or merge operation.
  30.     Private WithEvents buttonSplit, buttonMerge As New Button
  31.  
  32.     ''' <summary>
  33.     ''' Initializes a new instance of the <see cref="Form1"/> class.
  34.     ''' </summary>
  35.     Public Sub New()
  36.  
  37.         ' This call is required by the designer.
  38.         Me.InitializeComponent()
  39.  
  40.         ' Set the controls properties.
  41.         With buttonSplit
  42.             .Text = "Split"
  43.             .Font = New Font(Me.Font.FontFamily, 14.0F, FontStyle.Bold)
  44.             .Size = New Size(200, 75)
  45.             .Location = New Point(0, 0)
  46.             .Cursor = Cursors.Hand
  47.         End With
  48.  
  49.         With buttonMerge
  50.             .Text = "Merge"
  51.             .Font = New Font(Me.Font.FontFamily, 14.0F, FontStyle.Bold)
  52.             .Size = New Size(200, 75)
  53.             .Location = New Point(buttonSplit.Location.X + buttonSplit.Width, 0)
  54.             .Cursor = Cursors.Hand
  55.         End With
  56.  
  57.         With labelSplit1
  58.             .Text = "Total Progress:"
  59.             .AutoSize = True
  60.             .Font = New Font(Me.Font.FontFamily, 9.0F, FontStyle.Regular)
  61.             .Location = New Point(0, buttonSplit.Location.Y + buttonSplit.Height + 10)
  62.         End With
  63.  
  64.         With labelSplit2
  65.             .Text = "Chunk Progress:"
  66.             .AutoSize = True
  67.             .Font = New Font(Me.Font.FontFamily, 9.0F, FontStyle.Regular)
  68.             .Location = New Point(0, labelSplit1.Location.Y + labelSplit1.Height)
  69.         End With
  70.  
  71.         With labelSplit3
  72.             .Text = "Chunk Count:"
  73.             .AutoSize = True
  74.             .Font = New Font(Me.Font.FontFamily, 9.0F, FontStyle.Regular)
  75.             .Location = New Point(0, labelSplit2.Location.Y + labelSplit2.Height)
  76.         End With
  77.  
  78.         With labelMerge1
  79.             .Text = "Total Progress:"
  80.             .AutoSize = True
  81.             .Font = New Font(Me.Font.FontFamily, 9.0F, FontStyle.Regular)
  82.             .Location = New Point(buttonMerge.Location.X, buttonMerge.Location.Y + buttonMerge.Height + 10)
  83.         End With
  84.  
  85.         With labelMerge2
  86.             .Text = "Chunk Progress:"
  87.             .AutoSize = True
  88.             .Font = New Font(Me.Font.FontFamily, 9.0F, FontStyle.Regular)
  89.             .Location = New Point(buttonMerge.Location.X, labelMerge1.Location.Y + labelMerge1.Height)
  90.         End With
  91.  
  92.         With labelMerge3
  93.             .Text = "Chunk Count:"
  94.             .AutoSize = True
  95.             .Font = New Font(Me.Font.FontFamily, 9.0F, FontStyle.Regular)
  96.             .Location = New Point(buttonMerge.Location.X, labelMerge2.Location.Y + labelMerge2.Height)
  97.         End With
  98.  
  99.         ' Add the controls on the UI.
  100.         Me.Controls.AddRange({labelSplit1, labelSplit2, labelSplit3,
  101.                               labelMerge1, labelMerge2, labelMerge3,
  102.                               buttonSplit, buttonMerge})
  103.  
  104.         ' Set the Form properties.
  105.         With Me
  106.             .Size = New Size(buttonSplit.Width + buttonMerge.Width + 20, 200)
  107.             .FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog
  108.             .MaximizeBox = False
  109.         End With
  110.  
  111.     End Sub
  112.  
  113.     ''' <summary>
  114.     ''' Handles the <see cref="Button.Click"/> event of the <see cref="ButtonSplit"/> control.
  115.     ''' </summary>
  116.     Private Sub ButtonSplit_Click() Handles buttonSplit.Click
  117.  
  118.         Me.splitter.Split(sourceFile:=Me.filePath,
  119.                           chunkSize:=Me.halfFileSize,
  120.                           chunkName:=Me.chunkName,
  121.                           chunkExt:=Me.chunkExt,
  122.                           overwrite:=True,
  123.                           deleteAfterSplit:=False)
  124.  
  125.         ' Or...
  126.         'Me.splitter.Split(sourceFile:=Me.filePath,
  127.         '                  chunkCount:=2,
  128.         '                  chunkName:=Me.chunkName,
  129.         '                  chunkExt:=Me.chunkExt,
  130.         '                  overwrite:=True,
  131.         '                  deleteAfterSplit:=False)
  132.  
  133.     End Sub
  134.  
  135.     ''' <summary>
  136.     ''' Handles the <see cref="Button.Click"/> event of the <see cref="ButtonMerge"/> control.
  137.     ''' </summary>
  138.     Private Sub ButtonMerge_Click() Handles buttonMerge.Click
  139.  
  140.         Me.splitter.Merge(sourceChunk:=Me.firstChunkPath,
  141.                           targetFile:=Me.targetFileName,
  142.                           overwrite:=True,
  143.                           deleteChunksAfterMerged:=False)
  144.  
  145.     End Sub
  146.  
  147.     ''' <summary>
  148.     ''' Handles the <see cref="FileSplitter.SplitProgressChangedArgs"/> event of the <see cref="Splitter"/> instance.
  149.     ''' </summary>
  150.     ''' <param name="sender">The source of the event.</param>
  151.     ''' <param name="e">The <see cref="FileSplitter.SplitProgressChangedArgs"/> instance containing the event data.</param>
  152.     Private Sub Splitter_SplitProgressChangedArgs(ByVal sender As Object, ByVal e As FileSplitter.SplitProgressChangedArgs) _
  153.     Handles splitter.SplitProgressChanged
  154.  
  155.         labelSplit1.Text = String.Format("Total Progress: {0}%", e.TotalProgress.ToString("n1"))
  156.         labelSplit2.Text = String.Format("Chunk Progress: {0}%", e.ChunkProgress.ToString("n1"))
  157.         labelSplit3.Text = String.Format("Current  Chunk: {0} of {1}", CStr(e.ChunksCreated + 1), CStr(e.ChunksToCreate))
  158.         Application.DoEvents()
  159.  
  160.     End Sub
  161.  
  162.     ''' <summary>
  163.     ''' Handles the <see cref="FileSplitter.MergeProgressChangedArgs"/> event of the <see cref="Splitter"/> instance.
  164.     ''' </summary>
  165.     ''' <param name="sender">The source of the event.</param>
  166.     ''' <param name="e">The <see cref="FileSplitter.MergeProgressChangedArgs"/> instance containing the event data.</param>
  167.     Private Sub Splitter_MergeProgressChangedArgs(ByVal sender As Object, ByVal e As FileSplitter.MergeProgressChangedArgs) _
  168.     Handles splitter.MergeProgressChanged
  169.  
  170.         labelMerge1.Text = String.Format("Total Progress: {0}%", e.TotalProgress.ToString("n1"))
  171.         labelMerge2.Text = String.Format("Chunk Progress: {0}%", e.ChunkProgress.ToString("n1"))
  172.         labelMerge3.Text = String.Format("Current  Chunk: {0} of {1}", CStr(e.ChunksMerged + 1), CStr(e.ChunksToMerge))
  173.         Application.DoEvents()
  174.  
  175.     End Sub
  176.  
  177. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement