use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project OpenRefine by OpenRefine.
the class FileProjectManager method untar.
protected void untar(File destDir, InputStream inputStream) throws IOException {
TarArchiveInputStream tin = new TarArchiveInputStream(inputStream);
TarArchiveEntry tarEntry = null;
while ((tarEntry = tin.getNextTarEntry()) != null) {
File destEntry = new File(destDir, tarEntry.getName());
File parent = destEntry.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
if (tarEntry.isDirectory()) {
destEntry.mkdirs();
} else {
FileOutputStream fout = new FileOutputStream(destEntry);
try {
IOUtils.copy(tin, fout);
} finally {
fout.close();
}
}
}
tin.close();
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project spring-boot by spring-projects.
the class TarLayoutWriterTests method writesTarArchive.
@Test
void writesTarArchive() throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (TarLayoutWriter writer = new TarLayoutWriter(outputStream)) {
writer.directory("/foo", Owner.ROOT);
writer.file("/foo/bar.txt", Owner.of(1, 1), 0777, Content.of("test"));
}
try (TarArchiveInputStream tarInputStream = new TarArchiveInputStream(new ByteArrayInputStream(outputStream.toByteArray()))) {
TarArchiveEntry directoryEntry = tarInputStream.getNextTarEntry();
TarArchiveEntry fileEntry = tarInputStream.getNextTarEntry();
byte[] fileContent = new byte[(int) fileEntry.getSize()];
tarInputStream.read(fileContent);
assertThat(tarInputStream.getNextEntry()).isNull();
assertThat(directoryEntry.getName()).isEqualTo("/foo/");
assertThat(directoryEntry.getMode()).isEqualTo(0755);
assertThat(directoryEntry.getLongUserId()).isEqualTo(0);
assertThat(directoryEntry.getLongGroupId()).isEqualTo(0);
assertThat(directoryEntry.getModTime()).isEqualTo(new Date(TarLayoutWriter.NORMALIZED_MOD_TIME));
assertThat(fileEntry.getName()).isEqualTo("/foo/bar.txt");
assertThat(fileEntry.getMode()).isEqualTo(0777);
assertThat(fileEntry.getLongUserId()).isEqualTo(1);
assertThat(fileEntry.getLongGroupId()).isEqualTo(1);
assertThat(fileEntry.getModTime()).isEqualTo(new Date(TarLayoutWriter.NORMALIZED_MOD_TIME));
assertThat(fileContent).isEqualTo("test".getBytes(StandardCharsets.UTF_8));
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project spring-boot by spring-projects.
the class TarArchiveTests method ofWritesTarContent.
@Test
void ofWritesTarContent() throws Exception {
Owner owner = Owner.of(123, 456);
TarArchive tarArchive = TarArchive.of((content) -> {
content.directory("/workspace", owner);
content.directory("/layers", owner);
content.directory("/cnb", Owner.ROOT);
content.directory("/cnb/buildpacks", Owner.ROOT);
content.directory("/platform", Owner.ROOT);
content.directory("/platform/env", Owner.ROOT);
});
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
tarArchive.writeTo(outputStream);
try (TarArchiveInputStream tarStream = new TarArchiveInputStream(new ByteArrayInputStream(outputStream.toByteArray()))) {
List<TarArchiveEntry> entries = new ArrayList<>();
TarArchiveEntry entry = tarStream.getNextTarEntry();
while (entry != null) {
entries.add(entry);
entry = tarStream.getNextTarEntry();
}
assertThat(entries).hasSize(6);
assertThat(entries.get(0).getName()).isEqualTo("/workspace/");
assertThat(entries.get(0).getLongUserId()).isEqualTo(123);
assertThat(entries.get(0).getLongGroupId()).isEqualTo(456);
assertThat(entries.get(2).getName()).isEqualTo("/cnb/");
assertThat(entries.get(2).getLongUserId()).isEqualTo(0);
assertThat(entries.get(2).getLongGroupId()).isEqualTo(0);
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project spring-boot by spring-projects.
the class ZipFileTarArchiveTests method writeToAdaptsContent.
@Test
void writeToAdaptsContent() throws Exception {
Owner owner = Owner.of(123, 456);
File file = new File(this.tempDir, "test.zip");
writeTestZip(file);
TarArchive tarArchive = TarArchive.fromZip(file, owner);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
tarArchive.writeTo(outputStream);
try (TarArchiveInputStream tarStream = new TarArchiveInputStream(new ByteArrayInputStream(outputStream.toByteArray()))) {
TarArchiveEntry dirEntry = tarStream.getNextTarEntry();
assertThat(dirEntry.getName()).isEqualTo("spring/");
assertThat(dirEntry.getLongUserId()).isEqualTo(123);
assertThat(dirEntry.getLongGroupId()).isEqualTo(456);
TarArchiveEntry fileEntry = tarStream.getNextTarEntry();
assertThat(fileEntry.getName()).isEqualTo("spring/boot");
assertThat(fileEntry.getLongUserId()).isEqualTo(123);
assertThat(fileEntry.getLongGroupId()).isEqualTo(456);
assertThat(fileEntry.getSize()).isEqualTo(4);
assertThat(fileEntry.getMode()).isEqualTo(0755);
assertThat(tarStream).hasContent("test");
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project spring-boot by spring-projects.
the class DirectoryBuildpackTests method assertHasExpectedLayers.
private void assertHasExpectedLayers(Buildpack buildpack) throws IOException {
List<ByteArrayOutputStream> layers = new ArrayList<>();
buildpack.apply((layer) -> {
ByteArrayOutputStream out = new ByteArrayOutputStream();
layer.writeTo(out);
layers.add(out);
});
assertThat(layers).hasSize(1);
byte[] content = layers.get(0).toByteArray();
try (TarArchiveInputStream tar = new TarArchiveInputStream(new ByteArrayInputStream(content))) {
List<TarArchiveEntry> entries = new ArrayList<>();
TarArchiveEntry entry = tar.getNextTarEntry();
while (entry != null) {
entries.add(entry);
entry = tar.getNextTarEntry();
}
assertThat(entries).extracting("name", "mode").containsExactlyInAnyOrder(tuple("/cnb/", 0755), tuple("/cnb/buildpacks/", 0755), tuple("/cnb/buildpacks/example_buildpack1/", 0755), tuple("/cnb/buildpacks/example_buildpack1/0.0.1/", 0755), tuple("/cnb/buildpacks/example_buildpack1/0.0.1/buildpack.toml", 0644), tuple("/cnb/buildpacks/example_buildpack1/0.0.1/bin/", 0755), tuple("/cnb/buildpacks/example_buildpack1/0.0.1/bin/detect", 0744), tuple("/cnb/buildpacks/example_buildpack1/0.0.1/bin/build", 0744));
}
}
Aggregations