(c) BigSpeed Computing Inc. - Secure private networking

Introduction

Transferring large files over the Internet can be speeded up significantly if compression/decompression is applied before/after the upload/download operation. This article shows how to do that in VB.NET with a freeware file sharing library.

Before we start


Download and install the free edition of file sharing SDK, which is free for non-commercial use.

The server



Load the sample project ZipServer.

To start accepting new connections, we have to set the property ZipSrv.ListenningPort (2222 in our case) and call the method ZipSrv.Start(). In order to make the compression work, we have to write handlers for the events OnNeedZip, OnZipDone, OnNeedUnzip, and OnUnzipDone.

OnNeedZip

OnNeedZip event notifies about a client's request and asks for permission to start a compression operation.

  'Request for zipping
  Private Sub ZipSrv_OnNeedZip(ByVal sender As System.Object, ByVal e As AxbsFileServerSDK.IBSZipSrvXEvents_OnNeedZipEvent) Handles ZipSrv.OnNeedZip
    e.aOkay = True           'permit the operation
    e.aRoot = App_Path()     'give our application folder as root folder
    LogMsg(GetUsername(e.aHandle) + ": start zipping to " + e.aPath)
  End Sub
We should set the parameter aOkay to True, otherwise the server will not commence the zip operation. We have to set the parameter aRoot to point to the physical directory on the local machine, which becomes the virtual root directory for the client. In our sample we use the current working directory, where the executable file resides.

OnZipDone

OnZipDone event reports the completion status of the zip operation.
  'The zip operation is completed
  Private Sub ZipSrv_OnZipDone(ByVal sender As System.Object, ByVal e As AxbsFileServerSDK.IBSFileSrvXEvents_OnZipDoneEvent) Handles ZipSrv.OnZipDone
    LogMsg(GetUsername(e.aHandle) + ": finish zipping - code " + Str(e.aCode))
  End Sub
We just make a record in the log.

OnNeedUnzip

OnNeedUnzip event notifies about a client's request and asks for permission to start a decompression information.
  'Request for unzipping
  Private Sub ZipSrv_OnNeedUnzip(ByVal sender As System.Object, ByVal e As AxbsFileServerSDK.IBSFileSrvXEvents_OnNeedUnzipEvent) Handles ZipSrv.OnNeedUnzip
    e.aOkay = True           'permit the operation
    e.aRoot = App_Path()     'give our application folder as root folder
    LogMsg(GetUsername(e.aHandle) + ": start unzipping from " + e.aPath)
  End Sub
We should set the parameter aOkay to True, otherwise the server will not commence the unzip operation. We have to set the parameter aRoot to point to the physical directory on the local machine, which becomes the virtual root directory for the client. In our sample we use the current working directory, where the executable file resides.

OnUnzipDone

OnUnzipDone event reports the completion status of the zip operation.
  'The unzip operation is completed
  Private Sub ZipSrv_OnUnzipDone(ByVal sender As Object, ByVal e As AxBSZipSrvLib.IBSZipSrvXEvents_OnUnzipDoneEvent) Handles ZipSrv.OnUnzipDone
    LogMsg(txtRmtAddr.Text + ": finish unzipping - " + Str(e.aCode))
  End Sub
We just make a record in the log.





The client



Load the sample project ZipClient.

To establish a new connection to the server, we have to call the method ZipCln.Connect(Addr, Port), passing as parameters the IP address and the listening port of the server.
'Initiate a connection request
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
  If fConnect.ShowDialog <> DialogResult.OK Then Exit Sub

  If fConnect.txtKey.Text() > "" Then
    ZipCln.SecurityMode = 2  'shared secret key
    ZipCln.SecretKey = fConnect.txtKey.Text
  Else
    ZipCln.SecurityMode = 0  ' no encryption
  End If
  If Not ZipCln.Connect(fConnect.txtHost.Text, Val(fConnect.txtPort.Text)) Then
    MsgBox("Error Code: " + Str(ZipCln.LastError))
  End If

  UpdateStatus()
End Sub

If the call is not successful, we show an error message.

In order to make the compression work, we have to employ the methods Zip and Unzip , and to write handlers for the events OnZipProgress, OnZipDone, OnUnzipProgress, OnUnzipDone.

Zip

The Zip method starts a new compression operation.
  'Start the zip operation
  Private Sub btnZip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZip.Click
    Dim Nm As String

    Nm = InputBox("Zip file name", "Zipping")                'ask the user for the zip file name
    If Nm = "" Then Exit Sub 'canceled by the user

    If ExtractFileExt(Nm) = "" Then
      Nm = Nm + ".zip"
    End If

    If ZipCln.Zip(AddSlash(txtFolder.Text) + Nm, AddSlash(txtFolder.Text) + lvFiles.SelectedItems(0).Text) Then
      'the request is accepted
      NowZip = True                   'raise the flag   
      ZipFile = Nm                    'remember the zip file
      ZipFol = txtFolder.Text         'remember the current folder
      txtZip.Text = "Handshaking..."
    Else
      CheckError(ZipCln.LastError)   'the request is rejected
    End If
    UpdateStatus()
  End Sub
