src/java.base/share/classes/java/util/zip/ZipFile.java

Print this page




1105                             // Let's do some extra verification, we don't care about the
1106                             // performance in this situation.
1107                             byte[] sbuf = new byte[4];
1108                             long cenpos = end.endpos - end.cenlen;
1109                             long locpos = cenpos - end.cenoff;
1110                             if  (cenpos < 0 ||
1111                                  locpos < 0 ||
1112                                  readFullyAt(sbuf, 0, sbuf.length, cenpos) != 4 ||
1113                                  GETSIG(sbuf) != CENSIG ||
1114                                  readFullyAt(sbuf, 0, sbuf.length, locpos) != 4 ||
1115                                  GETSIG(sbuf) != LOCSIG) {
1116                                 continue;
1117                             }
1118                         }
1119                         if (comlen > 0) {    // this zip file has comlen
1120                             comment = new byte[comlen];
1121                             if (readFullyAt(comment, 0, comlen, end.endpos + ENDHDR) != comlen) {
1122                                 zerror("zip comment read failed");
1123                             }
1124                         }
1125                         if (end.cenlen == ZIP64_MAGICVAL ||
1126                             end.cenoff == ZIP64_MAGICVAL ||
1127                             end.centot == ZIP64_MAGICCOUNT)
1128                         {
1129                             // need to find the zip64 end;
1130                             try {
1131                                 byte[] loc64 = new byte[ZIP64_LOCHDR];
1132                                 if (readFullyAt(loc64, 0, loc64.length, end.endpos - ZIP64_LOCHDR)

1133                                     != loc64.length || GETSIG(loc64) != ZIP64_LOCSIG) {
1134                                     return end;
1135                                 }
1136                                 long end64pos = ZIP64_LOCOFF(loc64);
1137                                 byte[] end64buf = new byte[ZIP64_ENDHDR];
1138                                 if (readFullyAt(end64buf, 0, end64buf.length, end64pos)
1139                                     != end64buf.length || GETSIG(end64buf) != ZIP64_ENDSIG) {
1140                                     return end;
1141                                 }
1142                                 // end64 found, re-calcualte everything.
1143                                 end.cenlen = ZIP64_ENDSIZ(end64buf);
1144                                 end.cenoff = ZIP64_ENDOFF(end64buf);
1145                                 end.centot = (int)ZIP64_ENDTOT(end64buf); // assume total < 2g










1146                                 end.endpos = end64pos;
1147                             } catch (IOException x) {}    // no zip64 loc/end
1148                         }
1149                         return end;
1150                     }
1151                 }
1152             }
1153             zerror("zip END header not found");
1154             return null; //make compiler happy
1155         }
1156 
1157         // Reads zip file central directory.
1158         private void initCEN(int knownTotal) throws IOException {
1159             if (knownTotal == -1) {
1160                 End end = findEND();
1161                 if (end.endpos == 0) {
1162                     locpos = 0;
1163                     total = 0;
1164                     entries  = new int[0];
1165                     cen = null;
1166                     return;         // only END header present
1167                 }
1168                 if (end.cenlen > end.endpos)




1105                             // Let's do some extra verification, we don't care about the
1106                             // performance in this situation.
1107                             byte[] sbuf = new byte[4];
1108                             long cenpos = end.endpos - end.cenlen;
1109                             long locpos = cenpos - end.cenoff;
1110                             if  (cenpos < 0 ||
1111                                  locpos < 0 ||
1112                                  readFullyAt(sbuf, 0, sbuf.length, cenpos) != 4 ||
1113                                  GETSIG(sbuf) != CENSIG ||
1114                                  readFullyAt(sbuf, 0, sbuf.length, locpos) != 4 ||
1115                                  GETSIG(sbuf) != LOCSIG) {
1116                                 continue;
1117                             }
1118                         }
1119                         if (comlen > 0) {    // this zip file has comlen
1120                             comment = new byte[comlen];
1121                             if (readFullyAt(comment, 0, comlen, end.endpos + ENDHDR) != comlen) {
1122                                 zerror("zip comment read failed");
1123                             }
1124                         }
1125                         // must check for a zip64 end record; it is always permitted to be present




1126                         try {
1127                             byte[] loc64 = new byte[ZIP64_LOCHDR];
1128                             if (end.endpos < ZIP64_LOCHDR ||
1129                                 readFullyAt(loc64, 0, loc64.length, end.endpos - ZIP64_LOCHDR)
1130                                 != loc64.length || GETSIG(loc64) != ZIP64_LOCSIG) {
1131                                 return end;
1132                             }
1133                             long end64pos = ZIP64_LOCOFF(loc64);
1134                             byte[] end64buf = new byte[ZIP64_ENDHDR];
1135                             if (readFullyAt(end64buf, 0, end64buf.length, end64pos)
1136                                 != end64buf.length || GETSIG(end64buf) != ZIP64_ENDSIG) {
1137                                 return end;
1138                             }
1139                             // end64 candidate found, 
1140                             long cenlen64 = ZIP64_ENDSIZ(end64buf);
1141                             long cenoff64 = ZIP64_ENDOFF(end64buf);
1142                             long centot64 = ZIP64_ENDTOT(end64buf);
1143                             // double-check
1144                             if (cenlen64 != end.cenlen && end.cenlen != ZIP64_MAGICVAL ||
1145                                 cenoff64 != end.cenoff && end.cenoff != ZIP64_MAGICVAL ||
1146                                 centot64 != end.centot && end.centot != ZIP64_MAGICCOUNT) {
1147                                 return end;
1148                             }
1149                             // to use the end64 values
1150                             end.cenlen = cenlen64;
1151                             end.cenoff = cenoff64;
1152                             end.centot = (int)centot64; // assume total < 2g
1153                             end.endpos = end64pos;
1154                         } catch (IOException x) {}    // no zip64 loc/end

1155                         return end;
1156                     }
1157                 }
1158             }
1159             zerror("zip END header not found");
1160             return null; //make compiler happy
1161         }
1162 
1163         // Reads zip file central directory.
1164         private void initCEN(int knownTotal) throws IOException {
1165             if (knownTotal == -1) {
1166                 End end = findEND();
1167                 if (end.endpos == 0) {
1168                     locpos = 0;
1169                     total = 0;
1170                     entries  = new int[0];
1171                     cen = null;
1172                     return;         // only END header present
1173                 }
1174                 if (end.cenlen > end.endpos)