Comprimir un archivo en java

Hola a todos, estuve revisando unas cosas y me encontré con un código que realice ya hace algunos años para comprimir un archivo. Al revisarlo aún estaría vigente su forma de creación, es de aclarar que fue hecho en java por si les sirve


/**
*
* @param nombreDirectorio
* @param path -- Ruta del archivo a comprimir
*/
public void zipDir(String path) {

System.out.println(" -path " + path);

fileList = new ArrayList<String>();
generateFileList(new File(path), path);
zipIt(path);
}

/**
*
* @param node
* @param path
*/
public void generateFileList(File node, String path){

   if(node.isFile()) {
    fileList.add(generateZipEntry(node.toString(), path));
   }

   if(node.isDirectory()) {
    String[] subNote = node.list();  
 
    for(String filename : subNote){
    generateFileList(new File(node, filename), path);
    }
   }
 
}

/**
*
* @param file
* @param path
* @return
*/
private String generateZipEntry(String file, String path) {
return file.substring(path.length()+1, file.length());
}

/**
*
* @param path
*/
public void zipIt(String path) {
byte[] buffer = new byte[1024];
String source="";
try {
try {
source=path.substring(path.lastIndexOf("\\")+1,path.length());
}
catch(Exception e) {
source=path;
}



        FileOutputStream fos = new FileOutputStream(path + ".zip");
        ZipOutputStream zos = new ZipOutputStream(fos);

        System.out.println("Output to Zip : " + path);

        for(String file : this.fileList){

            System.out.println("File Added : " + file);
            ZipEntry ze= new ZipEntry(source+File.separator+file);
            zos.putNextEntry(ze);

            FileInputStream in = new FileInputStream(path + File.separator + file);

            int len;
            while ((len = in.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }

            in.close();
        }

        zos.closeEntry();      
        zos.close();

        System.out.println("Folder successfully compressed");
} catch(IOException ex) {
ex.printStackTrace();
}
}


Espero que les sirva y hasta una próxima oportunidad.

Comentarios