Ver Mensaje Individual
  #4 (permalink)  
Antiguo 23/05/2013, 11:08
eduardomxm
 
Fecha de Ingreso: abril-2010
Ubicación: Cancun
Mensajes: 88
Antigüedad: 14 años
Puntos: 2
Respuesta: Crear zip en java que funcione en php

Código:
public void CrearZIP (){
		try {
			//name of zip file to create
			String outFilename = "D:\destino\miarchivo.zip";
			
			//create ZipOutputStream object
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
			
			//path to the folder to be zipped
			File zipFolder = new File("D:\destino\imagenes/");

			addFolderToZip(zipFolder, out);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void addFolderToZip(File folder, ZipOutputStream zip) throws IOException {
		URI base = folder.toURI();
		Deque<File> queue = new LinkedList<File>();
	    queue.push(folder);
	    Closeable res = zip;
	    try{
	    	while (!queue.isEmpty()) {
	    		folder = queue.pop();
	            for (File kid : folder.listFiles()) {
	            	String name = base.relativize(kid.toURI()).getPath();
	            	if (kid.isDirectory()) {
	                    queue.push(kid);
	                    name = name.endsWith("/") ? name : name + "/";
	                    //JOptionPane.showMessageDialog(null,name);
	                    zip.putNextEntry(new ZipEntry(name));
	                  } else {
	                	zip.putNextEntry(new ZipEntry(name));
	                	//JOptionPane.showMessageDialog(null,name);
	                    copy(kid, zip);
	                    zip.closeEntry();
	                  }
	            }
	    	}
	    }
	    finally{
	    	res.close();
	    }
	}
	private static void copy(InputStream in, OutputStream out) throws IOException {
	    byte[] buffer = new byte[1024];
	    while (true) {
	      int readCount = in.read(buffer);
	      if (readCount < 0) {
	        break;
	      }
	      out.write(buffer, 0, readCount);
	    }
	  }

	  private static void copy(File file, OutputStream out) throws IOException {
	    InputStream in = new FileInputStream(file);
	    try {
	      copy(in, out);
	    } finally {
	      in.close();
	    }
	  }

	  private static void copy(InputStream in, File file) throws IOException {
	    OutputStream out = new FileOutputStream(file);
	    try {
	      copy(in, out);
	    } finally {
	      out.close();
	    }
	  }