Click here to Skip to main content
13,551,173 members
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 8 Jul 2008
Licenced CPOL

Opening Jars with Visual Basic

, 8 Jul 2008
Rate this:
Please Sign up or sign in to vote.
This article shall describe an approach that may be used to extract Jar files.


This article shall describe an approach that may be used to extract Jar files. Jars are merely files compressed using zip compression; the primary difference between a standard trash zip file and a jar file is that the jar file includes a manifest; also true jar files can be run as an executable on a machine equipped with the Java runtime. Jars typically contain Java class files; if you have some need to extract a jar and view its contents, it is no more difficult to do that than it is to unzip a normal zip file.


Figure 1: Extracting a Jar File

Since the task of extracting a jar is really the same as it is to extract a zip file, the demo application will rely upon the free SharpZipLib libraries to perform the extraction; these libraries may be downloaded from this location: The Zip, GZip, BZip2 and Tar Implementation For .NET.

In addition to handling jar and zip files, the library also handles tar, gzip, and bzip2 compression.

The Solution

The solution contains a single project (Jars). The example is provided in the form of a single Windows Forms project; the project contains a single main form; the references are all in the default configuration with the exception being that the SharpZipLib DLL has been added (first item in the reference list). Having downloaded the DLL from the Open Source Projects for the .NET Platform page, the download was installed into the local file system and was adding by using the “Add Reference” dialog’s Browse option. All of the code necessary to drive the application is included in the main form’s code file.


Figure 2: The Solution Explorer Showing the Project

The Code: Jars–Form1

The Jars project is a Windows Forms project containing a single form. All UI and code required by the project are contained in the single main form.

The only references added to the project were the SharpZipLib, System.IO, and System.Text. The imports and class declaration are as follows:

Imports ICSharpCode.SharpZipLib.Zip
Imports System.IO
Imports System.Text

Public Class Form1

After the class declaration, two private variables are declared; these variables are used to retain the path to the jar file and the path to the destination folder (where the jar will be unpacked).

' set local variables to hold
' the paths to the jar file and
' to destination folder
Private mSourceJar As String
Private mDestinationFolder As String

The next block of code is the default constructor; nothing added there:

Public Sub New()

    ' This call is required by the Windows Form Designer.

    ' Add any initialization after the InitializeComponent() call.

End Sub

The next block of code is used to perform the actual extraction. The ExtractJar function accepts two arguments, a string value defining the path to the jar file, and a string value defining the path to the destination folder (the location at which the jar will be unpacked). The method uses the SharpZipLibrary to extract the jar file. The code is annotated to describe the activity.

''' <summary>
''' Extracts the jar file to a specified
''' destination folder.
''' </summary>
Private Sub ExtractJar(ByVal pathToJar As String, _
                       ByVal saveFolderPath As String)

    Dim jarPath As String = pathToJar
    Dim savePath As String = saveFolderPath


        ' verify the paths are set
        If (String.IsNullOrEmpty(jarPath) = False And _
            String.IsNullOrEmpty(saveFolderPath) = False) Then


                ' use the SharpZip library FastZip
                ' utilities ExtractZip method to
                ' extract the jar file
                Dim fz As New FastZip()
                fz.ExtractZip(jarPath, saveFolderPath, "")

                ' Success at this point, tell the user
                ' we are done.
                MessageBox.Show("Jar File: " + jarPath + " was extracted
                    to " + saveFolderPath, "Finished")

                ' open the destination folder in explorer

            Catch ex As Exception

                ' something went wrong
                MessageBox.Show(ex.Message, "Extraction Error")

            End Try


            ' the paths were not, tell the user to
            ' get with the program

            Dim sb As New StringBuilder()
            sb.Append("Set the paths to both the jar file and " +
            sb.Append("destination folder before attempting to " +
            sb.Append("to extract a jar file.")

            MessageBox.Show(sb.ToString(), "Unable to Extract")

        End If

    Catch ex As Exception

        ' something else went wrong
        MessageBox.Show(ex.Message, "Extraction Error")

    End Try

End Sub

The next block of code is used to set the path to the jar file. This button click event handler is used to display an open file dialog box; the user can use the dialog box to navigate to the location of the jar file and select it. The selected file name is used to set the mSourceJar string variable to the file path of the jar file; the file path is also displayed in the text box adjacent to the button.

''' <summary>
''' Set the path to the Jar file
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub btnSetJar_Click(ByVal sender As System.Object, _
                            ByVal e As System.EventArgs) _
                            Handles btnSetJar.Click


        OpenFileDialog1.Title = "Extract JAR"
        OpenFileDialog1.DefaultExt = "jar"
        OpenFileDialog1.Filter = "Jar Files|*.jar"
        OpenFileDialog1.FileName = String.Empty
        OpenFileDialog1.Multiselect = False

        If (OpenFileDialog1.ShowDialog() = DialogResult.OK) Then

            If (OpenFileDialog1.FileName = String.Empty) Then
            End If

            Dim strExt As String
            strExt =
            strExt = strExt.ToUpper()

            If (strExt = ".JAR") Then
                mSourceJar = OpenFileDialog1.FileName
                txtJarPath.Text = mSourceJar
                MessageBox.Show("The selected file is not a jar", "File
            End If

            MessageBox.Show("File request cancelled by user.",
        End If

    Catch ex As Exception

        MessageBox.Show(ex.Message.ToString(), "Error")

    End Try

End Sub

The next section of code is used to set the path to the destination folder. The destination folder defines the location where the selected jar file will be unpacked. The button click event handler opens a folder browser dialog which will permit the user to navigate to and/or to create a destination folder. Once set, the folder browser dialog’s selected path property is used to the mDestinationFolder variable and it is also used to display the selected destination folder path using the selected path’s value.

''' <summary>
''' Set the path to the folder where
''' the jar file is to be extracted
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub btnDestinationFolder_Click(ByVal sender As System.Object, _
                                       ByVal e As System.EventArgs) _
                                       Handles btnDestinationFolder.Click


        folderBrowserDialog1.ShowNewFolderButton = True

        Dim result As DialogResult = folderBrowserDialog1.ShowDialog()
        If (result = DialogResult.OK) Then

            mDestinationFolder = folderBrowserDialog1.SelectedPath
            txtDestinationFolder.Text = mDestinationFolder

        End If

    Catch ex As Exception

        MessageBox.Show(ex.Message, "Error Setting Destination Folder")

    End Try

End Sub

The last button click event handler is used to call the ExtractJar method. The click event handler merely evokes the ExtractJar method and passes it the two variables used to contain the source jar file and the destination folder.

''' <summary>
''' Use the ExtractJar function to
''' extract the source jar file into the
''' destination folder
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub btnExtract_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExtract.Click

    ExtractJar(mSourceJar, mDestinationFolder)

End Sub

That wraps up the description of the code used in this project.


This example demonstrates extracting a jar file using SharpZipLib; Jar files are nothing more than zip compressed archives containing some meta data along with (typically) Java class files; extracting them is no more difficult than extracting a normal zipped archive; particularly with the assistance of the SharpZipLib.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180515.1 | Last Updated 8 Jul 2008
Article Copyright 2008 by salysle
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid