Secciones

Zipping using password protection with Groovy

Inicio » Artículos » Zipping using password protection with Groovy
La categoría del artículo es
Escrito el 11 September 2015, 19:46


If you need to zip a bunch of files using password protection in Groovy (and of course in Java), there is a very nice library called zip4j that you can embed in your projects.


If you need to zip a bunch of files using password protection in Groovy (and of course in Java), there is a very nice library called zip4j that you can embed in your projects. Here you can find a working example to show you how to get it:

Groovy:
@Grapes(
    @Grab(group='net.lingala.zip4j', module='zip4j', version='1.3.2')
)

import net.lingala.zip4j.core.ZipFile
import net.lingala.zip4j.model.ZipParameters
import net.lingala.zip4j.util.Zip4jConstants

String compressedFile = "myfile.zip"
String inputFile1 = "file1.txt"
String inputFile2 = "file2.txt"

ZipFile zipFile = new ZipFile(compressedFile)

ArrayList<File> filesToAdd = new ArrayList<File>();
filesToAdd.add(new File(inputFile1))
filesToAdd.add(new File(inputFile2))
           
ZipParameters parameters = new ZipParameters()

parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE)
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL)
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD)
parameters.setEncryptFiles(true)
parameters.setPassword("mypassword" as char[])

zipFile.addFiles(filesToAdd, parameters)
 


Espero tus comentarios...

Ayuda Textile