¿©±â¼´Â zipÆÄÀÏ ¶Ç´Â jarÆÄÀÏÀ» api¸¦ ÀÌ¿ëÇÏ¿© Ǫ´Â ¹æ¹ý¿¡ ´ëÇÏ¿© ¼³¸íÇÑ´Ù.
¿©±â¼ °¡Á®¾ßÇÒ ±âº»ÀûÀÎ Áö½ÄÀº ½ºÆ®¸²¿¡ ´ëÇÑ ÀÌÇØ°¡ ÇʼöÀ̸ç, ÆÄÀÏ¿¡ ´ëÇÑ read,
write ¸Þ¼Òµå¿Í ±×°ÍµéÀ» ÇÊÅ͸µÇÒ ¼ö ÀÖ´Â ´É·ÂÀÌ ÇÊ¿äÇÑ´Ù.
°£´ÜÇÏ°Ô ¼Ò°³ÇÏÀÚ¸é ÀÚ¹Ù¿¡¼ IO¶õ ¿©·¯ºÐµéÀÌ ¼öµµ·Î µé¾î¿Â ¹°À» Á¤¼ö±â·Î °É·¯
Á÷Á¢ ¸¶½Ç¼ö ÀÖ´À³Ä ¾ø´À³ÄÀÇ ¹®Á¦À̸ç, ¶ÇÇÑ ¼öµµ²ÀÁö¸¦ Àá±×´Â°ÍÀÌ closeÀÌ°í ¿©´Â°ÍÀÌ
readÀ̸ç, Á¤¼ö±âÀÇ ÇÊÅ͸¦ ÀÌ¿ëÇÏ¿© ¹°À» °É·¯ ¸¶½Ã´Â °ÍÀ» ÇÊÅ͸µÀ̶ó ÇÑ´Ù.
¿©±â¼´Â java.io.ZipInputStreamµîÀ» ÅëÇÏ¿© ÀԷ¹ÞÀº zip or jarÆÄÀÏ Æ÷¸ËÀÇ ¾ÐÃàÆÄÀÏÀ»
Ç®µµ·Ï ÇÑ´Ù. jarÆÄÀÏÀÌ zipÆÄÀÏÀÇ ÇüŶó´Â °ÍÀº ¿©·¯ºÐµµ ÀÍÈ÷ ¾Ë°í ÀÖÀ» °ÍÀÌ´Ù.
´ç¿¬È÷ zipÆÄÀÏÀÌ Ç®¸®¸é jarÆÄÀϵµ Ç®¸®´Â °ÍÀÌ´Ù.
ÇÙ½ÉÀûÀÎ ÄÚµå´Â ¾Æ·¡¿Í °°´Ù.
FileInputStream fis=new FileInputStream(args[0]);
BufferedInputStream bis=new BufferedInputStream(fis);
ZipInputStream zis=new ZipInputStream(bis);
ÀԷ¹ÞÀº ÆÄÀÏÀ» ÀоîµéÀÌ´Â ¿ªÇÒÀ» Çϸç ZipEntry¸¦ ÅëÇÏ¿© ÇØ´ç ÆÄÀÏÀÇ ¾ÐÃàµÇ¾îÁø
¿£Æ®¸®¸¦ È®ÀÎ ÇÒ ¼ö ÀÖ´Ù.
¶ÇÇÑ ¾ÐÃàÀÌ Ç®¸± À§Ä¡¸¦ ÁöÁ¤ÇÒ ¼ö ÀÖ¾î¾ß ÇϹǷΠgetDirectory¶ó´Â ¸Þ¼Òµå¸¦ ÅëÇÏ¿©
ÀԷ¹ÞÀº °ªÀ» ÀÌ¿ëÇÏ¿© ¾ÐÃàÀÌ Ç®¸±¼ö ÀÖµµ·Ï »ç¿ëÇÑ´Ù.
¿ì¼± µð·ºÅ丮ÀÔ·ÂÀ» ¹ÞÀ» ¼ö ÀÖ´Â MakeDirectoryŬ·¡½º¸¦ º¸µµ·Ï ÇÏÀÚ.
public static String getDirectory(String target){
File f = new File(target);
if(f.exists()) return target;
Vector v = new Vector();
boolean END=false;
while(!END){
if(!f.exists()) {
v.addElement(f);
f = new File(f.getParent());
}
else END=!END;
}
for(int i=v.size()-1 ; i>=0 ;i--){
((File)v.elementAt(i)).mkdir();
}
return target;
}
}
À§ÀÇ ¸Þ¼Òµå¿¡¼´Â ¾ÐÃàÀ» Ç® µð·ºÅ丮¸¦ ã¾Æº»ÈÄ ¸¸¾à Á¸ÀçÇÏÁö ¾Ê´Â´Ù¸é ÀԷ¹ÞÀº
À§Ä¡¿¡ µð·ºÅ丮¸¦ recursiveÇÏ°Ô »ý¼ºÇÑ´Ù.
mkdir -r ¿É¼Ç°°Àº °æ¿ì¶ó »ý°¢ÇÏ¸é ¹«¹æÇÏ°Ú´Ù.
ÀÚ, ±×·¯¸é ÀÌ ÆÄÀÏÀÇ Àüü ¼Ò½º¸¦ º¸µµ·Ï ÇÏÀÚ.
import java.util.zip.*;
import java.util.jar.*;
import java.io.*;
import java.util.*;
class JarTarget {
public static void main(String[] args) {
if( args.length < 2 ) {
System.out.println("Usage : java JarTarget source_jar target_directory");
System.exit(0);
}
try{
FileInputStream fis=new FileInputStream(args[0]);
BufferedInputStream bis=new BufferedInputStream(fis);
ZipInputStream zis=new ZipInputStream(bis);
ZipEntry ze=null;
int count = 0;
while ((ze=zis.getNextEntry())!=null){
// if it is a directory then we do nothing
// System.out.println("[Extracting the file] " + ze.getName());
String zeName = ze.getName().replace('/', File.separatorChar);
File path = new File(args[1] + File.separator + zeName);
makeDirectory(
path.toString().substring(0,path.toString().lastIndexOf(File.separator)));
if (ze.isDirectory()) {
path.mkdir();
continue;
}
System.out.println(path.toString());
FileOutputStream fos =new FileOutputStream(path.toString());
int size=(int) ze.getSize();
if (size==-1) {
// ZipEntry.getSize()¾ò´Â ºÎºÐ ¾µ¶ó¸é ¾²°í..
}
// ÇٽɺκÐ...
byte[] b=new byte[(int)size];
int rb=0;
int chunk=0;
while (((int)size - rb) > 0) {
chunk=zis.read(b,rb,(int)size - rb);
fos.write(b, 0, chunk);
if (chunk==-1) {
break;
}
rb+=chunk;
}
++count;
}
}catch(Exception e) {
e.printStackTrace();
}
}
public static String makeDirectory(String target){
File f = new File(target);
if(f.exists()) return target;
Vector v = new Vector();
boolean END=false;
while(!END){
if(!f.exists()) {
v.addElement(f);
f = new File(f.getParent());
}
else END=!END;
}
for(int i=v.size()-1 ; i>=0 ;i--){
((File)v.elementAt(i)).mkdir();
}
return target;
}
}
À§ÀÇ ÄÚµåÁ¤µµ¸¦ ÀÌÇØÇÒ ¼ö ÀÖ´Ù¸é IO¿¡´Â ¾î´ÀÁ¤µµ ±âº»±â°¡ °®ÃçÁ³´Ù°í Çصµ ¹«¹æÇÏ´Ù
ÀϹÝÀûÀ¸·Î °³¹ßÀÚµéÀÌ ¾àÇÑ ºÎºÐÀÌ IO/Thread/NetworkÀÌ ¾Æ´Ò±î ÇÑ´Ù.
Written by carouser
2001-07-12 |