Tieni... è una classe che controlla una cartella e se vengono aggiunti file zip li estrae... dovrebbe fare al caso tuo...
codice:import java.io.*; import java.util.*; import java.util.zip.*; import java.text.*; public class UnzipFiles { // DICHIARAZIONI VARIABILI E OGGETTI private Vector orig; private Vector mod; private String pathFolderZip; private String pathFolderUnzip; private String pathMoveFileZip; private int timeControl; private LogFile pathLog; private int esecuzioneProcedura = 1; // ESECUZIONE PRINCIPALE DEL PROGRAMMA public static void main(String[] args) { final UnzipFiles scompattaFiles = new UnzipFiles(); scompattaFiles.init(); Runnable r = new Runnable() { public void run () { Thread t = Thread.currentThread(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } scompattaFiles.monitorFolder(); } }; (new Thread (r)).start(); } // INIZIALIZZO VARIABILI public void init(){ // LETTURA DA FILE DI CONFIGURAZIONE try{ pathLog = new LogFile("TraceUnzip.log"); Properties props=new Properties(); props.load( new FileInputStream(new File("AutoUnzip.cfg"))); pathFolderZip = props.getProperty("pathFolderZip"); pathFolderUnzip = props.getProperty("pathFolderUnzip"); pathMoveFileZip = props.getProperty("pathMoveFileZip"); timeControl = Integer.parseInt(props.getProperty("timeControl")); // CARICO IL VETTORE CON I FILES ZIP orig = cercaFileZip(); }catch(Exception e5){ esecuzioneProcedura = 0; // SCRITTURA LOG try { pathLog.log("Errori durante la lettura del file di configurazione ",e5.getMessage()); } catch (IOException e1) { e1.printStackTrace(); } } } // RESTIUTISCO VETTORE CON LA LISTA DEI FILE ZIP IN pathFolder public Vector cercaFileZip(){ // INSTANZIO VETTORE PER LA RESTITUZIONE DEI FILES Vector returnValue = new Vector(); try{ // APRO CARTELLA ORIGINE File fromDir = new File(pathFolderZip); // TUTTI I FILES CONTENUTI IN CARTELLA ORIGINE File[] filesInDir = fromDir.listFiles(); // FACCIO SCORRERE TUTTI I FILE for(int i=0;i<filesInDir.length;i++){ // SE HA ESTENSIONE ZIP if (filesInDir[i].getName().endsWith(".zip")){ // LO AGGIUNGO NEL VETTORE returnValue.add(filesInDir[i]); } } }catch(Exception e){ // SCRITTURA LOG try { pathLog.log("Errori durante la lettura dei files da pathFolderZip ",e.getMessage()); } catch (IOException e1) { e1.printStackTrace(); } } return returnValue; } // CONTROLLO CICLICO DELLA CARTELLA public void monitorFolder() { Vector cambiamentiFolder; while (esecuzioneProcedura > 0){ // CARICO IL VETTORE PER IL CONTROLLO mod = new Vector(); mod = cercaFileZip(); // CONFRONTO cambiamentiFolder = new Vector(); cambiamentiFolder = trovaDifferenze(orig,mod); // CONTROLLO CAMBIAMENTI for(int a = 0;a < cambiamentiFolder.size();a++){ // RECUPERO FILE File fileZip = (File)cambiamentiFolder.elementAt(a); // SCRITTURA LOG try { pathLog.log("Nuovo File Zip Rilevato ",fileZip.getName()); } catch (IOException e1) { e1.printStackTrace(); } // UNZIP DEL FILE try { unzip(fileZip.getAbsolutePath()); // SCRITTURA LOG try { pathLog.log("Unzip del file in ",pathFolderUnzip); } catch (IOException e1) { e1.printStackTrace(); } } catch (IOException e2) { // SCRITTURA LOG try { pathLog.log("UNZIP DEL FILE NON RIUSCITO !!! ",e2.getMessage()); } catch (IOException e1) { e1.printStackTrace(); } } // SCRITTURA LOG try { pathLog.log("Nuovo path del file zip ",pathMoveFileZip+fileZip.getName()); } catch (IOException e1) { e1.printStackTrace(); } } // FINE CICLO FOR // ATTESA ESPRESSA IN MILLISECONDI try{ Thread.sleep(timeControl); }catch(InterruptedException e){ // SCRITTURA LOG try { pathLog.log("Errori durante tempo di attesa controllo cartella ",e.getMessage()); } catch (IOException e1) { e1.printStackTrace(); } } } // FINE WHILE } // RESTIUTISCO VETTORE CON LA LISTA DEI FILE ZIP IN pathFolder public Vector trovaDifferenze(Vector v1, Vector v2){ // INSTANZIO VETTORE PER LA RESTITUZIONE DELLE DIFFERENZE Vector returnValue = new Vector(); // FACCIO SCORRERE TUTTI I FILE DI v2 for(int i=0;i<v2.size();i++){ boolean fileTrovato = false; // CERCO IN v1 il FILE for(int a=0;a<v1.size();a++){ // SE LO TROVA ESCE DAL CICLO if(v2.elementAt(i).equals(v1.elementAt(a))){ fileTrovato = true; a=v1.size(); } } // SE NON L'HA TROVATO LO AGGINGO AL VETTORE DI DIFFERENZE if (fileTrovato==false){ returnValue.add(v2.elementAt(i)); } } return returnValue; } // FUNZIONE PER DECOMPRIMERE I FILES ZIP public void unzip(String pathFile) throws IOException { try { int BUFFER = 2048; BufferedOutputStream dest = null; BufferedInputStream is = null; ZipEntry entry; ZipFile zipfile = new ZipFile(pathFile); Enumeration e = zipfile.entries(); while(e.hasMoreElements()) { entry = (ZipEntry) e.nextElement(); is = new BufferedInputStream(zipfile.getInputStream(entry)); int count; byte data[] = new byte[BUFFER]; FileOutputStream fos = new FileOutputStream(pathFolderUnzip+entry.getName()); dest = new BufferedOutputStream(fos, BUFFER); while ((count = is.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); is.close(); zipfile.close(); } } catch(Exception e) { e.printStackTrace(); } } }


Rispondi quotando