안드로이드 개발 질문/답변
(글 수 45,052)
zip 파일을 unzip시 zip내부에 있는 파일 하나만 풀어올수 있나요?
zip파일 내부에 있는 파일하나의 실제크기를 알고싶어서 입니다?
가능할까요? 가능하다면 어떤 api나 방법이 있나요?
고수님들 도와주세요 ^^;
2012.04.26 16:58:44
아래 소스에서 보시면 zis.getNextEntry() 로 트리 구조로 탐색을하는것 같은데요 특정 경로로 바로 접근해서 있는지 체크해서
그 파일만 풀수가 있을까요?
public class CUnzip {
final static int BUFFER_SIZE = 1024 * 1000;
public boolean UnzipStart(String fname ){
File zipFile = new File("/sdcard/urdir/filename.ZIP") ;
File targetDir = new File("/sdcard/dir/");
try {
return unzip(zipFile,targetDir);
} catch (Exception e) {
return false;
}
}
private boolean unzip(File zipFile, File targetDir) {
FileInputStream fis = null;
ZipInputStream zis = null;
ZipEntry zentry = null;
try {
try {
fis = new FileInputStream(zipFile);
zis = new ZipInputStream(fis);
while ((zentry = zis.getNextEntry()) != null) {
String fileNameToUnzip = zentry.getName();
File targetFile = new File(targetDir, fileNameToUnzip);
if (zentry.isDirectory()) {//if is Directory
File path = new File(targetFile.getAbsolutePath());
if (!path.isDirectory()) {
path.mkdirs();
}
} else { //if is File
File path = new File(targetFile.getParent());
if (!path.isDirectory()) {
path.mkdirs();
}
unzipEntry(zis, targetFile);
}
}
} finally {
if (zis != null) {
zis.close();
}
if (fis != null) {
fis.close();
}
}
} catch (Exception e) {
return false;
}
return true;
}
private static File unzipEntry(ZipInputStream zis, File targetFile) throws Exception {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(targetFile);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = zis.read(buffer)) != -1) {
if(len == 0){
return null;
}
fos.write(buffer, 0, len);
}
} finally {
if (fos != null) {
fos.close();
}
}
return targetFile;
}




답변하곤 관련없지만 근성체의 냄새가 물씬