Zip and UnZip files using Groovy
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() |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
nice) |
This comment has been minimized.
This comment has been minimized.
very good Successfully started process 'command '~/.gradle/caches/jars-3/b904ae6b5680741fdcde1a177998dc6f/kotlinc/bin/kotlinc''
错误: 找不到或无法加载主类 org.jetbrains.kotlin.preloading.Preloader
...
FAILURE: Build failed with an exception. using code below with AntBuilder which aapt/kotlinc file can be executed success /**
* https://relentlesscoding.com/2017/08/04/using-groovys-antbuilder-to-zip-and-unzip-files/
* https://stackoverflow.com/questions/645847/unzip-archive-with-groovy
* @param zipFileName
* @param outputDir
* @return
*/
static boolean unzipV2(String zipFileName, String outputDir) {
try {
def ant = new AntBuilder()
ant.unzip(
src: zipFileName,
dest: outputDir,
overwrite: "true"
)
return true
} catch (Exception e) {
e.printStackTrace()
return false
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.