in rest-api/src/jetbrains/buildServer/server/rest/data/ArchiveElement.java [79:141]
public StreamingOutput getStreamingOutput(@Nullable final Long startOffset, @Nullable final Long length, final Supplier<String> detailsForLog) {
if (startOffset != null || length != null){
throw new IllegalStateException("Partial streaming is not yet supported");
}
return out -> {
final ZipArchiveOutputStream resultOutput = new ZipArchiveOutputStream(new BufferedOutputStream(out));
resultOutput.setEncoding(null); // TW-12815
int errorsCount = 0;
try {
for (ArtifactTreeElement artifact : myArtifacts) { //todo: need to read-lock artifacts???
if (!artifact.isLeaf()){
//process a directory
String directoryFullName = artifact.getFullName();
if (!directoryFullName.endsWith("/")){
directoryFullName += "/";
}
ZipArchiveEntry entry = new ZipArchiveEntry(directoryFullName);
final Long lastModified = artifact.getLastModified();
if (lastModified != null) entry.setTime(lastModified);
try {
resultOutput.putArchiveEntry(entry);
resultOutput.closeArchiveEntry();
} catch (IOException e) {
errorsCount++;
LOG.warnAndDebugDetails("Error packing directory, ignoring. Directory: '" + artifact.getFullName() + "'", e);
}
}
if (!artifact.isContentAvailable()) continue;
try {
InputStream stream = artifact.getInputStream();
try {
ZipArchiveEntry entry = new ZipArchiveEntry(artifact.getFullName());
final Long lastModified = artifact.getLastModified();
if (lastModified != null) entry.setTime(lastModified); //might need to add more, see com.intellij.util.io.ZipUtil.addFileToZip()
resultOutput.putArchiveEntry(entry);
try {
FileUtil.copyStreams(stream, resultOutput);
} finally {
resultOutput.closeArchiveEntry();
}
} finally {
FileUtil.close(stream);
}
} catch (IOException e) {
errorsCount++;
LOG.warnAndDebugDetails("Error packing artifact, ignoring. File: '" + artifact.getFullName() + "'", e);
}
}
} finally {
if (errorsCount > 0) {
LOG.warn("Encountered " + errorsCount + " errors while processing " + detailsForLog.get());
}
try {
resultOutput.close();
} catch (Exception e) {
LOG.warnAndDebugDetails("Error closing archived stream", e);
}
}
};
}