오늘의 포스팅은 자바에서 리눅스 명령어를 사용해 보는 것입니다.
바로 코딩을 시작해 보겠습니다.
if(System.getProperty("os.name").toLowerCase().indexOf("windows") != -1) {
//윈도우 환경(로컬)
} else {
//윈도우 환경아 아닌 리눅스 환경(개발)
//s_DownPath -> /home/framework/image/ 이미지가 있는 상위 폴더
String s_DownPath = EgovProperties.getProperty("DownPath");
//s_image_dir -> /home/framework/image/test/ 압축할 폴더
String s_image_dir = s_DownPath + "/test";
//s_image_dir -> /home/framework/image/temp.tpz 로 압축파일 생성
String s_image_tgz = s_DownPath + "/test.tgz";
try {
//s_source_dir 폴더의 img들을 s_source_tgz위치의 temp.tgz로 생성
String command = "tar cfv " + s_source_tgz + " -C " + s_source_dir + " ./";
//명령어 실행
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
//마지막에 실핼할 문장
}
}
첫번째줄 if문에서는 windows 환경에서는 실행하지 않기위해 조건문을 넣었습니다.
로컬환경는 OS가 windows이고 개발환경은 OS가 linux이기 때문입니다.
리눅스 명령어를 실행하기 위해서는 파일위치/파일명 과 압축할 대상 을 지정해야 합니다.
/home/framework/image/test/test01.jpg
/home/framework/image/test/test02.jpg
/home/framework/image/test/test03.jpg
test 폴더 안에 이미지가 3개 존재합니다.
/home/framework/image/test/test.tgz
/home/framework/image/test.tgz
테스트 이미지들을 어느 폴더에 압축을 할지 정해야 합니다.
test 폴더에 압축 파일을 압축할지 이미지 폴더에 압축 파일을 압축할지에 따라서 명령어가 조금씩 달라집니다.
/home/framework/image/test/test.tgz
String command = tar cfv /home/framework/image/test.tgz /home/framework/image/test/
위의 명령어로 실행한다면 test 폴더에 압축파일 test.tgz 가 생성됩니다.
근데 문제가 생겼습니다.
압축파일 test.tgz 를 서버에서 다운받아서 압축을 풀어봤더니
이런식으로 풀려 버리는 것입니다.
/home/framework/image/test/test01.jpg
/home/framework/image/test/test02.jpg
/home/framework/image/test/test03.jpg
제가 원하는 것은 test 폴더 안에 이미지들이 있도록 하는 것이였습니다.
/test/test01.jpg
/test/test02.jpg
/test/test03.jpg
그래서 명령어를 조금 바꿔봤습니다.
String command = tar cfv /home/framework/image/test.tgz -C /home/framework/image/ ./test/
image 폴더 아래 test.tgz 파일을 생성하고
test.tgz 를 압축을 풀었을때 test 폴더에 이미지들이 나열될 수 있도록 한다.
-C 명령어는 해당위치로 이동해서 test 폴더로 압축한다.
test폴더도 만들기 시르시면 ./ 하시면 됩니다.
/test/test01.jpg
/test/test02.jpg
/test/test03.jpg
여러분들도 command 명령어를 바꿔가면서 자바에서 실행해 보셨으면 좋겠군요
제가 작성한 글이 도움이 되셨다면 ♥공감을 꾹 눌러주세요
도움이 필요한 내용이 있으시다면 댓글도 남겨주세요
맵(Map) - 컬렉션 인터페이스 (0) | 2020.08.18 |
---|---|
자바 최상위 추상 클래스 inputStream (0) | 2020.05.20 |
자바 파일 압축 및 해제 자바 한글깨짐 현상 ( commons-compress 사용 ) (0) | 2020.05.14 |
File 클래스란 무엇인가? (1) | 2020.05.11 |
스트림(stream)이란 데이터의 흐름이다. (0) | 2020.05.08 |