|
| 1 | +package fi.helsinki.cs.tmc.langs.util.tarservice; |
| 2 | + |
| 3 | +import static org.apache.commons.compress.archivers.tar.TarArchiveOutputStream.LONGFILE_POSIX; |
| 4 | + |
| 5 | +import org.apache.commons.compress.archivers.ArchiveException; |
| 6 | +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; |
| 7 | +import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; |
| 8 | +import org.apache.commons.compress.utils.IOUtils; |
| 9 | + |
| 10 | +import java.io.FileInputStream; |
| 11 | +import java.io.FileNotFoundException; |
| 12 | +import java.io.FileOutputStream; |
| 13 | +import java.io.IOException; |
| 14 | +import java.nio.file.Files; |
| 15 | +import java.nio.file.Path; |
| 16 | + |
| 17 | +public class TarCreator { |
| 18 | + |
| 19 | + /** |
| 20 | + * Copies the location of tmc-langs-cli and tmcrun to unzipped project folder |
| 21 | + * and creates a tarball. |
| 22 | + * |
| 23 | + * @param projectDir Location of unzipped project dir |
| 24 | + * @param tmcLangs Location of tmc-langs-cli.jar |
| 25 | + * @param tmcrun Location of tmc-run init script |
| 26 | + * @param targetLocation Location where the tar archive should be extracted to |
| 27 | + * @throws IOException Error! |
| 28 | + * @throws ArchiveException Error! |
| 29 | + */ |
| 30 | + public void createTarFromProject(Path projectDir, Path tmcLangs, Path tmcrun, |
| 31 | + Path targetLocation) throws IOException, ArchiveException { |
| 32 | + Files.copy(tmcrun, projectDir.resolve(tmcrun.getFileName())); |
| 33 | + Files.copy(tmcLangs, projectDir.resolve(tmcLangs.getFileName())); |
| 34 | + createTarBall(projectDir, targetLocation); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Creates a tarball from a directory. |
| 39 | + * |
| 40 | + * @param project Project directory file |
| 41 | + * @param targetLocation Location where the tar archive should be extracted to |
| 42 | + * @throws IOException Error! |
| 43 | + * @throws ArchiveException Error! |
| 44 | + */ |
| 45 | + private void createTarBall(Path project, Path targetLocation) |
| 46 | + throws IOException, ArchiveException { |
| 47 | + try (FileOutputStream tarOut = new FileOutputStream(targetLocation.toString())) { |
| 48 | + try (TarArchiveOutputStream aos = new TarArchiveOutputStream(tarOut)) { |
| 49 | + aos.setLongFileMode(LONGFILE_POSIX); |
| 50 | + addFilesToTarBall(project, aos, project); |
| 51 | + aos.finish(); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Adds all files and folders inside a folder to a tar file. |
| 58 | + * |
| 59 | + * @param folder The folder to add |
| 60 | + * @param tar TarArchiveOutputStreamer tar |
| 61 | + * @param lengthOfPath The length of String from root until the start folder. |
| 62 | + * @throws FileNotFoundException Error! |
| 63 | + * @throws IOException Error! |
| 64 | + */ |
| 65 | + private void addFilesToTarBall(Path folder, TarArchiveOutputStream tar, |
| 66 | + Path basePath) throws FileNotFoundException, IOException { |
| 67 | + for (Path path : Files.newDirectoryStream(folder)) { |
| 68 | + if (Files.isDirectory(path)) { |
| 69 | + addFilesToTarBall(path, tar, basePath); |
| 70 | + } else { |
| 71 | + TarArchiveEntry entry = new TarArchiveEntry(basePath.relativize(path).toString()); |
| 72 | + entry.setSize(path.toFile().length()); |
| 73 | + tar.putArchiveEntry(entry); |
| 74 | + try (FileInputStream fis = new FileInputStream(path.toFile())) { |
| 75 | + IOUtils.copy(fis, tar); |
| 76 | + } |
| 77 | + tar.closeArchiveEntry(); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments