use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project docker-maven-plugin by fabric8io.
the class ImageArchiveUtilTest method readUnrelatedArchive.
@Test
public void readUnrelatedArchive() throws IOException {
byte[] archiveBytes;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(baos)) {
final byte[] entryData = UUID.randomUUID().toString().getBytes();
TarArchiveEntry tarEntry = new TarArchiveEntry("unrelated.data");
tarEntry.setSize(entryData.length);
tarOutput.putArchiveEntry(tarEntry);
tarOutput.write(entryData);
tarOutput.closeArchiveEntry();
tarOutput.finish();
archiveBytes = baos.toByteArray();
}
ImageArchiveManifest manifest = ImageArchiveUtil.readManifest(new ByteArrayInputStream(archiveBytes));
Assert.assertNull(manifest);
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project docker-maven-plugin by fabric8io.
the class ImageArchiveUtilTest method readValidArchive.
@Test
public void readValidArchive() throws IOException {
final byte[] entryData = new Gson().toJson(createBasicManifestJson()).getBytes(StandardCharsets.UTF_8);
final byte[] relatedData = new Gson().toJson(new JsonObject()).getBytes(StandardCharsets.UTF_8);
byte[] archiveBytes;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(baos)) {
TarArchiveEntry tarEntry;
tarEntry = new TarArchiveEntry("image-id-sha256.json");
tarEntry.setSize(relatedData.length);
tarOutput.putArchiveEntry(tarEntry);
tarOutput.write(relatedData);
tarOutput.closeArchiveEntry();
tarEntry = new TarArchiveEntry(ImageArchiveUtil.MANIFEST_JSON);
tarEntry.setSize(entryData.length);
tarOutput.putArchiveEntry(tarEntry);
tarOutput.write(entryData);
tarOutput.closeArchiveEntry();
tarOutput.finish();
archiveBytes = baos.toByteArray();
}
ImageArchiveManifest manifest = ImageArchiveUtil.readManifest(new ByteArrayInputStream(archiveBytes));
Assert.assertNotNull(manifest);
Assert.assertNotNull(manifest.getEntries());
Assert.assertFalse(manifest.getEntries().isEmpty());
ImageArchiveManifestEntry entry = manifest.getEntries().get(0);
Assert.assertNotNull(entry);
Assert.assertEquals("image-id-sha256.json", entry.getConfig());
Assert.assertEquals("image-id-sha256", entry.getId());
Assert.assertNotNull(entry.getRepoTags());
Assert.assertEquals(Collections.singletonList("test/image:latest"), entry.getRepoTags());
Assert.assertNotNull(entry.getLayers());
Assert.assertEquals(Collections.singletonList("layer-id-sha256/layer.tar"), entry.getLayers());
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project che by eclipse.
the class TarArchiver method compress.
@Override
public void compress(OutputStream tarOutput, VirtualFileFilter filter) throws IOException, ServerException {
try (TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(tarOutput)) {
tarOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
folder.accept(new VirtualFileVisitor() {
@Override
public void visit(VirtualFile visitedVirtualFile) throws ServerException {
if (filter.accept(visitedVirtualFile)) {
if (!visitedVirtualFile.equals(folder)) {
addTarEntry(visitedVirtualFile, tarOutputStream);
}
if (visitedVirtualFile.isFolder()) {
for (VirtualFile child : visitedVirtualFile.getChildren()) {
child.accept(this);
}
}
}
}
});
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project camel by apache.
the class TarFileDataFormat method marshal.
@Override
public void marshal(final Exchange exchange, final Object graph, final OutputStream stream) throws Exception {
String filename = exchange.getIn().getHeader(FILE_NAME, String.class);
Long filelength = exchange.getIn().getHeader(FILE_LENGTH, Long.class);
if (filename == null) {
// generate the file name as the camel file component would do
filename = StringHelper.sanitize(exchange.getIn().getMessageId());
} else {
// remove any path elements
filename = Paths.get(filename).getFileName().toString();
}
TarArchiveOutputStream tos = new TarArchiveOutputStream(stream);
tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, graph);
if (filelength == null) {
filelength = (long) is.available();
}
TarArchiveEntry entry = new TarArchiveEntry(filename);
entry.setSize(filelength);
tos.putArchiveEntry(entry);
try {
IOHelper.copy(is, tos);
} finally {
tos.closeArchiveEntry();
IOHelper.close(is, tos);
}
String newFilename = filename + ".tar";
exchange.getOut().setHeader(FILE_NAME, newFilename);
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project camel by apache.
the class TarAggregationStrategy method addEntryToTar.
private void addEntryToTar(File source, String entryName, byte[] buffer, int length) throws IOException, ArchiveException {
File tmpTar = File.createTempFile(source.getName(), null, parentDir);
tmpTar.delete();
if (!source.renameTo(tmpTar)) {
throw new IOException("Cannot create temp file: " + source.getName());
}
FileInputStream fis = new FileInputStream(tmpTar);
TarArchiveInputStream tin = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, fis);
TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(source));
tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
// copy the existing entries
ArchiveEntry nextEntry;
while ((nextEntry = tin.getNextEntry()) != null) {
tos.putArchiveEntry(nextEntry);
IOUtils.copy(tin, tos);
tos.closeArchiveEntry();
}
// Create new entry
TarArchiveEntry entry = new TarArchiveEntry(entryName);
entry.setSize(length);
tos.putArchiveEntry(entry);
tos.write(buffer, 0, length);
tos.closeArchiveEntry();
IOHelper.close(fis, tin, tos);
LOG.trace("Deleting temporary file: {}", tmpTar);
FileUtil.deleteFile(tmpTar);
}
Aggregations