2011. 10. 17. 17:37
package com.xxx.xxx;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
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;
}
}
'Programming > Android' 카테고리의 다른 글
Android - Message에 Stirng 데이터 담아 핸들러에 sendMessage 하기 (2) | 2011.10.24 |
---|---|
Android - taking a phone screenshot from a computer / 폰화면 컴퓨터에서 스크린샷 찍기 (0) | 2011.10.20 |
Android - Down file using FTP/FTP방식으로 파일 다운 (0) | 2011.10.17 |
Android - Down file using http/http방식으로 파일 다운 (0) | 2011.10.17 |
Android - Methods GET and POST in HTML forms//http전송 get방식, post방식 (0) | 2011.10.17 |