in sources/java-incremental-compilation/jvm-inc-builder/src/com/intellij/tools/build/bazel/jvmIncBuilder/impl/ZipOutputBuilderImpl.java [161:224]
public void close(boolean saveChanges) throws IOException {
try {
if (!myHasChanges || !saveChanges) {
if (myReadZipFile != null) {
myReadZipFile.close();
if (saveChanges && !myReadZipPath.equals(myWriteZipPath)) {
// ensure content at the destination path is the same as the source content
if (!Files.exists(myWriteZipPath) || !Files.isSameFile(myReadZipPath, myWriteZipPath)) {
Files.copy(myReadZipPath, myWriteZipPath, StandardCopyOption.REPLACE_EXISTING);
}
}
}
else {
if (saveChanges && !Files.exists(myWriteZipPath)) {
// ensure an empty output file exists, even if there are no changes (bazel requirement)
try (var zos = new ZipOutputStream(openOutputStream(myWriteZipPath))) {
zos.setMethod(ZipOutputStream.STORED);
}
}
}
}
else {
boolean useTempOutput = myReadZipFile != null /*srcZip exists*/ && Files.exists(myWriteZipPath) && Files.isSameFile(myReadZipPath, myWriteZipPath);
Path outputPath = useTempOutput? getTempOutputPath() : myWriteZipPath;
try {
if (myCreateIndex) {
writeClassFilesIfNeeded(myEntries);
saveToIndexedArchive(outputPath);
}
else {
// augment entry map with all currently present directory entries
for (String dirName : myDirIndex.keySet()) {
ZipEntry existingEntry = myExistingDirectories.get(dirName);
if (existingEntry == null && "/".equals(dirName)) {
continue; // keep root '/' entry if it were present in the original zip
}
if (existingEntry != null) {
myEntries.put(dirName, createEntryData(myReadZipFile, existingEntry));
}
else {
myEntries.put(dirName, createEntryData(dirName, EntryData.NO_DATA_BYTES));
}
}
saveToArchive(outputPath);
}
}
finally {
if (myReadZipFile != null) {
myReadZipFile.close();
}
if (useTempOutput) {
Files.move(outputPath, myWriteZipPath, StandardCopyOption.REPLACE_EXISTING);
}
}
}
}
finally {
myExistingDirectories.clear();
myDirIndex.clear();
myEntries.clear();
mySwap.clear();
}
}