String > byte[] > 압축 > byte[] ==[전송]==> byte[] > 압축 해제 > byte[] > String
String이 네트워크에서 전송이 된다면 1byte 문자세트를 이용하는 것이 바람직하다.
String > byte[] > 압축 > byte[] > String ==[전송]==> String > byte[] > 압축 해제 > byte[] > String문제는
new String(baos.toByteArray(), "ISO-8859-1") String ==[전송]==>String : input.getBytes("ISO-8859-1")
입력된 String은 한글이 포함된 UTF-8이라고 가정하여 압축 해제 후에 한글이 깨지지 않게 UTF-8 잊지 마세요....
public static String compress(String input) throws Exception {
String result = null;
if (input != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzipos = new GZIPOutputStream(baos);
gzipos.write(input.getBytes());
gzipos.close();
result = new String(baos.toByteArray(), "ISO-8859-1");
baos.close();
}
return result;
}
public static String decompress(String input) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(input.getBytes("ISO-8859-1"));
GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream);
BufferedInputStream bufferedInputStream = new BufferedInputStream(gzipInputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while((length = bufferedInputStream.read(buffer,0,1024)) > 0) {
byteArrayOutputStream.write(buffer, 0, length);
}
bufferedInputStream.close();
gzipInputStream.close();
byteArrayInputStream.close();
byteArrayOutputStream.close();
return byteArrayOutputStream.toString("UTF-8");
}
'개발 거들기' 카테고리의 다른 글
해당월의 마지막 날짜 가져오기 (0) | 2017.02.22 |
---|---|
소스를 블로그에 올릴 때 (0) | 2016.05.12 |
ATOM에서 TODO관리하기 (0) | 2015.12.21 |
git 로컬 저장소 생성(.git 폴더) (0) | 2015.01.14 |
git 수정사항 확인 (0) | 2015.01.14 |