ImportData
This commit is contained in:
parent
55fec686b0
commit
4d17091234
2 changed files with 79 additions and 10 deletions
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue