zip 압축파일 푸는코딩을 구글링해서 구하여 작성하고
pc 에서압축한 zip 파일을 다운받아서
unzip 하는데 제대로 풀지를 못하는군요
여러개의 폴더와 파일들을 압축한파일인데
일부만 압축이 풀리고 종료되는것같습니다
어디서문제가 있는지 한번살펴봐주시면 감사하겠습니다
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
........................
/**
* Zip 파일의 압축을 푼다.
*
* @param zipFile - 압축 풀 Zip 파일
* @param targetDir - 압축 푼 파일이 들어간 디렉토리
* @param fileNameToLowerCase - 파일명을 소문자로 바꿀지 여부
* @throws Exception
*/
public static void unzip(String zipFile, String targetDir, boolean fileNameToLowerCase) throws Exception {
FileInputStream fis = null;
ZipInputStream zis = null;
ZipEntry zentry = null;
try {
fis = new FileInputStream(zipFile); // FileInputStream
zis = new ZipInputStream(fis); // ZipInputStream
while ((zentry = zis.getNextEntry()) != null) {
String fileNameToUnzip = zentry.getName();
if (fileNameToLowerCase) { // fileName toLowerCase
fileNameToUnzip = fileNameToUnzip.toLowerCase();
}
File targetFile = new File(targetDir, fileNameToUnzip);
if (zentry.isDirectory()) {// Directory 인 경우
//FileUtils.makeDir(targetFile.getAbsolutePath()); // 디렉토리 생성
File path = new File(targetFile.getAbsolutePath());
path.mkdirs();
} else { // File 인 경우
// parent Directory 생성
//FileUtils.makeDir(targetFile.getParent());
File path = new File(targetFile.getParent());
path.mkdirs();
unzipEntry(zis, targetFile);
}
}
} finally {
if (zis != null) {
zis.close();
}
if (fis != null) {
fis.close();
}
}
}
/**
* Zip 파일의 한 개 엔트리의 압축을 푼다.
*
* @param zis - Zip Input Stream
* @param filePath - 압축 풀린 파일의 경로
* @return
* @throws Exception
*/
protected 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) {
fos.write(buffer, 0, len);
}
} finally {
if (fos != null) {
fos.close();
}
}
return targetFile;
}