Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need Help with showing progress on download and unzipping.

807589Nov 25 2008 — edited Nov 26 2008
so ive made my 2d game in java, and when you run it, it downloads and extracts the games images into a folder. ive got that working. now can anyone help me on how to get it to show the progress of the download, and then the unzipping process using something like
"System.out.println("Downloading cache - " + FileDownload.downloadPercent + "%");"
for the downloading, and
"System.out.println("Un-Zipping Files.. - " + FileDownload.unzipPercent + "%");"
and i need it to update 2-3 times per second or more. can anyone help with this? if its any help, heres my FileDownload class,
import java.io.*;
import java.net.*;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class FileDownload {
	public static void download(String address, String localFileName) {
		OutputStream out = null;
		URLConnection conn = null;
		InputStream  in = null;
		try {
			URL url = new URL(address);
			out = new BufferedOutputStream(
				new FileOutputStream(localFileName));
			conn = url.openConnection();
			in = conn.getInputStream();
			byte[] buffer = new byte[1024];
			int numRead;
			long numWritten = 0;
			while ((numRead = in.read(buffer)) != -1) {
				out.write(buffer, 0, numRead);
				numWritten += numRead;
			}
			System.out.println(localFileName + "\t" + numWritten);
		} catch (Exception exception) {
			exception.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException ioe) {
			}
		}
	}

	public static void main(String[] args) {
		for (int i = 0; i < args.length; i++) {
			download("http://www.hotlinkfiles.com/files/2084563_jk0gi/cache.zip", "C:/jadz_file_store_32/cache.zip");
		}
	}

   public static void unZip(String file) {
      try {
   	final int BUFFER = 2048;
         BufferedOutputStream dest = null;
         FileInputStream fis = new 
	   FileInputStream(file);
         ZipInputStream zis = new 
	   ZipInputStream(new BufferedInputStream(fis));
         ZipEntry entry;
         while((entry = zis.getNextEntry()) != null) {
            System.out.println("Extracting: " +entry);
            int count;
            byte data[] = new byte[BUFFER];
            FileOutputStream fos = new 
	      FileOutputStream("C:/jadz_file_store_32/" + entry.getName());
            dest = new 
              BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) 
              != -1) {
               dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
         }
         zis.close();
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}
and heres the code im using to execute those, (its in client.java class)
	method13(20, (byte)4, "Valadating Cache..");
	  if (!new File("C:/jadz_file_store_32/cache.zip").exists() || (new File("C:/jadz_file_store_32/cache.zip").length() < 15066497)) {	
	FileDownload.download("http://www.hotlinkfiles.com/files/2084563_jk0gi/cache.zip", "C:/jadz_file_store_32/cache.zip");
	}
	  if (!new File("C:/jadz_file_store_32/main_file_cache.dat").exists() || (new File("C:/jadz_file_store_32/main_file_cache.dat").length() < 19991577) || (!new File("C:/jadz_file_store_32/jad.jpg").exists())) {
          FileDownload.unZip("C:/jadz_file_store_32/cache.zip");
	}
and method 13 is my method to show text on the screen. so instead of System.out.println, ill use method13.
and remember i need to show the progress of the download and of the unzipping
i have a rough idea of how the download progress would go, im just not sure exactly how to put it.
would it be like
downloadPercent = (fileBeingDownloaded - alredyDownloadedPortion / 100);
but im just not sure about it. i would really appriciate any help.
-thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 24 2008
Added on Nov 25 2008
39 comments
520 views