下面列出了java.util.zip.ZipEntry#setCreationTime ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Write model jar file with nullability model at DEFAULT_ASTUBX_LOCATION
*
* @param outPath Path of output model jar file.
*/
private void writeModelJAR(String outPath) throws IOException {
Preconditions.checkArgument(
outPath.endsWith(ASTUBX_JAR_SUFFIX), "invalid model file path! " + outPath);
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outPath));
if (!nonnullParams.isEmpty()) {
ZipEntry entry = new ZipEntry(DEFAULT_ASTUBX_LOCATION);
// Set the modification/creation time to 0 to ensure that this jars always have the same
// checksum
entry.setTime(0);
entry.setCreationTime(FileTime.fromMillis(0));
zos.putNextEntry(entry);
writeModel(new DataOutputStream(zos));
zos.closeEntry();
}
zos.close();
LOG(VERBOSE, "Info", "wrote model to: " + outPath);
}
@Override
public void addFile(@NonNull String relativePath, byte[] bytes, int offset, int end)
throws IOException {
Preconditions.checkNotNull(jarOutputStream, "Archive is not writeable : %s", targetPath);
// Need to pre-compute checksum for STORED (uncompressed) entries)
CRC32 checksum = new CRC32();
checksum.update(bytes, offset, end);
ZipEntry zipEntry = new ZipEntry(relativePath);
zipEntry.setLastModifiedTime(ZERO_TIME);
zipEntry.setLastAccessTime(ZERO_TIME);
zipEntry.setCreationTime(ZERO_TIME);
zipEntry.setCrc(checksum.getValue());
zipEntry.setSize(end - offset);
zipEntry.setCompressedSize(end - offset);
zipEntry.setMethod(ZipEntry.STORED);
jarOutputStream.putNextEntry(zipEntry);
jarOutputStream.write(bytes, offset, end);
jarOutputStream.flush();
jarOutputStream.closeEntry();
}
private ZipEntry cloneEntry(ZipEntry entry) {
ZipEntry newEntry = new ZipEntry(entry.getName());
newEntry.setTime(entry.getTime());
if (entry.getCreationTime() != null) {
newEntry.setCreationTime(entry.getCreationTime());
}
if (entry.getLastModifiedTime() != null) {
newEntry.setLastModifiedTime(entry.getLastModifiedTime());
}
if (entry.getLastAccessTime() != null) {
newEntry.setLastAccessTime(entry.getLastAccessTime());
}
newEntry.setComment(entry.getComment());
newEntry.setExtra(entry.getExtra());
return newEntry;
}
/**
* Create the in-memory JAR containing all the classes in this module.
*/
public byte[] createJar(long blockTimeStamp) throws IOException {
// set jar file timestamp to block timestamp so the whole network is in agreement over this.
FileTime timestamp = FileTime.fromMillis(blockTimeStamp);
// manifest, we explicitly write it so that can can control its timestamps.
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, this.mainClass);
ZipEntry manifestEntry = new ZipEntry(JarFile.MANIFEST_NAME);
manifestEntry.setLastModifiedTime(timestamp);
manifestEntry.setLastAccessTime(timestamp);
manifestEntry.setCreationTime(timestamp);
// Create a temporary memory location for this JAR.
ByteArrayOutputStream tempJarStream = new ByteArrayOutputStream(MAX_JAR_BYTES);
// create the jar file
try (JarOutputStream target = new JarOutputStream(tempJarStream)) {
// first, write the manifest file
target.putNextEntry(manifestEntry);
manifest.write(target);
target.closeEntry();
// add the classes
for (String clazz : this.classes.keySet()) {
JarEntry entry = new JarEntry(clazz.replace('.', '/') + ".class");
entry.setLastModifiedTime(timestamp);
entry.setLastAccessTime(timestamp);
entry.setCreationTime(timestamp);
target.putNextEntry(entry);
target.write(this.classes.get(clazz));
target.closeEntry();
}
}
return tempJarStream.toByteArray();
}
@NonNull
private ExceptionRunnable createFile(
@NonNull File input, @NonNull File output, @NonNull TransformInvocation invocation) {
return () -> {
try (ZipFile inputZip = new ZipFile(input);
ZipOutputStream outputZip =
new ZipOutputStream(
new BufferedOutputStream(
Files.newOutputStream(output.toPath())))) {
Enumeration<? extends ZipEntry> inEntries = inputZip.entries();
while (inEntries.hasMoreElements()) {
ZipEntry entry = inEntries.nextElement();
if (!entry.getName().endsWith(SdkConstants.DOT_CLASS)) {
continue;
}
InputStream originalFile =
new BufferedInputStream(inputZip.getInputStream(entry));
ZipEntry outEntry = new ZipEntry(entry.getName());
byte[] newEntryContent =
getFixedClass(originalFile, getClassLoader(invocation));
CRC32 crc32 = new CRC32();
crc32.update(newEntryContent);
outEntry.setCrc(crc32.getValue());
outEntry.setMethod(ZipEntry.STORED);
outEntry.setSize(newEntryContent.length);
outEntry.setCompressedSize(newEntryContent.length);
outEntry.setLastAccessTime(ZERO);
outEntry.setLastModifiedTime(ZERO);
outEntry.setCreationTime(ZERO);
outputZip.putNextEntry(outEntry);
outputZip.write(newEntryContent);
outputZip.closeEntry();
}
}
};
}
private void putArtifacts ( final ZipOutputStream zos, final String baseName, final ReadableChannel channel, final Collection<? extends ArtifactInformation> inputArtifacts, final boolean onlyRoot ) throws IOException
{
final List<ArtifactInformation> artifacts = new ArrayList<> ( inputArtifacts ); // make new instance in order to sort them
Collections.sort ( artifacts, Comparator.comparing ( ArtifactInformation::getId ) );
for ( final ArtifactInformation art : artifacts )
{
if ( !art.is ( "stored" ) )
{
continue;
}
if ( onlyRoot && art.getParentId () != null )
{
// only root artifacts in this run
continue;
}
final String name = String.format ( "%s%s/", baseName, art.getId () );
// make a dir entry
{
final ZipEntry ze = new ZipEntry ( name );
ze.setComment ( art.getName () );
final FileTime timestamp = FileTime.fromMillis ( art.getCreationTimestamp ().getTime () );
ze.setLastModifiedTime ( timestamp );
ze.setCreationTime ( timestamp );
ze.setLastAccessTime ( timestamp );
zos.putNextEntry ( ze );
zos.closeEntry ();
}
// put the provided properties
putProperties ( zos, name + "properties.xml", art.getProvidedMetaData () );
putDataEntry ( zos, name + "name", art.getName () );
if ( art.is ( "generator" ) )
{
putDataEntry ( zos, name + "generator", art.getVirtualizerAspectId () );
}
// put the blob
try
{
channel.getContext ().stream ( art.getId (), in -> {
zos.putNextEntry ( new ZipEntry ( name + "data" ) );
ByteStreams.copy ( in, zos );
zos.closeEntry ();
} );
}
catch ( final Exception e )
{
throw new IOException ( "Failed to export artifact", e );
}
// further calls will process all artifacts but only get a filtered list
final List<? extends ArtifactInformation> childs = art.getChildIds ().stream ().map ( id -> channel.getArtifact ( id ) ).filter ( opt -> opt.isPresent () ).map ( opt -> opt.get () ).collect ( Collectors.toList () );
// put children of this entry
putArtifacts ( zos, name, channel, childs, false );
}
}