¿Se puede subir y descomprimir un archivo *.zip al mismo tiempo?
Código PHP:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.ZipInputStream;
import static java.lang.System.out;
public class Prueba13 {
public static final int BUFFER = 2048;
public static final String path="C:/Users/alguien/Documents/NetBeansProjects/IntroGWT1/pruebaDescomprimir/descomprime/pruebaZip.zip";
public static final String destino="C:/temp/";
public static void main(String[] args)throws Exception{
Prueba13 pro=new Prueba13();
out.println("[Inicio descomprimir]");
out.println("Lee: "+path.toString());
//ver
pro.leer(path);
FileInputStream fis=new FileInputStream(path);
out.println("Descomprime:");
//descomprimir solo xml y pdf
pro.descomprimirGuardar(fis);
out.println("[Fin descomprimir]");
}
//descomprimir y guardar en directorio por default
public void descomprimirGuardar(FileInputStream fis){
try {
BufferedOutputStream dest = null;
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while((entry = zis.getNextEntry()) != null) {
// System.out.println("Extrayendo: " +entry);
//si no es directorio
if(!entry.isDirectory()){
if(esPDF(entry)){
out.println(entry+" >>> es pdf: "+esPDF(entry));
descomprime(entry,dest,zis);
}
if(esXML(entry)){
out.println(entry+" >>> es xml: "+esXML(entry));
descomprime(entry,dest,zis);
}
}
//si es directorio
if(entry.isDirectory()){
out.println("[Directorio encontrado]");
if(esPDF(entry)){
out.println(entry+" >>> es pdf: "+esPDF(entry));
descomprime(entry,dest,zis);
}
if(esXML(entry)){
out.println(entry+" >>> es xml: "+esXML(entry));
descomprime(entry,dest,zis);
}
}
}
zis.close();
}
catch(Exception e) {e.printStackTrace();}
}
//proceso para descomprimir
public void descomprime(ZipEntry entry,BufferedOutputStream dest,ZipInputStream zis) throws IOException{
//dest --> revisar el "destino" de los archivos descomprimidos
// String currentEntry = entry.getName();
// File destFile =new File(destino, currentEntry);
// File destinationParent = destFile.getParentFile();
// destinationParent.mkdirs();
//------------------------------------------------------------
try{
int count;
byte[] data = new byte[BUFFER];
FileOutputStream fos = new FileOutputStream(entry.getName());
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER))!= -1) {
dest.write(data, 0, count);
}
}catch(Exception exc){
}
dest.flush();
dest.close();
}
//solo lee el contenido, identificando pdf y/o xml
public void leer(String path){
try {
FileInputStream fis = new FileInputStream(path);
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry ze;
while((ze=zis.getNextEntry())!=null){
//si no es directorio
if(!ze.isDirectory()){
if(esPDF(ze)){
out.println(ze+" >>> es pdf: "+esPDF(ze));
}
if(esXML(ze)){
out.println(ze+" >>> es xml: "+esXML(ze));
}
}
//si es directorio
if(ze.isDirectory()){
out.println("[Directorio encontrado]");
if(esPDF(ze)){
out.println(ze+" >>> es pdf: "+esPDF(ze));
}
if(esXML(ze)){
out.println(ze+" >>> es xml: "+esXML(ze));
}
}
zis.closeEntry();
}
zis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//si es pdf
public boolean esPDF(ZipEntry arch){
if(arch.getName().endsWith("pdf"))
return true;
return false;
}
//si es xml
public boolean esXML(ZipEntry arch){
if(arch.getName().endsWith("xml"))
return true;
return false;
}
}