* @param inputFolder = 압축할 폴더의 위치
* @param filePath = 압축파일을 생성시킬 위치
* @param zipName = 압축파일의 이름
* 이렇게 인자값으로 받지 않고 메소드안에서 설정해도 무방합니다.
*/
public static void zip(String inputFolder, String filePath, String zipName)
throws Exception {
FileOutputStream fileOutputStream = null;
File file = new File(filePath+File.separator+"zipName");
fileOutputStream = new FileOutputStream(file);
ZipOutputStream zipOutputStream =
new ZipOutputStream(fileOutputStream);
File inputFile = new File(inputFolder);
if(inputFile.isFile()) {
zipFile(inputFile)
} else if(inputFile.isDirectory()) {
zipFolder(zipOutputStream,inputFile,"");
}
zipOutputStream.close();
fileOutputStream.close();
}
public static void zipFolder(ZipOutputStream zipOutputStream, File inputFile,
String parentName) throws Exception {
String myName = parentName+inputFolder.getName()+File.separator;
ZipEntry folderZipEntry = new ZipEntry(myName);
zipOutputStream.putNextEntry(folderZipEntry);
File[] contents = inputFile.listFiles();
for(File file : contents) {
if(file.isFile()) {
zipFile(file,myName,zipOutputStream);
} else if(file.isDirectory()) {
zipFolder(zipOutputStream,file,myName);
}
zipOutputStream.closeEntry();
}
}
public static void zipFile(File inputFile, String parentName,
ZipOutputStream zipOutputStream) throws Exception {
ZipEntry zipEntry = new ZipEntry(parentName+inputFile.getName());
zipOutputStream.putNextEntry(zipEntry);
FileInputStream fileInputStream = new FileInputStream(inputFile);
byte[] buf = new byte[4096];
int byteRead;
while((byteRead = fileInputStream.read(buf))>0) {
zipOutputStream.write(buf,0,bytesRead);
}
zipOutputStream.closeEntry();
fileInputStream.close();
}