sobota 30. července 2016

Update file inside zip inside zip inside zip. Using Gradle, Groovy, Closure

Easy way to modify content of zip file inside scripts. Using Gradle/Groovy

short example:

dependencies {
classpath 'org.zeroturnaround:zt-zip:1.8'
classpath 'org.apache.commons:commons-io:1.3.2'
}
// ----------------------------
import org.apache.commons.io.FileUtils
import org.zeroturnaround.zip.ZipUtil
/**
*
* Extract zipFile into directory in temp, then call closure and give this directory as parameter to closure
* (closure update this directory), then pack this directory back into ZipFile
* Nice method :) */
private void extractZipAndThenPackItBack(File zipFile, Closure closure) {
def extractedDirectory
try {
//Extract
extractedDirectory = getTempDir()
ZipUtil.unpack(zipFile, extractedDirectory);
//Call closure
closure.call(extractedDirectory)
//Pack result back
zipFile.delete()
ZipUtil.pack(extractedDirectory, zipFile);
} finally {
FileUtils.deleteDirectory(extractedDirectory)
}
}
//Sample usage:
extractZipAndThenPackItBack(zipFile) { File directoryWhereZipWasExtracted ->
//update directory where zip was extracted
}

And here is full sample project: https://bitbucket.org/bugs_/samples/src/default/ZipUpdaterGroovyGradleClosure/

čtvrtek 28. července 2016

Count number bytes in UTF-8 for Java String

https://bitbucket.org/bugs_/utils/src/default/CoreUtils/src/main/java/cz/vondr/coreutils/utf/Utf8LengthUtil.java

  • Problem 1: You have Java String variable. And you need get number of bytes in UTF-8
  • Problem 2: You need split String into multiple Strings, in the way that each of them have exact number of bytes. (expect last part of course :) )

Thanks to stackoverflow.com