Google

miércoles, 30 de enero de 2008

Generar archivo ZIP

public void generarZip(){
String[] filenames = new String[]{"D:\\01.java", "D:\\02.java","D:\\03.txt"};
// Crear un bufer para leer los archivos
byte[] buf = new byte[1024];
try {
// Crear el archivo ZIP
String outFilename = "D:\\outfile.zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
// Comprimir los archivos
for (int i=0; i FileInputStream in = new FileInputStream(filenames[i]);
// Agregar las entradas ZIP al outputstream.
out.putNextEntry(new ZipEntry(filenames[i]));
// Transferencia de bytes desde el archivo original al archivo ZIP
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
}
catch (IOException e) {

}
}

No hay comentarios: