LANGUAGE/Java & Groovy 2017. 3. 7. 21:08


# Java (자바)


그루비(Groovy)로 코딩되었습니다.


조금만 수정하시면, 자바에서도 사용할 수 있습니다. :D



### Extract TAR (Untar/언타르)


1. Build.gradle

dependencies {
//GROOVY
compile 'org.codehaus.groovy:groovy-all:2.1.3'

//TAR
compile 'org.apache.commons:commons-compress:1.4.1'
}

2. xxx.groovy

public void untar(String filePath, String dest){
println ' - Start Untar'
byte[] buffer = new byte[2048]


try{
/** Ready **/
FileInputStream fin = new FileInputStream(filePath)
BufferedInputStream bis = new BufferedInputStream(fin)
GzipCompressorInputStream gzIn = new GzipCompressorInputStream(bis)
TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn)
TarArchiveEntry entry
/** Read the tar entries using the getNextEntry method **/
while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null){
File file = new File(dest, entry.getName())
println "Extracting: ${file.getAbsolutePath()}"
if (entry.isDirectory()){
file.mkdirs()
}else{
file.parentFile.mkdirs()
int len
FileOutputStream fos = new FileOutputStream(file)
BufferedOutputStream destOs = new BufferedOutputStream(fos, BUFFER)
while ((len = tarIn.read(buffer, 0, BUFFER)) != -1){
destOs.write(buffer, 0, len)
}
destOs.close()
}
}
/** Close the input stream **/
tarIn.close()
println "Untar Completed Successfully!!"

}catch(IOException ex){
ex.printStackTrace()
}
}




### Extract ZIP (Unzip/언집)

1. Build.gradle

dependencies {
//GROOVY
compile 'org.codehaus.groovy:groovy-all:2.1.3'
}

2. xxx.groovy

public void unzip(String sourcePath, String destPath){
println ' - Start Unzip'
byte[] buffer = new byte[2048]


try{
/** Ready **/
ZipInputStream zis = new ZipInputStream(new FileInputStream(sourcePath))
ZipEntry entry
/** Read the zip entries using the getNextEntry method **/
while ((entry = zis.getNextEntry()) != null){
File file = new File(destPath + File.separator + entry.getName())
println "Extracting: ${file.getAbsolutePath()}"
if (entry.isDirectory()){
file.mkdirs()
}else{
file.parentFile.mkdirs()
FileOutputStream fos = new FileOutputStream(file)
int len
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len)
}
fos.close()
}
}
/** Close the input stream **/
zis.closeEntry()
zis.close()
println "Unzip Completed Successfully!!"

}catch(IOException ex){
ex.printStackTrace()
}
}




### Extract JAR (Unjar/언자르)

1. Build.gradle

dependencies {
//GROOVY
compile 'org.codehaus.groovy:groovy-all:2.1.3'
}

2. xxx.groovy

public void unjar(String sourcePath, String destPath){
println ' - Start Unjar'
byte[] buffer = new byte[2048]


try{
/** Ready **/
java.util.jar.JarFile jar = new java.util.jar.JarFile(sourcePath)
java.util.Enumeration enumEntries = jar.entries()
/** Read the jar entries using the nextElement method **/
while (enumEntries.hasMoreElements()) {
java.util.jar.JarEntry entry = (java.util.jar.JarEntry) enumEntries.nextElement()
java.io.File file = new java.io.File(destPath + java.io.File.separator + entry.getName())
println "Extracting: ${file.getAbsolutePath()}"
if (entry.isDirectory()) {
file.mkdirs()
}else{
file.parentFile.mkdirs()
java.io.InputStream is = jar.getInputStream(entry)
java.io.FileOutputStream fos = new java.io.FileOutputStream(file)
int len
while ((len = is.read(buffer)) > 0) {
fos.write(buffer, 0, len)
}
fos.close()
is.close()
}
}
/** Close the input stream **/
jar.close()
println "Unjar Completed Successfully!!"

}catch(IOException ex){
ex.printStackTrace()
}


}




### 참고

How to write a Java program which can extract a JAR file and store its data in specified directory (location)?: 

http://stackoverflow.com/questions/1529611/how-to-write-a-java-program-which-can-extract-a-jar-file-and-store-its-data-in-s


'LANGUAGE > Java & Groovy ' 카테고리의 다른 글

[Java] 형형색색 색깔 출력 ( print )  (0) 2017.11.11
[Groovy] JDK버전별  (0) 2017.11.10
[Java] jar파일 실행하기  (0) 2017.03.07
[Java] System.getProperty  (0) 2017.03.05
[Java] JUnit (제이유닛)  (0) 2017.02.27