Ver Mensaje Individual
  #5 (permalink)  
Antiguo 13/03/2010, 14:09
jorgebee65
 
Fecha de Ingreso: septiembre-2009
Mensajes: 4
Antigüedad: 14 años, 7 meses
Puntos: 0
Respuesta: Convertir fichero a .zip

podrias hacer algo como esto:

import java.io.*;
import java.util.zip.*;

public class Zip {
static final int BUFFER = 2048;


public void Convertir(String nombreArchivo){
try {
BufferedInputStream origin = null;
String ext=nombreArchivo.substring(nombreArchivo.lastInde xOf("."));
System.out.println(ext);
String nombrezip= nombreArchivo.replace(ext,"");
FileOutputStream dest = new
FileOutputStream(nombrezip+".zip");
ZipOutputStream out = new ZipOutputStream(new
BufferedOutputStream(dest));
//out.setMethod(ZipOutputStream.DEFLATED);
byte data[] = new byte[BUFFER];
// get a list of files from current directory
File f = new File(nombreArchivo);

System.out.println("Adding: "+f);
FileInputStream fi = new
FileInputStream(f);
origin = new
BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(f.toString());
out.putNextEntry(entry);
int count;
while((count = origin.read(data, 0,
BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();

out.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}