package common;
import java.io.*;
import java.util.*;
import net.sf.jazzlib.*;
public class Unzip {
public static String runUn(String zip, String where) {
String fileName[] = zip.split("\\.");
String fileType = fileName[fileName.length-1];
if(fileType.equals("zip"))
return unZip(zip, where);
else if(fileType.equals("gz"))
return unGz(zip, where);
else return "fileExtErr";
}
public static String unGz(String zip, String where) {
String res = "";
try {
GZIPInputStream in = new GZIPInputStream(new FileInputStream(zip));
FileOutputStream out = new FileOutputStream(where);
byte[] buffer = new byte[4096];
int bytes_read;
while((bytes_read = in.read(buffer)) != -1) {
out.write(buffer, 0, bytes_read);
}
out.close();
in.close();
res = "true";
} catch(IOException e) {
res = "false";
e.printStackTrace();
}
return res;
}
public static String unZip(String zip, String where) {
String res = "";
try {
ZipFile zf = new ZipFile(new File(zip));
Enumeration entryEnum = zf.entries();
ZipEntry entry = null;
InputStream is = null;
String fileName = null;
FileOutputStream out = null;
byte[] buffer = new byte[4096];
int bytes_read;
while(entryEnum.hasMoreElements()) {
entry = (ZipEntry)entryEnum.nextElement();
fileName = entry.getName();
is = zf.getInputStream(entry);
if(entry.isDirectory()) {
File fd = new File(where + fileName);
if(!fd.exists()) fd.mkdirs();
} else {
String fileNameDir = fileName.substring(0, fileName.lastIndexOf("/"));
File fd = new File(where + fileNameDir);
if(!fd.exists()) fd.mkdirs();
out = new FileOutputStream(where + fileName);
while((bytes_read = is.read(buffer)) != -1) {
out.write(buffer, 0, bytes_read);
}
out.close();
}
}
is.close();
res = "true";
} catch(IOException e) {
res = "false";
e.printStackTrace();
}
return res;
}
}
'Java' 카테고리의 다른 글
lotto 당첨번호 파싱 (4) | 2010.04.07 |
---|---|
openCsv sample function (2) | 2010.04.07 |
AES java 암호화 구현 function sample (1) | 2010.04.07 |
PropertyLoader.java (2) | 2010.04.07 |
java 한글 인코딩 (swing) (1) | 2010.04.07 |