Oggi vediamo come gestire in modo abbastanza semplice gli zip attraverso il Java.
Prima di tutto creiamo il nostro progetto dove aver fatto cioè creiamo 2 o piu file di test
nella cartella del progetto(1.txt,2.txt).
Ok dopo aver fatto tutto ciò creo la mia prima classe e la nomino Zippa.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
//Importo librerie import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Zippa { public static void main(String[] args) { //File che includiamo nel nostro zip String[] source = new String[]{"1.txt", "2.txt"}; // Creo un buffer per leggere i file byte[] buf = new byte[1024]; try { // Creo lo zip String target = "Zippato.zip"; ZipOutputStream out = new ZipOutputStream(new FileOutputStream(target)); // Compresso i files for (int i=0; i<source.length; i++) { FileInputStream in = new FileInputStream(source[i]); //Aggiungo la voce zip al stream output out.putNextEntry(new ZipEntry(source[i])); //Metto i byte del file nello zip int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } //Completo la trasformazione out.closeEntry(); in.close(); } //Chiudo il file stream output out.close(); System.out.println("Creazione del file zip effetuato con successo"); //Stampo Errore in caso il try non sia andato a buon fine } catch (IOException e) { System.out.println("Creazione del file zip fallita"); } } } |
Dopo aver eseguito il seguente programma abbiamo il nostro supermegabellissimo file “Zippato.zip” ora creiamo una seconda classe che ci servirà per scompattare il nostro file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
//Importo librerie import java.io.*; import java.util.zip.*; class UnZippa { public static void main(String[] args) { //Setto Filename e provo a unzippare il file try { String filename = "Zippato.zip"; UnZippa list = new UnZippa( ); list.getZipFiles(filename); System.out.println("Estrazione del file zip con successo"); } //Stampo Errore in caso il try non sia andato a buon fine catch (Exception e) { System.out.println("Estrazione del file zip e fallita"); e.printStackTrace(); } } //Metodo che prende il filename e mi ristiituisce i file scompattati :) public void getZipFiles(String filename) { try { //Setto la dir dove sta il file String destinationname = "d:\\JAVA\\Zip\\"; //Creo il mio buffer byte[] buf = new byte[1024]; //Creo il mio Zip Stream Input ZipInputStream zipinputstream = null; ZipEntry zipentry; zipinputstream = new ZipInputStream( new FileInputStream(filename)); zipentry = zipinputstream.getNextEntry(); //Ciclo while che estrae ogni voce presente nel archivio while (zipentry != null) { String entryName = zipentry.getName(); System.out.println("entryname "+entryName); int n; FileOutputStream fileoutputstream; File newFile = new File(entryName); String directory = newFile.getParent(); //Controllo se la directory e vuota se e cosi esco dal while if(directory == null) { if(newFile.isDirectory()) break; } //Creo il mio Zip Stream Output fileoutputstream = new FileOutputStream( destinationname+entryName); while ((n = zipinputstream.read(buf, 0, 1024)) > -1) fileoutputstream.write(buf, 0, n); fileoutputstream.close(); zipinputstream.closeEntry(); zipentry = zipinputstream.getNextEntry(); } zipinputstream.close(); } catch (Exception e) { System.out.println("Estrazione del file zip e fallita"); e.printStackTrace(); } } } |