download zip

T

thanh toan

I can download the zip file, but can't extract it with jar. The zip size is
correct. Thank you.

import java.net.*;

import java.io.*;

public class downloadZip

{

public static void main(String args[]) throws Exception

{


URL yh = new URL("the url of the zip file");

InputStreamReader in = new InputStreamReader( yh.openStream());

FileWriter out = new FileWriter("the name of the zip file");

int m;

while( (m= in.read()) != -1 )

{

out.write(m);

}



in.close();

out.close();




}

}
 
T

Thomas Weidenfeller

thanh said:
I can download the zip file, but can't extract it with jar. The zip size is
correct. Thank you. [...]

InputStreamReader in = new InputStreamReader( yh.openStream());

FileWriter out = new FileWriter("the name of the zip file");

You are using character Readers/Writers on a binary file.

/Thomas
 
T

thanh toan

The error message is ugly. Thank you both for telling flush() and read
binary. I used the following and it works.
and for the line

byte buffer[] = new buffer[255] make the downloaded zip corrupted, but if I
used this
byte buffer[] = new buffer[1], the downloaded zip extracts very ok.




import java.net.*;

import java.io.*;

public class downloadZip

{

public static void main(String args[]) throws Exception

{


URL yahoo = new URL("the url of the zip file");

DataInputStream in = new DataInputStream(yahoo.openStream() );

FileOutputStream out = new FileOutputStream("the name of the zip
file");

byte buffer[] = new byte[1]; /*if i tried 255, downloaded zip is
corrupted, but 1 works */




while( in.read(buffer) != -1)

{

out.write(buffer,0,buffer.length);

}

out.flush();

in.close();

out.close();






}

}

codes/ Web & IT Help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,698
Latest member
alexxx

Latest Threads

Top