如果这个包是.jar文件的话,就用java.util.zip里面的东东就行了,下面这个程序就可以在无参数时打印文件名清单,加上参数-x则释放jar文档中的文件
public class WholeProgram{public static final int LIST = 0,EXTRACT = 1; protected int mode = LIST;
protected ZipFile zippy;
protected byte[] b; public static void main(String[] args){
WholeProgram u = new WholeProgram(); for(int i = 0;i < args.length;i++){
if("-x".equals(args[i])){
u.setMode(EXTRACT);
continue;
}
String candidate = args[i];
if(candidate.endsWith(".zip") || candidate.endsWith(".jar"))
u.unZip(candidate);
else System.err.println("Not a zip file? " + candidate);
}
System.err.println("All done!");
} public WholeProgram(){
b = new byte[8092];
} protected void setMode(int m){
if(m == LIST || m == EXTRACT)
mode = m;
} public void unZip(String filename){
try{
zippy = new ZipFile(filename);
Enumeration all = zippy.entries();
while(all.hasMoreElements()){
getFile(((ZipEntry)(all.nextElement())));
}
}
catch(IOException e){
System.err.println("IO Error: " + e);
return;
}
} protected void getFile(ZipEntry e) throws IOException{
String zipName = e.getName();
if(mode == EXTRACT){
if(zipName.endsWith("")){
new File(zipName).mkdirs();
return;
}
System.err.println("Creating " + zipName);
FileOutputStream os = new FileOutputStream(zipName);
InputStream is = zippy.getInputStream(e);
int n = 0;
while((n = is.read(b)) > 0)
os.write(b,0,n);
is.close();
os.close();
}
else
if(e.isDirectory()){
System.out.println("Directory " + zipName);
}
else{
System.out.println("File " + zipName);
}
}
}
如果是目录形式,则就用和上面一样的办法,读入一个目录,先用list()方法,然后判断里面的文件是否为目录,如果是又继续向下
public class WholeProgram{public static final int LIST = 0,EXTRACT = 1; protected int mode = LIST;
protected ZipFile zippy;
protected byte[] b; public static void main(String[] args){
WholeProgram u = new WholeProgram(); for(int i = 0;i < args.length;i++){
if("-x".equals(args[i])){
u.setMode(EXTRACT);
continue;
}
String candidate = args[i];
if(candidate.endsWith(".zip") || candidate.endsWith(".jar"))
u.unZip(candidate);
else System.err.println("Not a zip file? " + candidate);
}
System.err.println("All done!");
} public WholeProgram(){
b = new byte[8092];
} protected void setMode(int m){
if(m == LIST || m == EXTRACT)
mode = m;
} public void unZip(String filename){
try{
zippy = new ZipFile(filename);
Enumeration all = zippy.entries();
while(all.hasMoreElements()){
getFile(((ZipEntry)(all.nextElement())));
}
}
catch(IOException e){
System.err.println("IO Error: " + e);
return;
}
} protected void getFile(ZipEntry e) throws IOException{
String zipName = e.getName();
if(mode == EXTRACT){
if(zipName.endsWith("")){
new File(zipName).mkdirs();
return;
}
System.err.println("Creating " + zipName);
FileOutputStream os = new FileOutputStream(zipName);
InputStream is = zippy.getInputStream(e);
int n = 0;
while((n = is.read(b)) > 0)
os.write(b,0,n);
is.close();
os.close();
}
else
if(e.isDirectory()){
System.out.println("Directory " + zipName);
}
else{
System.out.println("File " + zipName);
}
}
}
如果是目录形式,则就用和上面一样的办法,读入一个目录,先用list()方法,然后判断里面的文件是否为目录,如果是又继续向下
FileOutputStream os = new FileOutputStream(zipName);
InputStream is = zippy.getInputStream(e);
int n = 0;
while((n = is.read(b)) > 0)
os.write(b,0,n);
is.close();
os.close();这一段是干什么用的?我比较菜,能否解释一下?