개발Story
Published 2020. 3. 2. 17:20
zip utill java/유틸

records.


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

/**
 * 디렉토리 압축
 * @param path 압축 대상 경로
 * @param toPath 압축 파일이 생성될 위치
 * @param fileName 압축 파일명
 */
public static void createZipFile(String path, String toPath, String fileName) {

File dir = new File(path);
String[] list = dir.list();
String _path;

if(!dir.canRead() || !dir.canWrite())
return;

int len = list.length;

if(path.charAt(path.length() - 1) != '/')
_path = path + "/";
else
_path = path;

try {
ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(toPath + "/" + fileName), 2048));
for(int i = 0 ; i < len ; i ++) {
File file = new File(_path + list[i]);
zipFolder("", file, zipOut, file.getParent() + "\\");
}

zipOut.flush();
zipOut.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {

}
}

/**
 * 폴더단위 압축을 실행한다
 * 
 * @param parent 부모파일 Path
 * @param file
 * @param zOut
 * @param zipFromPath 압축 대상 루트 디렉토리
 * @throws IOException
 */
private static void zipFolder(String parent, File file, ZipOutputStream zOut, String zipFromPath) throws IOException {
byte[] data = new byte[2048];
int read;

if(file.isFile()) {
ZipEntry entry = new ZipEntry(parent + file.getName());
zOut.putNextEntry(entry);
BufferedInputStream  instream = new BufferedInputStream(new FileInputStream(file));

while((read = instream.read(data, 0 , 2048)) != -1)
zOut.write(data, 0, read);

zOut.flush();
zOut.closeEntry();
instream.close();

} else if (file.isDirectory()) {
String parentString = file.getPath().replace(zipFromPath, "");
parentString = parentString.substring(0, parentString.length() - file.getName().length());
ZipEntry entry = new ZipEntry(parentString + file.getName() + "/");
zOut.putNextEntry(entry);

String[] list = file.list();
if(list != null) {
int len = list.length;
for(int i = 0; i < len ; i ++) {
zipFolder(entry.getName(), new File(file.getPath() + "/" + list[i]), zOut, zipFromPath);
}
}
}
}

/**
 * 해당 파일을 압축 해제한다.
 * 
 * @param toPath 압축해제 대상 디렉토리
 * @param zipPath 압축파일 경로
 * @return 압축해제 성공유무
 */
public static boolean unzipFile(String toPath, String zipPath) {

boolean result = false;

byte[] data = new byte[2048];
ZipEntry entry = null;
ZipInputStream zipStream = null;
FileOutputStream out = null;

if(!(toPath.charAt(toPath.length() -1) == '/')) {
toPath += "/";
}

File directory = new File(toPath);
directory.mkdirs();

try {
zipStream = new ZipInputStream(new FileInputStream(zipPath));

while((entry = zipStream.getNextEntry()) != null) {

int read = 0;
File entryFile;

// 디렉토리일 경우 폴더를 생성
if(entry.isDirectory()) {
File folder = new File(toPath + entry.getName());
folder.mkdirs();
continue;
} else {
entryFile = new File(toPath + entry.getName());
}


if(!entryFile.exists()) {
entryFile.createNewFile();
}

out = new FileOutputStream(entryFile);
while((read = zipStream.read(data, 0 , 2048)) != -1)
out.write(data, 0 , read);

zipStream.closeEntry();
} 


} catch (FileNotFoundException  e) {
e.printStackTrace();
result = false;
} catch (IOException e) {
e.printStackTrace();
result = false;
} finally {

if(out != null) {

try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}

}

if(zipStream != null) {

try {
zipStream.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}

return result;
}

}
profile

개발Story

@슬래기

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!