Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ZipInputStream does not read comments




On Jan 5, 2006, at 2:51 AM, Nicholas Rinard wrote:

how is this not a bug?


I thought when I read it that it acknowledged as a bug with a work around. Get the ZipFile. So more accurately, yes a bug, but no, will not get fixed. The central directory for the zip file is at the end of the file or stream which is problematic for this access.
If you wanted information from the central directory without using the java.util.zip code you could handle it as a RandomAccessFile and use something like...


private void findDirFooter(RandomAccessFile raf,long length) throws IOException
{
long seekPos = length - FixedSizeOfDirFooter;
if (seekPos < 0)
throw new IOException(zipPath + " central directory footer not found");
raf.seek(seekPos);
for (int i = 0; i < 256; i++)
{
while (raf.readInt() != DIR_END) raf.seek(--seekPos);
footer = new ZipDirFooter(raf);
return;
}
// Messed up bad enough this doesn't attempt to checkpoint with error()
throw new IOException(zipPath + " central directory footer not found");
}

private void readCentralDir(RandomAccessFile raf,long length) throws IOException
{
try { findDirFooter(raf,length); }
catch(EOFException eof) {
return;
}
long dirpos = (long)footer.centralDirOffset;
if (dirpos == 0) return; // If empty return
// Verify at dir, seeing some bad ones, else seek
raf.seek(dirpos);
int dirtest = raf.readInt();
if (dirtest != DIR) {
boolean dirfound = false;
for (;(++dirpos) < (raf.length()-4);) {
raf.seek(dirpos);
dirtest = raf.readInt();
if (dirtest == DIR) {
footer.setCentralDirOffset((int)dirpos); // Should update be forced?
dirfound = true;
utillib.common.Configuration.getSysOut().println("ZipFile: misplaced directory header found at " + dirpos + " for " + zipPath);
break;
}
}
// Even after locating correctly we now seem to corrupt worse, so error as it used to be for now
throw new IOException("Directory header not found for " + zipPath);
}
raf.seek(dirpos);
int entries = (int)footer.getDirEntriesCnt();
for (int i = 0; i < entries; i++) {
int header = raf.readInt();
if (header != DIR) {
if (header == DIR_END) {
System.out.println("ZipFile: " + zipPath + " - Early directory end with " + (i+1) + " of " + entries + " entries");
}
long savepos = rafzip.getFilePointer();
logger.error("ZipFile: " + zipPath + " - Error reading directory got " + Integer.toHexString(header) + " instead of DIR at " + Long.toHexString(savepos-4));
ZipFileUtils.dump(rafzip,(long)(rafzip.getFilePointer()-64)/ 32*32,(long)128);
rafzip.seek(savepos); // In trouble anyhow but restore location
throw new IOException("Directory header not found");
}
ZipDirHeader dirHeader = new ZipDirHeader(raf);
dirList.put(dirHeader.getFileName(),dirHeader);
}
logger.debug("ZipFile: " + zipPath + " read central dir complete");
}


This my own code (some comments deleted, I've fought file corruption with this code a number of times) , already mentioned if not shamelessly plugged.


Mike Hall mikehall at spacestar dot net http://www.spacestar.net/users/mikehall http://sourceforge.net/projects/macnative



_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden
References: 
 >ZipInputStream does not read comments (From: Geoff Levner <email@hidden>)
 >Re: ZipInputStream does not read comments (From: Geoff Levner <email@hidden>)
 >Re: ZipInputStream does not read comments (From: Nicholas Rinard <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.