파일 압축 및 압축 해제
7414 단어 압축 풀기
public class ZipFileUtil {
private static final String ZIP_FORMAT = "zip";
private static final String SYMBOL_POINT = ".";
private static final String SYMBOL_SLASH = "/";
/**
*
*
* @param zipFileName
* @param inputFile
* @return isSuccess
*/
public static boolean zipFiles(String[] inputFiles,String zipFileName){
boolean isSuccess = false;
//
if (inputFiles == null || !isZipFile(zipFileName)) {
return false;
}
ZipOutputStream zoStream = null;
FileInputStream fiStream = null;
FileOutputStream foStream = null;
try {
foStream = new FileOutputStream(zipFileName);
zoStream = new ZipOutputStream(foStream);
for (int i = 0; i < inputFiles.length; i++) {
fiStream = new FileInputStream(inputFiles[i]);
ZipEntry zipEntry = new ZipEntry(getFileName(inputFiles[i]));
zoStream.putNextEntry(zipEntry);
StreamUtil.io(fiStream, zoStream);
fiStream.close();
}
zoStream.closeEntry();
isSuccess = true;
} catch (FileNotFoundException e) {
//log
return false;
} catch (IOException e) {
//log
return false;
} finally {
if (fiStream != null) {
try {
fiStream.close();
} catch (IOException e) {
//log
}
}
if (zoStream != null) {
try {
zoStream.close();
} catch (IOException e) {
//log
}
}
if (foStream != null) {
try {
foStream.close();
} catch (IOException e) {
//log
}
}
}
return isSuccess;
}
/**
* zip
*
* @param filePath
* @return
*/
private static boolean isZipFile(String filePath) {
if (StringUtil.isBlank(filePath)) {
return false;
}
String fileExtension = StringUtil.substringAfterLast(filePath, SYMBOL_POINT);
return ZIP_FORMAT.equalsIgnoreCase(fileExtension);
}
/**
*
*
* @param pathName
* @return
*/
private static String getFileName(String filePath) {
if (StringUtil.isBlank(filePath)) {
return null;
}
boolean isFile = StringUtil.contains(filePath, SYMBOL_SLASH);
if (isFile) {
return StringUtil.substringAfterLast(filePath, SYMBOL_SLASH);
} else {
return filePath;
}
}
/**
* ZIP ZIP --> files
*
* @param destZipFile
* @param unzipDir
* @return List<String> , size 0
*/
public static List<String> unzipFiles(String unzipDir, String destZipFile) {
List<String> resultFileList = new ArrayList<String>();
if (!isZipFile(destZipFile)) {
return resultFileList;
}
String unzipSubDir = getUnZipSubDir(unzipDir);
try {
FileInputStream in = new FileInputStream(destZipFile);
unzipStreamToFiles(in, unzipSubDir, resultFileList);
} catch (FileNotFoundException e) {
//log
return resultFileList;
}
return resultFileList;
}
private static String getUnZipSubDir(String unzipDir) {
File fileDir = new File(unzipDir);
int fileNum = fileDir.list().length;
int subDirNum = 0;
String subDir = null;
for(int i=0;i<fileNum;i++){
if(fileDir.listFiles()[i].isDirectory()){
subDirNum++;
}
}
subDir = Integer.toString(subDirNum+1);
if(subDirNum<10){
subDir = "0"+subDir;
}
unzipDir = unzipDir+File.separator+subDir+File.separator;
fileDir = new File(unzipDir);
if (!fileDir.exists()) {
new File(unzipDir).mkdir();
}
return unzipDir;
}
/**
* zip , resultFileList resultFileList
* , ,
*
* @param zipedStream zip (NOT NULL)
* @param unzipDir (NOT NULL)
* @param resultFileList (NOT NULL)
* @return
*/
public static boolean unzipStreamToFiles(InputStream zipedStream, String unzipDir,
List<String> resultFileList) {
if (zipedStream == null || StringUtil.isBlank(unzipDir) || resultFileList == null) {
return false;
}
File dir = new File(unzipDir);
if (!dir.exists()) {
dir.mkdirs();
}
resultFileList.clear();
ZipInputStream ziStream = null;
FileOutputStream foStream = null;
ziStream = new ZipInputStream(zipedStream);
ZipEntry zipEntry;
try {
zipEntry = ziStream.getNextEntry();
while (zipEntry != null) {
String fileName = getFileName(zipEntry.getName());
String destFile = unzipDir + fileName;
foStream = new FileOutputStream(destFile);
StreamUtil.io(ziStream, foStream);
foStream.close();
ziStream.closeEntry();
zipEntry = ziStream.getNextEntry();
resultFileList.add(destFile);
}
} catch (IOException e) {
//log
} finally {
if (foStream != null) {
try {
foStream.close();
} catch (IOException e) {
//log
}
}
if (ziStream != null) {
try {
ziStream.close();
} catch (IOException e) {
//log
}
}
}
return true;
}
}