publicstaticvoid main(String[] args) throws Exception { // create a zip file, and read it in as a byte array
Path path = Files.createTempFile("bad", ".zip"); try (OutputStream os = Files.newOutputStream(path);
ZipOutputStream zos = new ZipOutputStream(os)) {
ZipEntry e = new ZipEntry("x");
zos.putNextEntry(e);
zos.write((int) 'x');
} int len = (int) Files.size(path); byte[] data = newbyte[len]; try (InputStream is = Files.newInputStream(path)) {
is.read(data);
}
Files.delete(path);
// year, month, day are zero
testDate(data.clone(), 0, LocalDate.of(1979, 11, 30).atStartOfDay()); // only year is zero
testDate(data.clone(), 0 << 25 | 4 << 21 | 5 << 16, LocalDate.of(1980, 4, 5).atStartOfDay()); // month is greater than 12
testDate(data.clone(), 0 << 25 | 13 << 21 | 1 << 16, LocalDate.of(1981, 1, 1).atStartOfDay()); // 30th of February
testDate(data.clone(), 0 << 25 | 2 << 21 | 30 << 16, LocalDate.of(1980, 3, 1).atStartOfDay()); // 30th of February, 24:60:60
testDate(data.clone(), 0 << 25 | 2 << 21 | 30 << 16 | 24 << 11 | 60 << 5 | 60 >> 1,
LocalDateTime.of(1980, 3, 2, 1, 1, 0));
}
privatestaticvoid testDate(byte[] data, int date, LocalDateTime expected) throws IOException { // set the datetime int endpos = data.length - ENDHDR; int cenpos = u16(data, endpos + ENDOFF); int locpos = u16(data, cenpos + CENOFF);
writeU32(data, cenpos + CENTIM, date);
writeU32(data, locpos + LOCTIM, date);
// ensure that the archive is still readable, and the date is 1979-11-30
Path path = Files.createTempFile("out", ".zip"); try (OutputStream os = Files.newOutputStream(path)) {
os.write(data);
} try (ZipFile zf = new ZipFile(path.toFile())) {
ZipEntry ze = zf.entries().nextElement();
Instant actualInstant = ze.getLastModifiedTime().toInstant();
Instant expectedInstant = expected.atZone(ZoneId.systemDefault()).toInstant(); if (!actualInstant.equals(expectedInstant)) { thrownew AssertionError(
String.format("actual: %s, expected: %s", actualInstant, expectedInstant));
}
} finally {
Files.delete(path);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.