| HOME | FORUM | INDICE |

download EstraiContenutoZip.java

/*
    L'esempio mostra come estrarre il contenuto di un file ZIP.

    Autore: Agosto 2009, Giovanni Iacobelli - http://www.iacosoft.com

*/


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

class EstraiContenutoZip
{
    static String nomeFile= "c:\\testzipjava.zip";

    /*
        punto d'ingresso dell'applicazione
    */

    public static void main(String[] args)
    {
        try
        {
            //il contenuto verra' decompresso e memorizzato nella cartella unzip presente
            //nella cartella corrente
            String Root= System.getProperty("user.dir") + "\\unzip";

            //se la directory unzip non esiste verra' creata
            File dir= new File(Root);
            if (!dir.exists())
            {
                dir.mkdir();
            }
            
            FileInputStream pf= new FileInputStream(nomeFile);
            ZipInputStream zip= new ZipInputStream(pf);
            ZipEntry f;
            String cartelle;
            String nomeFile;
            String tmp;
            String pathname_relativo;

            int pos= 0;
            while ( (f= zip.getNextEntry()) != null)
            {
                
                /* CREA LA STRUTTURA DELLE CARTELLE SE NON ESISTONO */
                nomeFile= f.getName();
                pos= nomeFile.indexOf(":");
                if ( pos >= 0)
                {
                    nomeFile= nomeFile.substring(pos +2);
                }    
                nomeFile= nomeFile.replace("/", "\\");
                
                pathname_relativo= nomeFile;

                pos= nomeFile.lastIndexOf("\\");
                if (pos >= 0)
                {
                    cartelle= nomeFile.substring(0, pos);
                    nomeFile= nomeFile.substring(pos +1);
                } else
                    cartelle= "";

                tmp= "";
                while (cartelle.length() > 0)
                {
                    if (tmp.length() > 0)
                        tmp += "\\";
                    
                    pos= cartelle.indexOf("\\");
                    if (pos > 0)
                    {
                        tmp += cartelle.substring(0, pos);
                        cartelle= cartelle.substring(pos+1);
                    } else
                    {
                        tmp += cartelle;
                        cartelle= "";
                    }

                    dir= new File(Root + "\\" + tmp);
                    if (!dir.exists())
                    {
                        dir.mkdir();
                    }
                }
                /* FINE CREAZIONE STRUTTURA CARTELLE */
                
                
                if (!f.isDirectory())
                {
                    System.out.println("decompressione di " + pathname_relativo + "...");

                    byte[] buf= new byte[1024];

                    int r= 0;
                    
                    FileOutputStream dest= new FileOutputStream(Root + "\\" + pathname_relativo);

                    //legge a blocchi di 1 KB */
                    while ((r= zip.read(buf,0, buf.length)) > 0)
                    {
                        dest.write(buf,0,r);
                    }
                    dest.close();
                    System.out.println("OK.");
                }
                

                zip.closeEntry();
            }
            
            zip.close();

            pf.close();
        } catch(Exception e)
        {
            System.err.println(e);
        }
    }
}