Introduction
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 a 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: http://www.icsharpcode.net/OpenSource/SharpZipLib/
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 www.icsharpcode.net web, 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.
InitializeComponent()
' 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 in 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
Try
' verify the paths are set
If (String.IsNullOrEmpty(jarPath) = False And _
String.IsNullOrEmpty(saveFolderPath) = False) Then
Try
' 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
System.Diagnostics.Process.Start("explorer",
saveFolderPath)
Catch ex As Exception
' something went wrong
MessageBox.Show(ex.Message, "Extraction Error")
End Try
Else
' 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 " +
Environment.NewLine)
sb.Append("destination folder before attempting to " +
Environment.NewLine)
sb.Append("to extract a jar file.")
MessageBox.Show(sb.ToString(), "Unable to Extract")

Join the conversation! Your thoughts help the community grow.