A program used to search for a class in a bunch of zip/jar files

Source: Internet
Author: User
Author:Sonymusic

Title:One is used to search for a class in a bunch of zip/jar filesProgram

Keywords:Java zip search

Category:ExampleCode

Confidentiality level:Public

(Score:, reply: 0, read: 142 )»»

Reprinted at will, but please indicate the source and author

Sonymusic

2003.05.28

========================================================== ========================================

A program used to search for a class in a bunch of zip/jar files

Package Sony. utils;

Import java. util. Jar .*;

Import java.util.zip .*;

Import java. util .*;

Import java. Io .*;

/**

* @ Author sonymusic

*

* Very simple, but it may be useful for finding a class in all the zip/jar files in the specified directory. <br>

* At least I think it is useful. Sometimes it is difficult to find a class in the JRun pile of. Jar files. <Br>

* <B> usage: </B> JAVA Sony. utils. findclass basedir classname

*/

Public class findclass {

Public static void main (string [] ARGs ){

If (ARGs. length! = 2 ){

Printuseage (-1 );

Return;

}

File base = new file (ARGs [0]);

If (! Base. exists ()){

System. Out. println ("base dir not exist! ");

Printuseage (-2 );

}

If (! Base. isdirectory ()){

System. Out. println ("base DIR is a file! ");

Printuseage (-3 );

}

List ziplist = getsubjarfiles (base );

Zipfile zfile = NULL;

Try {

Int fcount = 0;

For (INT I = 0; I <ziplist. Size (); I ++ ){

Zfile = new zipfile (File) ziplist. Get (I ));

Enumeration Enum = zfile. Entries ();

While (enum. hasmoreelements ()){

Zipentry ze = (zipentry) enum. nextelement ();

If (matchentry (Ze, argS [1]) {

System. Out. println ("found in zip/JAR file:" + zfile. getname () + "\ tfilename:" + Ze. getname ());

++ Fcount;

Break;

}

}

}

If (fcount = 0 ){

System. Out. println ("Not found. Maybe basedir or classname is not correct .");

Printuseage (0 );

}

Else {

System. Out. println ("found" + fcount + "zip/JAR file (s )");

}

}

Catch (exception e ){

E. printstacktrace ();

Printuseage (-10 );

}

}



Private Static void printuseage (INT retstatus ){

System. Out. println ("useage: Java Sony. utils. findclass basedir classname ");

System. Out. println ("\ tbasedir: where you want to find. e.g. D: \ WEB \ Web-INF \ Lib ");

System. Out. println ("\ tclassname: waht you want to find. e.g. httpservlet ");

System. Out. println ();

System. Exit (retstatus );

}



Private Static list getsubjarfiles (File basedir ){

List ret = new arraylist ();

// File base = new file (basedir );

File [] TMP = basedir. listfiles (filefilter );

For (INT I = 0; I <TMP. length; I ++ ){

If (TMP [I]. isfile ()){

Ret. Add (TMP [I]);

}

If (TMP [I]. isdirectory ()){

Ret. addall (getsubjarfiles (TMP [I]);

}

}

Return ret;

}



Private Static Boolean matchentry (zipentry Ze, string classname ){

Stringtokenizer ST = new stringtokenizer (ze. getname (),"/");

String filename = NULL;

While (St. hasmoretokens ()){

Filename = ST. nexttoken ();

}

If (filename. Equals (classname + ". Class ")){

Return true;

}

Else return false;

}



Private Static filefilter = new filefilter (){

Public Boolean accept (File pathname ){

String TMP = pathname. getname (). tolowercase ();

If (TMP. endswith (". Jar") | TMP. endswith (". Zip ")){

Return true;

}

Return false;

}

};

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.