< prev index next >

src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java

Print this page
rev 57906 : 8211917: (zipfs) Creating or updating a JAR file system should put the MANIFEST.MF at the start
Reviewed-by:


1712     // sync the zip file system, if there is any update
1713     private void sync() throws IOException {
1714         // check ex-closer
1715         if (!exChClosers.isEmpty()) {
1716             for (ExistingChannelCloser ecc : exChClosers) {
1717                 if (ecc.closeAndDeleteIfDone()) {
1718                     exChClosers.remove(ecc);
1719                 }
1720             }
1721         }
1722         if (!hasUpdate)
1723             return;
1724         PosixFileAttributes attrs = getPosixAttributes(zfpath);
1725         Path tmpFile = createTempFileInSameDirectoryAs(zfpath);
1726         try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(tmpFile, WRITE))) {
1727             ArrayList<Entry> elist = new ArrayList<>(inodes.size());
1728             long written = 0;
1729             byte[] buf = null;
1730             Entry e;
1731 



1732             // write loc
1733             for (IndexNode inode : inodes.values()) {
















1734                 if (inode instanceof Entry) {    // an updated inode
1735                     e = (Entry)inode;
1736                     try {
1737                         if (e.type == Entry.COPY) {
1738                             // entry copy: the only thing changed is the "name"
1739                             // and "nlen" in LOC header, so we update/rewrite the
1740                             // LOC in new file and simply copy the rest (data and
1741                             // ext) without enflating/deflating from the old zip
1742                             // file LOC entry.
1743                             if (buf == null)
1744                                 buf = new byte[8192];
1745                             written += copyLOCEntry(e, true, os, written, buf);
1746                         } else {                          // NEW, FILECH or CEN
1747                             e.locoff = written;
1748                             written += e.writeLOC(os);    // write loc header
1749                             written += writeEntry(e, os);
1750                         }
1751                         elist.add(e);
1752                     } catch (IOException x) {
1753                         x.printStackTrace();    // skip any in-accurate entry




1712     // sync the zip file system, if there is any update
1713     private void sync() throws IOException {
1714         // check ex-closer
1715         if (!exChClosers.isEmpty()) {
1716             for (ExistingChannelCloser ecc : exChClosers) {
1717                 if (ecc.closeAndDeleteIfDone()) {
1718                     exChClosers.remove(ecc);
1719                 }
1720             }
1721         }
1722         if (!hasUpdate)
1723             return;
1724         PosixFileAttributes attrs = getPosixAttributes(zfpath);
1725         Path tmpFile = createTempFileInSameDirectoryAs(zfpath);
1726         try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(tmpFile, WRITE))) {
1727             ArrayList<Entry> elist = new ArrayList<>(inodes.size());
1728             long written = 0;
1729             byte[] buf = null;
1730             Entry e;
1731 
1732             final IndexNode manifestInode = getInode(getBytes("/META-INF/MANIFEST.MF"));
1733             final Iterator<IndexNode> inodeIterator = inodes.values().iterator();
1734             boolean manifestProcessed = false;
1735             // write loc
1736             while(inodeIterator.hasNext()) {
1737                 final IndexNode inode;
1738                 // write the manifest inode (if any) first in the loc so that the
1739                 // java.util.jar.JarInputStream can find it, since it expects it to be
1740                 // the first or second entry in the jar
1741                 if (manifestInode != null && !manifestProcessed) {
1742                     inode = manifestInode;
1743                     manifestProcessed = true;
1744                 } else {
1745                     // the manifest is either absent or we have already processed it,
1746                     // so we pick the next inode
1747                     inode = inodeIterator.next();
1748                     // don't process the same (manifest) node more than once
1749                     if (inode == manifestInode) {
1750                         continue;
1751                     }
1752                 }
1753                 if (inode instanceof Entry) {    // an updated inode
1754                     e = (Entry)inode;
1755                     try {
1756                         if (e.type == Entry.COPY) {
1757                             // entry copy: the only thing changed is the "name"
1758                             // and "nlen" in LOC header, so we update/rewrite the
1759                             // LOC in new file and simply copy the rest (data and
1760                             // ext) without enflating/deflating from the old zip
1761                             // file LOC entry.
1762                             if (buf == null)
1763                                 buf = new byte[8192];
1764                             written += copyLOCEntry(e, true, os, written, buf);
1765                         } else {                          // NEW, FILECH or CEN
1766                             e.locoff = written;
1767                             written += e.writeLOC(os);    // write loc header
1768                             written += writeEntry(e, os);
1769                         }
1770                         elist.add(e);
1771                     } catch (IOException x) {
1772                         x.printStackTrace();    // skip any in-accurate entry


< prev index next >