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;
}
}
'java > 유틸' 카테고리의 다른 글
템플릿 다운로드. (0) | 2020.04.29 |
---|---|
File 저장하기. (0) | 2020.03.02 |
[전자정부 프레임워크] 중복로그인 방지. (0) | 2019.12.15 |
Excel로 올라온 19리가 넘는 숫자데이터 controller에서 받기 (0) | 2019.12.15 |
싱글톤 패턴,인스턴스 ,클래스, 객체 (0) | 2019.02.21 |