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..");
}
}
}