Compress a complete Zip folder and enter the name of which folder will be compressed. JAVA

1

I am doing a program that compresses zip file, but it does not compress the folders, only individual files and what interests me is compressing folders, I would also like to know how the program asks for the name of the folder that you have to compress and not have to enter it in the code one by one.

Annex the code that I have:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Conversor {

    public static void main(String[] args) {
        // cadena que contiene la ruta donde están los archivos a comprimir
        String directorioZip = "C:\ZIP\";
        // ruta completa donde están los archivos a comprimir
        File carpetaComprimir = new File(directorioZip);

        // valida si existe el directorio
        if (carpetaComprimir.exists()) {
            // lista los archivos que hay dentro del directorio
            File[] ficheros = carpetaComprimir.listFiles();


            // ciclo para recorrer todos los archivos a comprimir
            for (int i = 0; i < ficheros.length; i++) {
                System.out.println("Nombre del fichero: " + ficheros[i].getName());
                String extension="";
                for (int j = 0; j < ficheros[i].getName().length(); j++) {
                    //obtiene la extensión del archivo
                    if (ficheros[i].getName().charAt(j)=='.') {
                        extension=ficheros[i].getName().substring(j, (int)ficheros[i].getName().length());
                        //System.out.println(extension);
                    }
                }
                try {
                    // crea un buffer temporal para ir poniendo los archivos a comprimir
                    ZipOutputStream zous = new ZipOutputStream(new FileOutputStream(directorioZip + ficheros[i].getName().replace(extension, ".zip")));

                    //nombre con el que se va guardar el archivo 
                    ZipEntry entrada = new ZipEntry(ficheros[i].getName());
                    zous.putNextEntry(entrada);



                        //obtiene el archivo para comprimir
                        FileInputStream fis = new FileInputStream(directorioZip+entrada.getName());
                        int leer;
                        byte[] buffer = new byte[1024];
                        while (0 < (leer = fis.read(buffer))) {
                            zous.write(buffer, 0, leer);
                        }
                        fis.close();
                        zous.closeEntry();
                    zous.close();                   
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }               
            }
            System.out.println("Directorio de salida: " + directorioZip);
        } else {
            System.out.println("No se encontró el directorio..");
        }
    }
}
    
asked by HugoCo 01.06.2017 в 19:30
source

1 answer

0

I use zip4j an open source library that does everything necessary in terms of zip, I fully recommend it, it also allows password encryption, etc. link

To compress a folder (and everything it contains ... without having to work with recursion), simply:

String pathZipInput = "C:/..."; // carpeta a comprimir
String pathOutputZip = "C:/..."; // ruta y nombre del zip que se genera
try {
    ZipFile zipFile = new ZipFile(pathOutputZip);               
    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);        
    zipFile.addFolder(pathZipInput, parameters); 
    // en la linea superior cambiar Folder por File si es archivo o carpeta
} catch (ZipException e) {
    JOptionPane.showMessageDialog(null, "Error: " + e.getMessage());
    e.printStackTrace();
}

In my humble opinion, the best thing for zip that there is, I do not know if it will be the answer to look for but to have an eye on it.

    
answered by 01.06.2017 / 19:59
source