Don not use potentially deprecated ZipEntry to get an entry's size. The entry

may have been generated for an old version of the zip file and not exist at all.
As a fix, don't store the ZipEntry at all in ZipResource, just the ZipEntry's name.
This commit is contained in:
hns 2005-04-11 11:44:54 +00:00
parent 530afc9f88
commit 47e6955aa2
2 changed files with 32 additions and 17 deletions

View file

@ -113,7 +113,7 @@ public final class ZipRepository extends AbstractRepository {
// path depth of this.depth + 1 // path depth of this.depth + 1
if (entrypath.length == depth + 1 && !entry.isDirectory()) { if (entrypath.length == depth + 1 && !entry.isDirectory()) {
// create a new child resource // create a new child resource
ZipResource resource = new ZipResource(file, entry, this); ZipResource resource = new ZipResource(entry.getName(), this);
newResources.put(resource.getShortName(), resource); newResources.put(resource.getShortName(), resource);
} else if (entrypath.length > depth) { } else if (entrypath.length > depth) {
// create a new child repository // create a new child repository
@ -161,7 +161,7 @@ public final class ZipRepository extends AbstractRepository {
* Called to create a child resource for this repository * Called to create a child resource for this repository
*/ */
protected Resource createResource(String name) { protected Resource createResource(String name) {
return new ZipResource(file, new ZipEntry(entryPath + "/" + name), this); return new ZipResource(entryPath + "/" + name, this);
} }
public long getChecksum() { public long getChecksum() {

View file

@ -23,22 +23,19 @@ import java.util.zip.ZipFile;
public final class ZipResource implements Resource { public final class ZipResource implements Resource {
private ZipEntry zipentry; private String entryName;
private File zipfile;
private ZipRepository repository; private ZipRepository repository;
private String name; private String name;
private String shortName; private String shortName;
private String baseName; private String baseName;
protected ZipResource(File zipfile, ZipEntry zipentry, ZipRepository repository) { protected ZipResource(String zipentryName, ZipRepository repository) {
this.zipentry = zipentry; this.entryName = zipentryName;
this.zipfile = zipfile;
this.repository = repository; this.repository = repository;
String entryname = zipentry.getName(); int lastSlash = entryName.lastIndexOf('/');
int lastSlash = entryname.lastIndexOf('/');
shortName = entryname.substring(lastSlash + 1); shortName = entryName.substring(lastSlash + 1);
name = new StringBuffer(repository.getName()).append('/') name = new StringBuffer(repository.getName()).append('/')
.append(shortName).toString(); .append(shortName).toString();
@ -48,16 +45,20 @@ public final class ZipResource implements Resource {
} }
public long lastModified() { public long lastModified() {
return zipfile.lastModified(); return repository.lastModified();
} }
public InputStream getInputStream() throws IOException { public InputStream getInputStream() throws IOException {
ZipFile zipfile = null; ZipFile zipfile = null;
try { try {
zipfile = repository.getZipFile(); zipfile = repository.getZipFile();
int size = (int) zipentry.getSize(); ZipEntry entry = zipfile.getEntry(entryName);
if (entry == null) {
throw new IOException("Zip resource " + this + " does not exist");
}
int size = (int) entry.getSize();
byte[] buf = new byte[size]; byte[] buf = new byte[size];
InputStream in = zipfile.getInputStream(zipentry); InputStream in = zipfile.getInputStream(entry);
int read = 0; int read = 0;
while (read < size) { while (read < size) {
int r = in.read(buf, read, size-read); int r = in.read(buf, read, size-read);
@ -76,7 +77,7 @@ public final class ZipResource implements Resource {
ZipFile zipfile = null; ZipFile zipfile = null;
try { try {
zipfile = repository.getZipFile(); zipfile = repository.getZipFile();
return (zipfile.getEntry(zipentry.getName()) != null); return (zipfile.getEntry(entryName) != null);
} catch (Exception ex) { } catch (Exception ex) {
return false; return false;
} finally { } finally {
@ -90,8 +91,12 @@ public final class ZipResource implements Resource {
ZipFile zipfile = null; ZipFile zipfile = null;
try { try {
zipfile = repository.getZipFile(); zipfile = repository.getZipFile();
InputStreamReader in = new InputStreamReader(zipfile.getInputStream(zipentry)); ZipEntry entry = zipfile.getEntry(entryName);
int size = (int) zipentry.getSize(); if (entry == null) {
throw new IOException("Zip resource " + this + " does not exist");
}
InputStreamReader in = new InputStreamReader(zipfile.getInputStream(entry));
int size = (int) entry.getSize();
char[] buf = new char[size]; char[] buf = new char[size];
int read = 0; int read = 0;
while (read < size) { while (read < size) {
@ -126,7 +131,17 @@ public final class ZipResource implements Resource {
} }
public long getLength() { public long getLength() {
return zipentry.getSize(); ZipFile zipfile = null;
try {
zipfile = repository.getZipFile();
return zipfile.getEntry(entryName).getSize();
} catch (Exception ex) {
return 0;
} finally {
try {
zipfile.close();
} catch (Exception ex) {}
}
} }
public Repository getRepository() { public Repository getRepository() {