• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Getting a zip File Using a URL

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 40746
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No It doesnt :-(
That was the first thing I tried.
 
Jeanne Boyarsky
author & internet detective
Posts: 40746
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic