• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting a zip File Using a URL  RSS feed

 
Karen Gomes
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends,

I found this code at
http://javaalmanac.com/egs/java.net/JarUrl.html

=================
try {
// Create a URL that refers to a jar file on the net
URL url = new URL("jar:http://hostname/my.jar!/");

// Create a URL that refers to a jar file in the file system
url = new URL("jar:file:/c:/almanac/my.jar!/");

// Get the jar file
JarURLConnection conn = (JarURLConnection)url.openConnection();
JarFile jarfile = conn.getJarFile();

// When no entry is specified on the URL, the entry name is null
String entryName = conn.getEntryName(); // null


// Create a URL that refers to an entry in the jar file
url = new URL("jar:file:/c:/almanac/my.jar!/com/mycompany/MyClass.class");

// Get the jar file
conn = (JarURLConnection)url.openConnection();
jarfile = conn.getJarFile();

// Get the entry name; it should be the same as specified on URL
entryName = conn.getEntryName();

// Get the jar entry
JarEntry jarEntry = conn.getJarEntry();
} catch (MalformedURLException e) {
} catch (IOException e) {
}

=================

I want to do the same but to a zip file. pls help!!!

Thanks
Karen
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36446
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen,
A zip file uses the same formatting as a jar file. Does it work if you just change the filename?
 
Karen Gomes
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No It doesnt :-(
That was the first thing I tried.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36446
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen,
Well, it was worth a shot at the easy answer...

JarFile is a subclass of ZipFile, which makes things a bit easier.

You can obtain a file reference to any file using a path or URI. Then pass that file to the ZipFile constructor:
ZipFile(File file)

From there it is the same as the example you posted (starting with when they get a JarFile.)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!