使用 Visual Basic 打开 Jar 文件
本文将描述一种可用于提取 Jar 文件的方法。
引言
本文将描述一种可用于提取 Jar 文件的方法。 Jar 仅仅是使用 zip 压缩的文件;标准 trash zip 文件和 jar 文件之间的主要区别在于 jar 文件包含 manifest 文件;此外,真正的 jar 文件可以在配备 Java 运行时的机器上作为可执行文件运行。 Jars 通常包含 Java 类文件;如果您需要提取 jar 并查看其内容,它并不比解压缩普通 zip 文件困难。
由于提取 jar 的任务实际上与提取 zip 文件相同,因此演示应用程序将依赖于免费的 SharpZipLib 库来执行提取;这些库可以从以下位置下载:The Zip, GZip, BZip2 and Tar Implementation For .NET.
除了处理 jar 和 zip 文件,该库还处理 tar、gzip 和 bzip2 压缩。
解决方案
解决方案包含一个项目 (Jars)。该示例以单个 Windows Forms 项目的形式提供;该项目包含单个主窗体;除了添加了 SharpZipLib DLL(引用列表中的第一项)之外,所有引用都在默认配置中。从 .NET 平台的开源项目 页面下载 DLL 后,下载安装到本地文件系统,并通过“添加引用”对话框的“浏览”选项添加。 驱动应用程序所需的所有代码都包含在主窗体的代码文件中。
代码:Jars–Form1
Jars 项目是一个包含单个窗体的 Windows Forms 项目。项目所需的所有 UI 和代码都包含在单个主窗体中。
添加到项目的唯一引用是 SharpZipLib、System.IO
和 System.Text
。导入和类声明如下
Imports ICSharpCode.SharpZipLib.Zip
Imports System.IO
Imports System.Text
Public Class Form1
在类声明之后,声明了两个 private
变量;这些变量用于保留 jar 文件的路径和目标文件夹(jar 将被解压缩到的位置)的路径。
' set local variables to hold
' the paths to the jar file and
' to destination folder
Private mSourceJar As String
Private mDestinationFolder As String
下一个代码块是默认构造函数;没有添加任何东西
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
下一个代码块用于执行实际提取。 ExtractJar
函数接受两个参数,一个 string
值,用于定义 jar 文件的路径,以及一个 string
值,用于定义目标文件夹(jar 将被解压缩到的位置)的路径。 该方法使用 SharpZipLibrary
提取 jar 文件。 代码已注释以描述活动。
''' <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")
End If
Catch ex As Exception
' something else went wrong
MessageBox.Show(ex.Message, "Extraction Error")
End Try
End Sub
下一个代码块用于设置 jar 文件的路径。 此按钮单击事件处理程序用于显示打开文件对话框; 用户可以使用该对话框导航到 jar 文件的位置并选择它。 所选文件名用于将 mSourceJar
string
变量设置为 jar 文件的文件路径; 文件路径也显示在按钮旁边的文本框中。
''' <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
Try
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
Return
End If
Dim strExt As String
strExt =
System.IO.Path.GetExtension(OpenFileDialog1.FileName)
strExt = strExt.ToUpper()
If (strExt = ".JAR") Then
mSourceJar = OpenFileDialog1.FileName
txtJarPath.Text = mSourceJar
Else
MessageBox.Show("The selected file is not a jar", "File
Error")
End If
Else
MessageBox.Show("File request cancelled by user.",
"Cancelled")
End If
Catch ex As Exception
MessageBox.Show(ex.Message.ToString(), "Error")
End Try
End Sub
下一节代码用于设置目标文件夹的路径。 目标文件夹定义了所选 jar 文件将被解压缩到的位置。 按钮单击事件处理程序打开一个文件夹浏览器对话框,该对话框允许用户导航到和/或创建目标文件夹。 设置后,文件夹浏览器对话框的 selected path 属性将用于 mDestinationFolder
变量,它也用于使用所选路径的值来显示所选目标文件夹路径。
''' <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
Try
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
最后一个按钮单击事件处理程序用于调用 ExtractJar
方法。 单击事件处理程序仅调用 ExtractJar
方法,并将用于包含源 jar 文件和目标文件夹的两个变量传递给它。
''' <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
这样就结束了对该项目中使用的代码的描述。
摘要
此示例演示了使用 SharpZipLib 提取 jar 文件; Jar 文件只不过是 zip 压缩的归档文件,其中包含一些元数据以及(通常)Java 类文件; 提取它们并不比提取普通的 zip 归档文件困难; 尤其是在 SharpZipLib 的帮助下。