-
-
Save krmao/a416eb32412b0082b3609faf8b927279 to your computer and use it in GitHub Desktop.
Zip and UnZip files using Groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.zip.* | |
String zipFileName = "file.zip" | |
String inputDir = "logs" | |
def outputDir = "zip" | |
//Zip files | |
ZipOutputStream zipFile = new ZipOutputStream(new FileOutputStream(zipFileName)) | |
new File(inputDir).eachFile() { file -> | |
//check if file | |
if (file.isFile()){ | |
zipFile.putNextEntry(new ZipEntry(file.name)) | |
def buffer = new byte[file.size()] | |
file.withInputStream { | |
zipFile.write(buffer, 0, it.read(buffer)) | |
} | |
zipFile.closeEntry() | |
} | |
} | |
zipFile.close() | |
//UnZip archive | |
def zip = new ZipFile(new File(zipFileName)) | |
zip.entries().each{ | |
if (!it.isDirectory()){ | |
def fOut = new File(outputDir+ File.separator + it.name) | |
//create output dir if not exists | |
new File(fOut.parent).mkdirs() | |
def fos = new FileOutputStream(fOut) | |
//println "name:${it.name}, size:${it.size}" | |
def buf = new byte[it.size] | |
def len = zip.getInputStream(it).read(buf) //println zip.getInputStream(it).text | |
fos.write(buf, 0, len) | |
fos.close() | |
} | |
} | |
zip.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment