Você pode encapsular a InputStream que é retornada pela URLConnection e encapsulá-la em uma ZipInputStream. Você vai ter de aprender também a classe ZipEntry.
ricardospinoza
Thingol, obrigado por responder. Irei tentar fazer conforme sua orientação, qqr coisa retorno.
D
dmarcosm
Eu já fiz algo parecido. Daí para poder descompactar o arquivo zip eu salvei ele como arquivo temporário usando
Daí é só usar a API do Java para descompactar o arquivo e pegar seu conteúdo.
Espero ter ajudao. Até.
ricardospinoza
Pessoal, obrigado pelas dicas.
Bom já tenho alguns progressos:
packagesorte;importjava.io.BufferedInputStream;importjava.io.DataInputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.net.MalformedURLException;importjava.net.URL;importjava.util.zip.ZipEntry;importjava.util.zip.ZipFile;importjava.util.zip.ZipInputStream;publicclassDataminingMegaSena{publicstaticvoidmain(String[]args){//-----------------------------------------------------//// Step 1: Start creating a few objects we'll need.//-----------------------------------------------------//URLu;InputStreamis=null;DataInputStreamdis;Strings;try{//------------------------------------------------------------//// Step 2: Create the URL. ////------------------------------------------------------------//// Note: Put your real URL here, or better yet, read it as a //// command-line arg, or read it from a file. ////------------------------------------------------------------//u=newURL("http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_megase.zip");//----------------------------------------------//// Step 3: Open an input stream from the url. ////----------------------------------------------//is=u.openStream();// throws an IOException//ZipFile zipFile = new ZipFile(is);//entries = zipFile.entries();//FileInputStream fin = new FileInputStream(is);ZipInputStreamzin=newZipInputStream(is);ZipEntryze=null;while((ze=zin.getNextEntry())!=null){System.out.println("Unzipping "+ze.getName());FileOutputStreamfout=newFileOutputStream(ze.getName());for(intc=zin.read();c!=-1;c=zin.read()){fout.write(c);}zin.closeEntry();fout.close();}zin.close();Filefile=newFile("D_MEGA.HTM");FileInputStreamfis=null;BufferedInputStreambis=null;//DataInputStream dis = null;try{fis=newFileInputStream(file);// Here BufferedInputStream is added for fast reading.bis=newBufferedInputStream(fis);dis=newDataInputStream(bis);// dis.available() returns 0 if the file does not have more lines.while(dis.available()!=0){// this statement reads the line from the file and print it to// the console.System.out.println(dis.readLine());}// dispose all the resources after using them.fis.close();bis.close();dis.close();}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}//-------------------------------------------------------------//// Step 4: ////-------------------------------------------------------------//// Convert the InputStream to a buffered DataInputStream. //// Buffering the stream makes the reading faster; the //// readLine() method of the DataInputStream makes the reading //// easier. ////-------------------------------------------------------------//// dis = new DataInputStream(new BufferedInputStream(is));//------------------------------------------------------------//// Step 5: ////------------------------------------------------------------//// Now just read each record of the input stream, and print //// it out. Note that it's assumed that this problem is run //// from a command-line, not from an application or applet. ////------------------------------------------------------------//// while ((s = dis.readLine()) != null) {// System.out.println(s);// }}catch(MalformedURLExceptionmue){System.out.println("Ouch - a MalformedURLException happened.");mue.printStackTrace();System.exit(1);}catch(IOExceptionioe){System.out.println("Oops- an IOException happened.");ioe.printStackTrace();System.exit(1);}finally{//---------------------------------//// Step 6: Close the InputStream ////---------------------------------//try{is.close();}catch(IOExceptionioe){// just going to ignore this one}}// end of 'finally' clause}// end of main }
Pego o arquivo pela url descompacto e consigo ler o arquivo, porém agora gostaria de sabe se há algum facilitador para interpretar tag´s html para
que eu possa extrair os dados do arquivo htm? sem querer abusar muito :lol:
neofito
Vixe!!! Quanto código!!!
Tsc, tsc… porque você não usa groovy? Olha só a diferença:
Karakas neófito, o código fica bem menor mesmo. Este Groovy é algum framework Java ou outro tipo de linguagem (a forma de uso lembra um pouco javascript, prototype…)?
neofito
O groovy é uma linguagem dinâmica para a jvm, inspirada em python, ruby e smalltalk (dentre outras).