EyeTSS/radar/src/main/java/kr/gmtc/tss/util/FileUtil.java

225 lines
6.2 KiB
Java

package kr.gmtc.tss.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.springframework.stereotype.Component;
@Component
public class FileUtil {
// @Value("${file-zip-path}")
// private String fileZipPath;
//
// @Value("${file-zip-bak-path}")
// private String fileZipBakPath;
//
// @Value("${file-make-time}")
// private int fileMakeTime;
// public static String FILE_LOG_NAME;
/**
* 로그파일을 압축한다.
* 라이브 데이터 압축을 피하기 위해 20분전 데이터를 압축하는 것으로 세팅
*/
public void createZipFile(String fileZipPath, String fileZipBakPath, int fileMakeTime) {
// 압축파일이 저장될 폴더가 존재하는지 확인
File backDir = new File(fileZipBakPath);
if(!backDir.exists()) {
backDir.mkdirs();
}
SimpleDateFormat fileFormatter = new SimpleDateFormat("yyyyMMddHHmm");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, -20); // 20분전 데이터 확인
String time20 = fileFormatter.format(cal.getTime());
// 압축할 파일 명 확인.
String bakFileName = "file."+time20+".log";
String fileName = "";
ZipOutputStream zip_out = null;
// 압축 파일 명 세팅
StringBuilder sb = new StringBuilder();
sb.append(bakFileName);
sb.append(".zip");
fileName = sb.toString();
String _path = fileZipPath+"/"+bakFileName;
File bakFile = new File(_path);
// 파일이 존재할 경우에만 실행.
if(bakFile.exists()) {
if (fileZipPath.charAt(fileZipPath.length() - 1) != '/') {
_path = _path + "/";
}
try {
zip_out = new ZipOutputStream(new FileOutputStream(fileZipBakPath+"/"+fileName));
zipFolder("", bakFile, zip_out);
zip_out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
} else {
System.out.println("파일이 존재하지 않습니다.");
}
}
/**
* ZipOutputStream를 넘겨 받아서 하나의 압축파일로 만든다.
* @param parent 상위폴더명
* @param file 압축할 파일
* @param zout 압축전체스트림
* @throws IOException
*/
private void zipFolder(String parent, File file, ZipOutputStream zout) throws IOException {
byte[] data = new byte[2048];
int read;
BufferedInputStream instream = new BufferedInputStream(new FileInputStream(file));
ZipEntry zipEntry = new ZipEntry(parent + file.getName());
zout.putNextEntry(zipEntry);
while ((read = instream.read(data, 0, 2048)) != -1) {
zout.write(data, 0, read);
}
zout.flush();
zout.closeEntry();
instream.close();
file.delete(); // 파일 삭제
}
private void bakFileDelete(String fileZipPath, String date) throws IOException {
File folder = new File(fileZipPath+"/"+date); // file 생성
try {
while(folder.exists()){
File[] files = folder.listFiles();
for(File file : files){
file.delete(); // 하위 파일 삭제
}
// 폴더 삭제
if(files.length == 0 && folder.isDirectory()){ // 하위 파일이 없는지와 폴더인지 확인 후 폴더 삭제
folder.delete(); // 대상폴더 삭제
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
/**
* 로그파일을 생성하는 함수
*/
public String createLogFile(String fileZipPath, String fileZipBakPath, int fileMakeTime) {
File fileDir = new File(fileZipPath);
if(!fileDir.exists()) {
fileDir.mkdirs();
}
// 현재 시각 중에 분을 구해온다. (minute)
SimpleDateFormat formatter = new SimpleDateFormat("mm");
String _path = "";
Date now = new Date();
String nowTime = formatter.format(now);
int time = Integer.parseInt(nowTime);
String nowT = "";
// 현재 분이 설정한 값의 배수인지 확인한다.
if(time%fileMakeTime == 0){
if(time == 0) {
nowT = "00";
}else{
nowT = String.valueOf(time);
}
}else{
if(time < fileMakeTime) {
nowT = "00";
}else{
nowT = nowTime.substring(0, 1)+"0";
}
}
// 생성할 파일명을 설정한다.
SimpleDateFormat fileFormatter = new SimpleDateFormat("yyyyMMddHH");
String fileTime = fileFormatter.format(now) + nowT;
try {
_path = fileZipPath+"/file."+fileTime+".log";
// static 변수에 값을 세팅한다.
//FILE_LOG_NAME = _path;
File file = new File(_path);
File parentDir = file.getParentFile();
if (!parentDir.exists()) {
parentDir.mkdirs();
}
if (file.createNewFile()) {
System.out.println("파일 생성 완료");
} else {
System.out.println("파일 이미 존재");
}
} catch (IOException e) {
e.printStackTrace();
}
return _path;
}
/**
* 파일에 메세지를 쓰는 함수
* @param msg
* @throws Exception
*/
public static void writeLogFile(String msg, String fileName) throws Exception{
// File file = new File(FILE_LOG_NAME);
File file = new File(fileName);
// 파일이 존재할 경우에만 실행
if(file.exists()) {
FileWriter fw;
try {
fw = new FileWriter(file, true);
fw.write(msg);
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}else {
System.out.println("파일 없음 파일명 확인 필요.");
}
}
}