Codigo:
package elrinconprograues.zip; import java.io.File; import java.util.ArrayList; import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.UnzipParameters; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.util.Zip4jConstants; /** * * @author Alex Callejas */ public class Zip { public void encryptedZipFile(String pass, String ruta, String archivo) { try { ZipFile zipFile = new ZipFile(ruta +".zip"); ArrayList filesToAdd = new ArrayList(); filesToAdd.add(new File(archivo)); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); parameters.setEncryptFiles(true); parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES); parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256); parameters.setPassword(pass); zipFile.addFiles(filesToAdd, parameters); } catch (ZipException e) { System.out.println(e.getMessage()); } } public void zipFile(String ruta, String archivo) { try { ZipFile zipFile = new ZipFile(ruta +".zip"); ArrayList filesToAdd = new ArrayList(); filesToAdd.add(new File(archivo)); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); parameters.setEncryptFiles(false); zipFile.addFiles(filesToAdd, parameters); } catch (ZipException e) { System.out.println(e.getMessage()); } } public void zipFolder(String ruta,String folder){ try { ZipFile zipFile=new ZipFile(ruta +".zip"); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); parameters.setEncryptFiles(false); zipFile.addFolder(folder, parameters); } catch (ZipException e) { System.out.println(e.getMessage()); } } public void zipFolderEncrypted(String ruta,String folder,String pass){ try { ZipFile zipFile=new ZipFile(ruta +".zip"); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); parameters.setEncryptFiles(true); parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES); parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256); parameters.setPassword(pass); zipFile.addFolder(folder, parameters); } catch (ZipException e) { System.out.println(e.getMessage()); } } public void unzip(String zip,String ruta){ try { ZipFile zipFile=new ZipFile(new File(zip)); zipFile.extractAll(ruta); } catch (ZipException e) { System.out.println(e.getMessage()); } } public void unzip(String zip,String ruta,String pass){ try { ZipFile zipFile=new ZipFile(new File(zip)); UnzipParameters unzipParameters= new UnzipParameters(); zipFile.setPassword(pass); zipFile.extractAll(ruta); } catch (ZipException e) { System.out.println(e.getMessage()); } } public static void main(String[] args) { Zip zip=new Zip(); //zip.zipFile("c:\\prueba\\pruebaZip", "c:\\prueba\\fichero.txt"); //zip.encryptedZipFile("password", "c:\\prueba\\pruebaZipPass", "c:\\prueba\\fichero.txt"); //zip.zipFolderEncrypted("c:\\prueba\\zipFolderPass", "c:\\prueba\\pruebaZip\\","password"); zip.unzip("c:\\prueba\\zipFolderPass.zip", "c:\\prueba\\pruebaZip\\cc\\","password"); // } }
Descarga código + librerías: MEGA