Search in sources :

Example 1 with TarOutputStream

use of org.kamranzafar.jtar.TarOutputStream in project alliance by codice.

the class OrderRequestImpl method writeTarFile.

private void writeTarFile(DestinationSink destinationSink, PackagingSpecFormatType packagingSpecFormatType, String filename, List<String> sentFiles, ResourceContainer file, List<Metacard> metacards) throws IOException {
    try (TemporaryFileBackedOutputStream tarFos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
        TarOutputStream tarOut = new TarOutputStream(tarFos)) {
        getTar(tarOut, file);
        try (TemporaryFileBackedOutputStream zipFos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
            ZipOutputStream zipOut = new ZipOutputStream(zipFos)) {
            getZip(zipOut, tarFos.asByteSource().openStream(), filename + ".tar");
            ByteSource contents = zipFos.asByteSource();
            writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, contents, metacards);
        }
    }
}
Also used : TarOutputStream(org.kamranzafar.jtar.TarOutputStream) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ByteSource(com.google.common.io.ByteSource)

Example 2 with TarOutputStream

use of org.kamranzafar.jtar.TarOutputStream in project alliance by codice.

the class OrderRequestImpl method writeMultipleFiles.

private void writeMultipleFiles(DestinationSink destinationSink, PackagingSpecFormatType packagingSpecFormatType, List<ResourceContainer> files, String filename, List<String> sentFiles) throws IOException {
    int totalNum = files.size() + 1;
    String totalNumPortion = String.format(FILE_COUNT_FORMAT, totalNum);
    switch(packagingSpecFormatType) {
        case FILESUNC:
            {
                int currNum = 1;
                for (ResourceContainer file : files) {
                    String currNumPortion = String.format(FILE_COUNT_FORMAT, currNum);
                    String currFileName = filename + "." + currNumPortion + "." + totalNumPortion;
                    try (InputStream fileInputStream = file.getInputStream()) {
                        destinationSink.writeFile(fileInputStream, file.getSize(), currFileName, file.getMimeTypeValue(), Collections.singletonList(file.getMetacard()));
                        currNum++;
                        sentFiles.add(currFileName);
                    }
                }
            }
            break;
        case FILESCOMPRESS:
            {
                int currNum = 1;
                for (ResourceContainer file : files) {
                    try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                        ZipOutputStream zipOut = new ZipOutputStream(fos);
                        InputStream fileInputStream = file.getInputStream()) {
                        getZip(zipOut, fileInputStream, file.getName());
                        ByteSource contents = fos.asByteSource();
                        String currNumPortion = String.format(FILE_COUNT_FORMAT, currNum);
                        String currFileName = filename + "." + currNumPortion + "." + totalNumPortion + packagingSpecFormatType.getExtension();
                        try (InputStream inputStream = contents.openStream()) {
                            destinationSink.writeFile(inputStream, contents.size(), currFileName, packagingSpecFormatType.getContentType(), Collections.singletonList(file.getMetacard()));
                            sentFiles.add(currFileName);
                        }
                        currNum++;
                    }
                }
            }
            break;
        case FILESZIP:
            {
                try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                    ZipOutputStream zipOut = new ZipOutputStream(fos)) {
                    getZip(zipOut, files);
                    ByteSource zip = fos.asByteSource();
                    writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, zip, files.stream().map(ResourceContainer::getMetacard).collect(Collectors.toList()));
                }
            }
            break;
        case FILESGZIP:
            {
                int currNum = 1;
                for (ResourceContainer file : files) {
                    try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                        GZIPOutputStream zipOut = new GZIPOutputStream(fos);
                        InputStream fileInputStream = file.getInputStream()) {
                        getGzip(zipOut, fileInputStream);
                        ByteSource contents = fos.asByteSource();
                        String currNumPortion = String.format(FILE_COUNT_FORMAT, currNum);
                        String currFileName = filename + "." + currNumPortion + "." + totalNumPortion + packagingSpecFormatType.getExtension();
                        try (InputStream inputStream = contents.openStream()) {
                            destinationSink.writeFile(inputStream, contents.size(), currFileName, packagingSpecFormatType.getContentType(), Collections.singletonList(file.getMetacard()));
                            sentFiles.add(currFileName);
                        }
                        currNum++;
                    }
                }
            }
            break;
        case TARUNC:
            {
                try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                    TarOutputStream tarOut = new TarOutputStream(fos)) {
                    getTar(tarOut, files);
                    ByteSource tar = fos.asByteSource();
                    writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, tar, files.stream().map(ResourceContainer::getMetacard).collect(Collectors.toList()));
                }
            }
            break;
        case TARZIP:
            {
                try (TemporaryFileBackedOutputStream tarFos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                    TarOutputStream tarOut = new TarOutputStream(tarFos)) {
                    getTar(tarOut, files);
                    try (TemporaryFileBackedOutputStream zipFos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                        ZipOutputStream zipOut = new ZipOutputStream(zipFos)) {
                        getZip(zipOut, tarFos.asByteSource().openStream(), filename + ".tar");
                        ByteSource zip = zipFos.asByteSource();
                        writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, zip, files.stream().map(ResourceContainer::getMetacard).collect(Collectors.toList()));
                    }
                }
            }
            break;
        case TARGZIP:
            {
                try (TemporaryFileBackedOutputStream tarFos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                    TarOutputStream tarOut = new TarOutputStream(tarFos)) {
                    getTar(tarOut, files);
                    try (TemporaryFileBackedOutputStream gzipFos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                        GZIPOutputStream zipOut = new GZIPOutputStream(gzipFos)) {
                        getGzip(zipOut, tarFos.asByteSource().openStream());
                        ByteSource zip = gzipFos.asByteSource();
                        writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, zip, files.stream().map(ResourceContainer::getMetacard).collect(Collectors.toList()));
                    }
                }
            }
            break;
        case TARCOMPRESS:
            {
                try (TemporaryFileBackedOutputStream tarFos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                    TarOutputStream tarOut = new TarOutputStream(tarFos)) {
                    getTar(tarOut, files);
                    try (TemporaryFileBackedOutputStream zipFos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                        ZipOutputStream zipOut = new ZipOutputStream(zipFos)) {
                        getZip(zipOut, tarFos.asByteSource().openStream(), filename + ".tar");
                        writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, zipFos.asByteSource(), files.stream().map(ResourceContainer::getMetacard).collect(Collectors.toList()));
                    }
                }
            }
            break;
        default:
            LOGGER.debug("Unknown packaging format type, skipping");
            break;
    }
}
Also used : TarOutputStream(org.kamranzafar.jtar.TarOutputStream) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) InputStream(java.io.InputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ByteSource(com.google.common.io.ByteSource) NsiliEndpoint(org.codice.alliance.nsili.endpoint.NsiliEndpoint)

Example 3 with TarOutputStream

use of org.kamranzafar.jtar.TarOutputStream in project gradle by gradle.

the class JTarPacker method pack.

@Override
public void pack(List<DataSource> inputs, DataTarget output) throws IOException {
    TarOutputStream tarOutput = new TarOutputStream(output.openOutput());
    for (DataSource input : inputs) {
        @SuppressWarnings("OctalInteger") TarHeader header = TarHeader.createHeader(input.getName(), input.getLength(), 0, false, 0644);
        TarEntry entry = new TarEntry(header);
        tarOutput.putNextEntry(entry);
        PackerUtils.packEntry(input, tarOutput, buffer);
    }
    tarOutput.close();
}
Also used : TarOutputStream(org.kamranzafar.jtar.TarOutputStream) TarHeader(org.kamranzafar.jtar.TarHeader) TarEntry(org.kamranzafar.jtar.TarEntry)

Example 4 with TarOutputStream

use of org.kamranzafar.jtar.TarOutputStream in project bagit-java by LibraryOfCongress.

the class CreateTarBagExample method createTarBagWithJTar.

/**
 * <b> THIS IS JUST AN EXAMPLE. DO NOT USE IN PRODUCTION!</b>
 */
@Test
public void createTarBagWithJTar() {
    try (TarOutputStream out = new TarOutputStream(outputStream)) {
        TarVistor visitor = new TarVistor(out, bagRoot);
        Files.walkFileTree(bagRoot, visitor);
        assertTrue(Files.exists(tarredBagPath));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : TarOutputStream(org.kamranzafar.jtar.TarOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with TarOutputStream

use of org.kamranzafar.jtar.TarOutputStream in project alliance by codice.

the class OrderRequestImpl method writeSingleFile.

private void writeSingleFile(DestinationSink destinationSink, PackagingSpecFormatType packagingSpecFormatType, List<ResourceContainer> files, String filename, List<String> sentFiles) throws IOException {
    ResourceContainer file = files.iterator().next();
    List<Metacard> metacards = Collections.singletonList(file.getMetacard());
    switch(packagingSpecFormatType) {
        case FILESUNC:
            {
                try (InputStream fileInputStream = file.getInputStream()) {
                    destinationSink.writeFile(fileInputStream, file.getSize(), filename, file.getMimeTypeValue(), metacards);
                    sentFiles.add(filename);
                }
            }
            break;
        case FILESCOMPRESS:
            {
                try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                    ZipOutputStream zipOut = new ZipOutputStream(fos);
                    InputStream fileInputStream = file.getInputStream()) {
                    getZip(zipOut, fileInputStream, file.getName());
                    ByteSource contents = fos.asByteSource();
                    writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, contents, metacards);
                }
            }
            break;
        case TARUNC:
            try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                TarOutputStream tarOut = new TarOutputStream(fos)) {
                getTar(tarOut, file);
                ByteSource contents = fos.asByteSource();
                writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, contents, metacards);
            }
            break;
        case TARZIP:
            {
                writeTarFile(destinationSink, packagingSpecFormatType, filename, sentFiles, file, metacards);
            }
            break;
        case FILESZIP:
            try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                GZIPOutputStream zipOut = new GZIPOutputStream(fos);
                InputStream fileInputStream = file.getInputStream()) {
                getGzip(zipOut, fileInputStream);
                ByteSource contents = fos.asByteSource();
                writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, contents, metacards);
            }
            break;
        case TARGZIP:
            {
                try (TemporaryFileBackedOutputStream tarFos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                    TarOutputStream tarOut = new TarOutputStream(tarFos)) {
                    getTar(tarOut, file);
                    try (TemporaryFileBackedOutputStream gzipFos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                        GZIPOutputStream zipOut = new GZIPOutputStream(gzipFos)) {
                        getGzip(zipOut, tarFos.asByteSource().openStream());
                        ByteSource contents = gzipFos.asByteSource();
                        writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, contents, metacards);
                    }
                }
            }
            break;
        case FILESGZIP:
            try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                GZIPOutputStream zipOut = new GZIPOutputStream(fos);
                InputStream fileInputStream = file.getInputStream()) {
                getGzip(zipOut, fileInputStream);
                ByteSource contents = fos.asByteSource();
                writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, contents, metacards);
            }
            break;
        case TARCOMPRESS:
            {
                writeTarFile(destinationSink, packagingSpecFormatType, filename, sentFiles, file, metacards);
            }
            break;
        default:
            LOGGER.debug("Unknown packaging format type, skipping");
            break;
    }
}
Also used : TarOutputStream(org.kamranzafar.jtar.TarOutputStream) Metacard(ddf.catalog.data.Metacard) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) InputStream(java.io.InputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ByteSource(com.google.common.io.ByteSource)

Aggregations

TarOutputStream (org.kamranzafar.jtar.TarOutputStream)6 ByteSource (com.google.common.io.ByteSource)3 InputStream (java.io.InputStream)3 ZipOutputStream (java.util.zip.ZipOutputStream)3 TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)3 IOException (java.io.IOException)2 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 MagiskManager (com.topjohnwu.magisk.MagiskManager)1 TarEntry (com.topjohnwu.magisk.container.TarEntry)1 SuFile (com.topjohnwu.superuser.io.SuFile)1 SuFileInputStream (com.topjohnwu.superuser.io.SuFileInputStream)1 Metacard (ddf.catalog.data.Metacard)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 NsiliEndpoint (org.codice.alliance.nsili.endpoint.NsiliEndpoint)1