Forum Stats

  • 3,828,121 Users
  • 2,260,865 Discussions
  • 7,897,483 Comments

Discussions

Unzipping a zip file from an applet

800152
800152 Member Posts: 19
edited Dec 9, 2010 2:48PM in New To Java
I am having trouble unzipping a zip file from an applet. It downloads the zip file to the user's home folder, and it is supposed to unzip it but it throws java.util.zip.ZipfileException.
import java.io.*;
import java.util.*;
import java.net.*;
import java.applet.*;
import java.util.zip.*;

public class downloader extends Applet {
	public String FileList;
	public static String newline = System.getProperty("line.separator");
	public static String file_separator = System.getProperty("file.separator");
	public static String user_home = System.getProperty("user.home");
	public void init() {
		String game = getParameter("game_dir");
		String game_zip = getCodeBase()+"games/"+game+".zip";
		String sphere_zip = "http://eggbertx.theoks.net/JavaSphere/sphere.zip";
		downloadFile(sphere_zip, user_home+"/sphere.zip");
		downloadFile(game_zip, user_home+"/"+game+".zip");
		unZip(user_home+file_separator+"sphere.zip");
		unZip(user_home+file_separator+game+".zip");
	}

	public void stop() {}


	public String getURL(String filename) {
		String file = "";
		try {
			URL file_url = new URL(filename);
			InputStream stream = file_url.openStream();
			InputStream temp_stream = file_url.openStream();
			BufferedReader buffer = new BufferedReader(new InputStreamReader(stream));
			BufferedReader temp_buffer = new BufferedReader(new InputStreamReader(temp_stream));
			while(temp_buffer.readLine() != null) {
				file = file+buffer.readLine()+newline;
			}
		} catch(MalformedURLException urle) {
			System.out.println("Malformed URL: "+filename);
		} catch(FileNotFoundException ioe) {
			System.out.println("Failed to load "+filename);
		} catch(Exception e) {
			System.out.println("General error: "+e);
		}
		return file;
	}

	public static final void unZip(String file) { //based off the Unzip class at http://www.devx.com/getHelpOn/10MinuteSolution/20447
		Enumeration entries;
		ZipFile zipFile;
		try {
			zipFile = new ZipFile(file);
			entries = zipFile.entries();
			while(entries.hasMoreElements()) {
				ZipEntry entry = (ZipEntry)entries.nextElement();
				if(entry.isDirectory()) {
					System.err.println("Extracting directory: " + entry.getName());
					(new File(entry.getName())).mkdir();
					continue;
				}
				System.err.println("Extracting file: " + entry.getName());
				InputStream in = zipFile.getInputStream(entry);
				OutputStream out = new BufferedOutputStream(new FileOutputStream(entry.getName()));
				byte[] buffer = new byte[1024];
				int len;
				while((len = in.read(buffer)) >= 0) {
					out.write(buffer, 0, len);
				}
				in.close();
				out.close();
			}
			zipFile.close();
		} catch (ZipException ze) {
			System.err.println("Zipfile error: "+ze);
		} catch (IOException ioe) {
			System.err.println("Unhandled exception:");
			ioe.printStackTrace();
			return;
		}
	}

	public static void downloadFile(String downloadURL, String filename)  { //by abar_sow at http://daniweb.com
		try {
			BufferedInputStream in = new java.io.BufferedInputStream(new URL(downloadURL).openStream());
			FileOutputStream fos = new java.io.FileOutputStream(filename);
			BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
			byte data[] = new byte[1024];
			while(in.read(data,0,1024)>=0) {
				bout.write(data);
			}
			bout.close();
			in.close();
		} catch(IOException ioe) {
			System.err.println("Unhandled exception:");
			ioe.printStackTrace();
			return;
		}
	}
}
Tagged:
«13

