diff options
author | jfrijters <jfrijters> | 2011-07-05 11:04:49 +0400 |
---|---|---|
committer | jfrijters <jfrijters> | 2011-07-05 11:04:49 +0400 |
commit | 733bd571e445717ce39e56cade7100e76b190f74 (patch) | |
tree | 104a9b0ef824259d2de3c6076c2b64f6e76c8777 /openjdk/java/util | |
parent | 2f765373ffd370b2a9d06154971dde8e47209c01 (diff) |
Implement Java 7 ZipFile.getComment() API.
Diffstat (limited to 'openjdk/java/util')
-rw-r--r-- | openjdk/java/util/zip/ZipFile.java | 68 |
1 files changed, 51 insertions, 17 deletions
diff --git a/openjdk/java/util/zip/ZipFile.java b/openjdk/java/util/zip/ZipFile.java index aa7c2f1b..fd7c4a74 100644 --- a/openjdk/java/util/zip/ZipFile.java +++ b/openjdk/java/util/zip/ZipFile.java @@ -225,23 +225,8 @@ public class ZipFile implements ZipConstants */ private void readEntries() throws IOException { - /* Search for the End Of Central Directory. When a zip comment is - * present the directory may start earlier. - * Note that a comment has a maximum length of 64K, so that is the - * maximum we search backwards. - */ PartialInputStream inp = new PartialInputStream(4096); - long pos = raf.length() - ENDHDR; - long top = Math.max(0, pos - 65536); - do - { - if (pos < top) - throw new ZipException("error in opening zip file"); - inp.seek(pos--); - } - while (inp.readLeInt() != ENDSIG); - - pos++; + long pos = inp.seekEndOfCentralDirectory(); inp.skip(6); int count = inp.readLeUnsignedShort(); long centralSize = inp.readLeUnsignedInt(); @@ -456,7 +441,31 @@ public class ZipFile implements ZipConstants checkClosed(); return entries.size(); } - + + /** + * Returns the zip file comment. + * + * @exception IllegalStateException when the ZipFile has already been closed + */ + public synchronized String getComment() + { + checkClosed(); + try + { + PartialInputStream inp = new PartialInputStream(4096); + long pos = inp.seekEndOfCentralDirectory(); + inp.skip(16); + int commentLength = inp.readLeUnsignedShort(); + if (commentLength == 0) + return null; + return inp.readString(commentLength, false); + } + catch (IOException _) + { + return null; + } + } + private static class ZipEntryEnumeration implements Enumeration<ZipEntry> { private final Iterator<ZipEntry> elements; @@ -503,6 +512,31 @@ public class ZipFile implements ZipConstants lazy = true; } + // Seek to the "end of central directory record" and position + // the stream after the ENDSIG and return the file offset + // where the record starts. + long seekEndOfCentralDirectory() throws IOException + { + /* Search for the End Of Central Directory. When a zip comment is + * present the directory may start earlier. + * Note that a comment has a maximum length of 64K, so that is the + * maximum we search backwards. + */ + long length = raf.length(); + if (length == 0) + throw new ZipException("zip file is empty"); + long pos = length - ENDHDR; + long top = Math.max(0, pos - 65536); + do + { + if (pos < top) + throw new ZipException("error in opening zip file"); + seek(pos--); + } + while (readLeInt() != ENDSIG); + return pos + 1; + } + void lazyInitialSeek() throws IOException { } |