* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package in.techdive.jar.operations;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
/**
* JarOperations Class contains the various operations that can be performed in a Jar file
*/
public class JarOperations
{
/**
* Program entry point
*/
public static void main(String args[]) throws IOException
{
String jarFilePath = "conf/test.jar";
JarFile jarFile = new JarFile(jarFilePath);
// Print List of Files inside the Jar
printJarContent(jarFile);
System.out.println();
// Print Specific File Content inside the Jar
JarEntry jarEntry = jarFile.getJarEntry("META-INF/MANIFEST.MF");
printFileContentInsideJar(jarFile, jarEntry);
jarFile.close();
}
/**
* Method used to prints list of all file names inside the Jar File
*
* @param jarFile
* Jar File
*/
private static void printJarContent(JarFile jarFile)
{
System.out.println("Jar File Name = " + jarFile.getName());
// Prints list of all file names inside the Jar
Enumeration entries = jarFile.entries();
while (entries.hasMoreElements())
{
ZipEntry entry = (ZipEntry) entries.nextElement();
System.out.println(entry.getName());
}
}
/**
* Method used to print Jar File Entry Content inside the Jar File
*
* @param jarFile Name of the Jar File
* @param jarEntry Entry in Jar File
* @throws IOException Error in reading the content
*/
private static void printFileContentInsideJar(JarFile jarFile, JarEntry jarEntry) throws IOException
{
InputStream input = jarFile.getInputStream(jarEntry);
InputStreamReader isr = new InputStreamReader(input);
BufferedReader reader = new BufferedReader(isr);
String line;
System.out.println("Printing the contents of the file inside jar file");
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
reader.close();
}
}
Program Output:
META-INF/
META-INF/MANIFEST.MF
in/
in/techdive/
in/techdive/tcp/
in/techdive/tcp/client/
in/techdive/tcp/client/TCPClient.class
in/techdive/tcp/server/
in/techdive/tcp/server/TCPServer.class
in/techdive/udp/
in/techdive/udp/client/
in/techdive/udp/client/UDPClient.class
in/techdive/udp/server/
in/techdive/udp/server/UDPServer.class
Printing the contents of the file inside jar file
Manifest-Version: 1.0
Created-By: 1.5.0_12 (Sun Microsystems Inc.)