Answers

  • Kayaman
    Kayaman Member Posts: 3,844 Silver Trophy
    Eggbertx wrote:
    I am having trouble unzipping a zip file from an applet. It downloads the zip file to the user's home folder, and it is supposed to unzip it but it throws java.util.zip.ZipfileException.
    Well, what do you want us to do about it? Fix the problem yourself. Or better yet, learn to program instead of copy pasting random snippets of code from other sources and then wondering why things don't work.

    Or did you lazy bum think that this is some sort of automatic fixing service, where you vomit your code out here, say the name of the exception and we'll fix it for you?
  • 800152
    800152 Member Posts: 19
    Kayaman wrote:
    Eggbertx wrote:
    I am having trouble unzipping a zip file from an applet. It downloads the zip file to the user's home folder, and it is supposed to unzip it but it throws java.util.zip.ZipfileException.
    Well, what do you want us to do about it? Fix the problem yourself. Or better yet, learn to program instead of copy pasting random snippets of code from other sources and then wondering why things don't work.

    Or did you lazy bum think that this is some sort of automatic fixing service, where you vomit your code out here, say the name of the exception and we'll fix it for you?
    No, I was hoping you could tell me why the exception is being thrown, or at least point me in the right direction. Pardon me for spending an hour trying to figure it out (I'm fairly new and not exactly a java god like you must be, pardon me for my newness oh great one) As far as copy pasting, the the getURL function I wrote myself, the downloader I copied rather than reinventing the wheel, and as for the unzipper, like I said, I'm fairly new. I hope you can find it in your heart of hearts to pardon my ignorance.
  • Kayaman
    Kayaman Member Posts: 3,844 Silver Trophy
    Eggbertx wrote:
    No, I was hoping you could tell me why the exception is being thrown, or at least point me in the right direction.
    But how am I supposed to tell you that. That's what the exception and its stacktrace is for. Since you only pasted the name of the exception, all I can say is that "there's something wrong with your zip file handling".
    Pardon me for spending an hour trying to figure it out (I'm fairly new and not exactly a java god like you must be, pardon me for my newness oh great one)
    I only give slack to people posting in "New To Java", in "Java Programming" I expect people not to be total incompetents.
    I hope you can find it in your heart of hearts to pardon my ignorance.
    Ignorance I can pardon, arrogance I cannot. Since you're the one asking for help here, I wouldn't get worked up when you're not immediately handed a solution on a silver platter.
  • 800152
    800152 Member Posts: 19
    The stacktrace gives me very little help except for telling me that it's coming from lines 18 and 19 (calling the function) and 50. When I put the unzipping function in a local class, and run it via a command line, it works fine.
  • Kayaman
    Kayaman Member Posts: 3,844 Silver Trophy
    Well, post the full stacktrace. Doesn't it give any message after the name of the exception? Like:

    java.util.zip.ZipFileException : <Some error message here>
    at 123: com.foo.Bar
  • 800152
    800152 Member Posts: 19
    java.util.zip.ZipException: error in opening zip file
    at java.util.zip.ZipFile.open(Native Method)
    at java.util.zip.ZipFile.<init>(Unknown Source)
    at java.util.zip.ZipFile.<init>(Unknown Source)
    at downloader.unZip(downloader.java:50)
    at downloader.init(downloader.java:19)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
  • Kayaman
    Kayaman Member Posts: 3,844 Silver Trophy
    Does the file exist? Check it, then double check it.
  • walterln
    walterln Member Posts: 2,302
    And even if the file exist, the applet may not be allowed to access it (maybe through JNLP FileService but I don't think that includes unzipping).
    So why do you want to unzip in the first place?
  • jduprez
    jduprez Member Posts: 2,457
    Isn't there a "caused by" clause too in the stack trace?

    Does the file exist? No, seriously, have you traced what the "file" (it's merely a file path) looks like ( System.out.println(file) ), and is it excatly what you expect?

    Random stab: does the "user_home" contain some space in it (e.g. on Windows, +"Documents and Settings\whatever"+ )? I don't know about ZipFile, and actually I would surprised, but some API, including some entry points in the standard JDK API, do not support those well.
  • jduprez
    jduprez Member Posts: 2,457
    Walter Laan wrote:
    And even if the file exist, the applet may not be allowed to access it (maybe through JNLP FileService but I don't think that includes unzipping).
    Yeah I had that idea too, but then, according to the ZipFIle javadoc, it would throw a SecurityException straight, not a ZipFIleException chaining with a root cause SecurityException .
This discussion has been closed.