Search in sources :

Example 66 with TarArchiveOutputStream

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) ImageArchiveManifest(io.fabric8.maven.docker.model.ImageArchiveManifest) Test(org.junit.Test)

Example 67 with TarArchiveOutputStream

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) ImageArchiveManifest(io.fabric8.maven.docker.model.ImageArchiveManifest) ImageArchiveManifestEntry(io.fabric8.maven.docker.model.ImageArchiveManifestEntry) Test(org.junit.Test)

Example 68 with TarArchiveOutputStream

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);
                        }
                    }
                }
            }
        });
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)

Example 69 with TarArchiveOutputStream

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);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) InputStream(java.io.InputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 70 with TarArchiveOutputStream

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);
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) FileOutputStream(java.io.FileOutputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) IOException(java.io.IOException) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Aggregations

TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)101 File (java.io.File)42 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)42 FileOutputStream (java.io.FileOutputStream)37 GzipCompressorOutputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream)30 BufferedOutputStream (java.io.BufferedOutputStream)22 IOException (java.io.IOException)21 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 FileInputStream (java.io.FileInputStream)19 Path (java.nio.file.Path)17 GZIPOutputStream (java.util.zip.GZIPOutputStream)17 ByteArrayInputStream (java.io.ByteArrayInputStream)15 OutputStream (java.io.OutputStream)13 Test (org.junit.Test)12 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)11 ArchiveOutputStream (org.apache.commons.compress.archivers.ArchiveOutputStream)11 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)9 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)9 ImageArchiveManifest (io.fabric8.maven.docker.model.ImageArchiveManifest)8 InputStream (java.io.InputStream)7