- import java.io.*;
- import java.util.*;
- import java.util.zip.*;
-
-
- public class UnZipPerso {
-
- static final int BUFFER = 2048;
-
- public UnZipPerso(String path) {
- try {
-
- // préparation du zip
- BufferedOutputStream dest = null;
- BufferedInputStream is = null;
- ZipEntry entry;
- ZipFile zipfile = new ZipFile(path);
- Enumeration e = zipfile.entries();
-
- // recharche du nom du zip
- File pathNom = new File(path);
- String nomFichier = pathNom.getName();
- String nom = nomFichier.substring(0, nomFichier.length() - 4);
- //System.out.println(nom);
-
- // création du dossier racine
- File NOM = new File(nom);
- NOM.mkdir();
-
- // URL de base
- String URLbase = "." + "/" + nom ;
-
-
-
- // boucle d'extraction des fichiers
- while (e.hasMoreElements()) {
-
- // préparation du fichier courant à extraire
- entry = (ZipEntry) e.nextElement();
- String url = entry.getName();
- //System.out.println("Extracting: " + url);
-
- String URLfichier = URLbase + "/" + url;
- //System.out.println("URL: " + URLfichier);
-
- File URL = new File(URLfichier);
-
-
- if (isFile(URL.getName())) {
- // écriture du fichier (extraction)
- is = new BufferedInputStream
- (zipfile.getInputStream(entry));
- int count;
- byte data[] = new byte[BUFFER];
- FileOutputStream fos = new
- FileOutputStream(URLfichier);
- dest = new
- BufferedOutputStream(fos, BUFFER);
- while ( (count = is.read(data, 0, BUFFER))
- != -1) {
- dest.write(data, 0, count);
- }
- dest.flush();
- dest.close();
- is.close();
- }
- else {
- // zone répértoire
- URL.mkdirs();
- }
-
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
-
- }
-
- private boolean isFile (String nom) {
- for (int i=nom.length()-1; i>=0; i--) {
-
- if (nom.charAt(i) == '.' ) {
- return true;
- }
-
- }
-
- return false;
- }
-
- public static void main(String[] args) {
- String path = "./essai.zip";
- UnZipPerso unZip1 = new UnZipPerso(path);
- }
-
- }
import java.io.*;
import java.util.*;
import java.util.zip.*;
public class UnZipPerso {
static final int BUFFER = 2048;
public UnZipPerso(String path) {
try {
// préparation du zip
BufferedOutputStream dest = null;
BufferedInputStream is = null;
ZipEntry entry;
ZipFile zipfile = new ZipFile(path);
Enumeration e = zipfile.entries();
// recharche du nom du zip
File pathNom = new File(path);
String nomFichier = pathNom.getName();
String nom = nomFichier.substring(0, nomFichier.length() - 4);
//System.out.println(nom);
// création du dossier racine
File NOM = new File(nom);
NOM.mkdir();
// URL de base
String URLbase = "." + "/" + nom ;
// boucle d'extraction des fichiers
while (e.hasMoreElements()) {
// préparation du fichier courant à extraire
entry = (ZipEntry) e.nextElement();
String url = entry.getName();
//System.out.println("Extracting: " + url);
String URLfichier = URLbase + "/" + url;
//System.out.println("URL: " + URLfichier);
File URL = new File(URLfichier);
if (isFile(URL.getName())) {
// écriture du fichier (extraction)
is = new BufferedInputStream
(zipfile.getInputStream(entry));
int count;
byte data[] = new byte[BUFFER];
FileOutputStream fos = new
FileOutputStream(URLfichier);
dest = new
BufferedOutputStream(fos, BUFFER);
while ( (count = is.read(data, 0, BUFFER))
!= -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
is.close();
}
else {
// zone répértoire
URL.mkdirs();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
private boolean isFile (String nom) {
for (int i=nom.length()-1; i>=0; i--) {
if (nom.charAt(i) == '.' ) {
return true;
}
}
return false;
}
public static void main(String[] args) {
String path = "./essai.zip";
UnZipPerso unZip1 = new UnZipPerso(path);
}
}