프로그래밍/JAVA

java.util.jar 이용한 jar 압축 풀기

잘 살아보세 2009. 4. 1. 14:51
public void unZip(String fileName, String filePath){
try{
FileInputStream fis = new FileInputStream(fileName);
JarInputStream jis = new JarInputStream(fis);
JarEntry je = null;
while( (je = jis.getNextJarEntry()) != null ){ //stream에서 entry를 얻어낸다.
System.out.println(je.getName());
System.out.println(filePath + je.getName());
if(je.isDirectory()){ //디렉토리이면 쓰지 않고 디렉토리를 생성한다.
File fff = new File(filePath + je.getName());
if(!fff.exists()){
fff.mkdir();
}

System.out.println("directory!!");
}else{ //파일이면 쓴다.
FileOutputStream fos = new FileOutputStream(filePath + je.getName
());
int red = 0;
while((red = jis.read()) != -1){
fos.write(red);
}
fos.close();
}
jis.closeEntry(); //Entry를 닫고 while문으로 돌아가 다음 entry 뽑아낸다.
}
jis.close(); //Entry 다 뽑아내면 stream닫기.
}catch(Exception e){
e.printStackTrace();
}
}