We prompt the user for the zip file name, and if it's not empty, we call the Zip method with two parameters. The first one is the relative pathname of the zip archive, the second one is the relative pathname of the file to be compressed. If the request is successful, we remember the current folder and the zip file name. Otherwise, we show an error message.

OnZipProgress

OnZipProgress event provides progress information about the zip operation.
  'Progress information is available for the zip operation
  Private Sub ZipCln_OnZipProgress(ByVal sender As System.Object, ByVal e As AxBSFileClientSDK.IBSFileClnXEvents_OnZipProgressEvent) Handles ZipCln.OnZipProgress
    'Show the current byte count and the total amount of bytes (low 32 bits)
    txtZip.Text = ZipFile + " - " + Str(e.aByteCountLo) + "/" + Str(e.aByteTotalLo)
  End Sub
We display two counters (low 32 bits only) - the number of bytes compressed so far, and the total number of requested bytes.

OnZipDone

OnZipDone event reports the result of the zip operation.
  'The zip operation is completed
  Private Sub ZipCln_OnZipDone(ByVal sender As System.Object, ByVal e As AxBSFileClientSDK.IBSFileClnXEvents_OnZipDoneEvent) Handles ZipCln.OnZipDone
    If e.aCode = 0 Then
      'success
      txtZip.Text = ZipFile + " - " + "Done."
      If ZipFol = txtFolder.Text Then
        ListFolder()           'refresh the file list
      End If
    Else
      txtZip.Text = ZipFile + " - " + "Aborted: " + ErrorText(e.aCode)     'there is an error, describe it
    End If
    NowZip = False
    UpdateStatus()
  End Sub
We display the completion status of the zip operation, and if the current folder is affected, we update the file list.

Unzip

The Unzip method starts a new decompression operation.
  'Start the unzip operation
  Private Sub btnUnzip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnzip.Click
    Dim Nm As String

    'ask the user for the extraction folder
    Nm = InputBox("Extract to folder (leave blank for current):", "Unzipping")
    If Nm = "" Then Exit Sub 'canceled by the user

    If ZipCln.Unzip(AddSlash(txtFolder.Text) + lvFiles.SelectedItems(0).Text, AddSlash(txtFolder.Text) + Nm) Then
      'the request is accepted
      NowUnzip = True                                  'raise the flag   
      UnzipFile = Nm                                   'remember the unzip file
      UnzipFol = txtFolder.Text                       'remember the current folder
      txtUnzip.Text = "Handshaking..."
    Else
      CheckError(ZipCln.LastError)   'the request is rejected
    End If
    UpdateStatus()
  End Sub
We prompt the user for the destination folder, where the extracted file will be placed. Then we call the Unzip method with two parameters. The first one is the relative pathname of the zip archive, the second one is the relative pathname of the destination folder. If the request is successful, we remember the current folder and the zip file name. Otherwise, we show an error message.

OnUnzipProgress

OnUnzipProgress event provides progress information about the unzip operation.
  'Progress information is available for the unzip operation
  Private Sub ZipCln_OnUnzipProgress1(ByVal sender As System.Object, ByVal e As AxBSFileClientSDK.IBSFileClnXEvents_OnUnzipProgressEvent) Handles ZipCln.OnUnzipProgress
    'Show the current byte count and the total amount of bytes (low 32 bits)
    txtUnzip.Text = UnzipFile + " - " + Str(e.aByteCountLo) + "/" + Str(e.aByteTotalLo)
  End Sub
We display two counters (low 32 bits only) - the number of bytes decompressed so far, and the total number of bytes in the archive.

OnUnzipDone

OnUnzipDone event reports the result of the unzip operation.
  'The unzip operation is completed
  Private Sub ZipCln_OnUnzipDone(ByVal sender As System.Object, ByVal e As AxBSFileClientSDK.IBSFileClnXEvents_OnUnzipDoneEvent) Handles ZipCln.OnUnzipDone
    If e.aCode = 0 Then
      'success
      txtUnzip.Text = UnzipFile + " - " + "Done."
      If UnzipFol = txtFolder.Text Then
        ListFolder()            'refresh the file list
      End If
    Else
      txtUnzip.Text = UnzipFile + " - " + "Aborted: " + ErrorText(e.aCode)     'there is an error, describe it
    End If
    NowUnzip = False
    UpdateStatus()
  End Sub
We display the completion status of the unzip operation, and if the current folder is affected, we update the file list.



The bottom line

Remote file compression can be very useful in some situations, when a large amount of data must be exchanged across the Internet. This could be one of the simplest solution for that.

(c) BigSpeed Computing Inc. - Secure private networking