diff --git a/ProjetGenieLogicielLoto.iml b/ProjetGenieLogicielLoto.iml index 25b7aca..ade512f 100644 --- a/ProjetGenieLogicielLoto.iml +++ b/ProjetGenieLogicielLoto.iml @@ -6,14 +6,12 @@ - - - + diff --git a/src/main/java/fr/myny/data/DataCsv.java b/src/main/java/fr/myny/data/DataCsv.java index dc34652..a6411cf 100644 --- a/src/main/java/fr/myny/data/DataCsv.java +++ b/src/main/java/fr/myny/data/DataCsv.java @@ -19,7 +19,7 @@ public class DataCsv { * La methode de recuperation dun fichier csv */ public void getCsv(String fileZip) throws IOException{ - + //"src/main/resources/loto_201911.zip" File desDir = new File("src/main/resources/"); byte[] buffer = new byte[1024]; ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip)); diff --git a/src/main/java/fr/myny/data/ImportData.java b/src/main/java/fr/myny/data/ImportData.java index 41a028a..205fb2c 100644 --- a/src/main/java/fr/myny/data/ImportData.java +++ b/src/main/java/fr/myny/data/ImportData.java @@ -1,14 +1,20 @@ package fr.myny.data; +import java.io.*; +import java.net.URL; +import java.net.MalformedURLException; + public class ImportData { - String url; + + protected String url; /** * Le constructeur de ImportData */ - public ImportData(){ - + ImportData(){ + this.url = "https://www.fdj.fr/jeux-de-tirage/loto/statistiques"; } + /** * Le constructeur de ImportData * @param url string contenant l URL dou recuperer les fichiers csv @@ -17,13 +23,76 @@ public class ImportData { this.url=url; } - /** - * La methode de recuperation de l URL - */ - public void getUrl(){ + private boolean UrlExist() { + try { + URL site = new URL(url); + try { + site.openStream(); + return true; + } catch (IOException e) { + return false; + } + } catch (MalformedURLException e) { + return false; + } } - public void unzip() { - + private boolean ZipNotExist(String filename) { + File directory = new File("src/main/java/resources/"); + File[] files = directory.listFiles(); + for (File f : files) { + if (f.getName().equals(filename)) + return false; + } + return true; } + + public void DownloadCsvZip() { + if (UrlExist()) { + BufferedReader in = null; + + try { + URL site = new URL(url); + in = new BufferedReader(new InputStreamReader(site.openStream())); + + String test = "loto_"; + String nameZip; + String urlZipLink; + while ((urlZipLink = in.readLine()) != null) { + if (urlZipLink.length() > 128) { + if (test.equals(urlZipLink.substring(50, 55))) { + nameZip = urlZipLink.substring(50,65); + urlZipLink = urlZipLink.substring(13,65); + if (ZipNotExist(nameZip)) { + try (BufferedInputStream bis = new BufferedInputStream(new URL(urlZipLink).openStream()); + FileOutputStream fos = new FileOutputStream("src/main/java/resources/" + nameZip)) { + byte data[] = new byte[1024]; + int byteContent; + while ((byteContent = bis.read(data, 0, 1024)) != -1) { + fos.write(data, 0, byteContent); + } + } catch (IOException e) { + e.printStackTrace(System.out); + } + } + + } + } + } + in.close(); + } catch (IOException e) { + System.out.println("Error URL can't open : " + e); + } + finally { + try { + in.close(); + } catch (IOException e) { + System.out.println("Error close buffer : " + e); + } + } + } + else + System.out.println("Web sit don't exist"); + } + } diff --git a/src/main/java/fr/myny/gui/Ihm.java b/src/main/java/fr/myny/gui/Ihm.java index 023c2f7..9c8ec2c 100644 --- a/src/main/java/fr/myny/gui/Ihm.java +++ b/src/main/java/fr/myny/gui/Ihm.java @@ -2,9 +2,6 @@ package fr.myny.gui; public class Ihm { - - - /** * Le constructeur de Ihm */ @@ -30,7 +27,7 @@ public class Ihm { * La methode de demande de telechargement de donnees */ public void downloadData(){ - + // Envoie directement l'utilisateur vers le site de téléchargement } /**