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);
}
}
}
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;
}
}
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();
}
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();
}
}
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;
}
}
Aggregations