Unhandled exception:
java.io.FileNotFoundException: C:\Ramesh\10_AssemblyTest\ (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:102)
at java.io.FileOutputStream.<init>(FileOutputStream.java:62)
at Unzip.main(Unzip.java:67)
here is my code..
public static final void main(String[] args) {
Enumeration entries;
ZipFile zipFile;
ZipFile zipFile = new ZipFile(f);
try {
String myfile = "C:\\Ramesh\\AIS02.jar";
String myfileout = "C:\\Ramesh\\10_AssemblyTest\\";
File f = new File(myfile);
zipFile = new ZipFile(f);
entries = zipFile.entries();
while(entries.hasMoreElements()) {
ZipEntry entry = new ZipEntry(myfileout);
if(entry.isDirectory()) {
(new File(entry.getName())).mkdir();
continue;
}
//System.err.println("Extracting file: " + entry.getName());
copyInputStream(zipFile.getInputStream(entry),
new BufferedOutputStream(new FileOutputStream(entry.getName())));
}
zipFile.close();
} catch (IOException ioe) {
System.err.println("Unhandled exception:");
ioe.printStackTrace();
return;
}
This is nonsens. You enumerate over the entries, but you never get any
of the entries at all.
You want something like:
ZipEntry entry = (ZipEntry)entries.nextElement();
>
>
> if(entry.isDirectory()) {
>
> (new File(entry.getName())).mkdir();
(a) You are not prepending your target directory "myfileout", so
the directories are not created where you think they are.
(b) This assumes that there is always a directory entry in the zip file
before there are the actual files for the directory. You might want to
check the zip format specification if this is guaranteed. I am not sure
at the moment.
> //System.err.println("Extracting file: " + entry.getName());
> copyInputStream(zipFile.getInputStream(entry),
> new BufferedOutputStream(new FileOutputStream(entry.getName())));
Again, you are not prepending your target directory name to the file
name.
/Thomas
>I am trying to unzip jar file into another directory I am getting following error
> ZipEntry entry = new ZipEntry(myfileout);
The API docs of ZipEntry say:
public ZipEntry(String name)
Creates a new zip entry with the specified name.
However, myfileout points to a directory (I guess).
More importantly, instead of calling entries.nextElement, you seem to
create a new ZipEntry without any context. So you are not really
working on the entries taken from the ZIP file.
[...]
Regards,
Marco
--
Please reply in the newsgroup, not by email!
Java programming tips: http://jiu.sourceforge.net/javatips.html
Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html