import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import net.sf.jazzlib.GZIPInputStream;
import net.sf.jazzlib.GZIPOutputStream;
public class GZipUtils {
/**
* 지정된 폴더를 Zip 파일로 압축한다.
*
* @param sourceFileName
* - 압축 대상 파일
* @param outputFileName
* - 확장자가 gz
* @param encoding 파일의 encoding EUC-KR, UTF-8 등..
* @throws Exception
*/
public static void zip(String sourceFileName, String outputFileName,String encoding)
throws Exception {
Reader reader = null;
Writer writer = null;
try {
InputStream inputStream = new FileInputStream(sourceFileName);
reader = new BufferedReader(new InputStreamReader(inputStream,encoding));
writer = new BufferedWriter(new OutputStreamWriter(
new GZIPOutputStream(new FileOutputStream(outputFileName)), encoding));
int bytes_read;
while ((bytes_read = reader.read()) != -1 ) {
writer.write(bytes_read);
}
writer.flush();
}catch(Exception e) {
throw e;
}finally{
if(reader != null) reader.close();
if(writer != null) writer.close();
}
}
/**
* GZip 파일의 압축을 푼다.
*
* @param gzipFileName
* - 압축 풀 GZip 파일명
* @param targetFileName
* - 압축 푼 파일명
* @param encoding 파일의 encoding EUC-KR, UTF-8 등..
* @param fileNameToLowerCase
* - 파일명을 소문자로 바꿀지 여부
* @throws Exception
*/
public static void unzip(String gzipFileName, String targetFileName,String encoding)
throws Exception {
Reader reader = null;
Writer writer = null;
try {
reader = (new InputStreamReader(new GZIPInputStream(new FileInputStream(gzipFileName)),encoding));
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFileName), encoding));
int bytes_read;
while ((bytes_read = reader.read()) != -1 ) {
writer.write(bytes_read);
}
writer.flush();
} catch (Exception ex) {
throw ex;
} finally {
if(reader != null) reader.close();
if(writer != null) writer.close();
}
}
}
한글깨짐때문에 고생하고 있었는데 많은 도움이 되었습니다.
답글삭제감사합니다.