Skip to content

Commit

Permalink
Merge pull request #2739 from DamnClin/avoid-tempp-zip-file
Browse files Browse the repository at this point in the history
Avoid temp zip file for project download
  • Loading branch information
pascalgrimaud authored Jul 22, 2022
2 parents f9bd75e + d06f3fe commit b390dcd
Showing 1 changed file with 7 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tech.jhipster.lite.project.infrastructure.secondary;

import java.io.File;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -35,7 +35,7 @@ public Optional<Project> get(ProjectPath path) {
try {
ProjectName projectName = readProjectName(source);

byte[] content = readAndDelete(zip(source));
byte[] content = zip(source);

return Optional.of(new Project(projectName, content));
} catch (IOException | ZipException e) {
Expand Down Expand Up @@ -63,19 +63,11 @@ private ProjectName readProjectName(Path source) throws IOException {
return new ProjectName(matcher.group(1));
}

private Path zip(Path source) throws IOException {
File compressedFile = File.createTempFile("download-", ".zip");
private byte[] zip(Path source) throws IOException {
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
ZipUtil.pack(source.toFile(), out);

ZipUtil.pack(source.toFile(), compressedFile);

return compressedFile.toPath();
}

private byte[] readAndDelete(Path compressedFilePath) throws IOException {
byte[] content = Files.readAllBytes(compressedFilePath);

Files.delete(compressedFilePath);

return content;
return out.toByteArray();
}
}
}

0 comments on commit b390dcd

Please sign in to comment.