17f2f5973SJonathan Nieder /* 25c5f7c6bSMatthias Sohn * Copyright (C) 2013 Google Inc. and others 37f2f5973SJonathan Nieder * 45c5f7c6bSMatthias Sohn * This program and the accompanying materials are made available under the 55c5f7c6bSMatthias Sohn * terms of the Eclipse Distribution License v. 1.0 which is available at 65c5f7c6bSMatthias Sohn * https://www.eclipse.org/org/documents/edl-v10.php. 77f2f5973SJonathan Nieder * 85c5f7c6bSMatthias Sohn * SPDX-License-Identifier: BSD-3-Clause 97f2f5973SJonathan Nieder */ 107f2f5973SJonathan Nieder package org.eclipse.jgit.archive; 117f2f5973SJonathan Nieder 127f2f5973SJonathan Nieder import java.io.IOException; 137f2f5973SJonathan Nieder import java.io.OutputStream; 1456cb2d92SJonathan Nieder import java.util.Arrays; 1556cb2d92SJonathan Nieder import java.util.Collections; 1656cb2d92SJonathan Nieder import java.util.List; 17c0c4c6f0SDavid Ostrovsky import java.util.Map; 187f2f5973SJonathan Nieder 197f2f5973SJonathan Nieder import org.apache.commons.compress.archivers.ArchiveOutputStream; 207f2f5973SJonathan Nieder import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; 21*6f82690aSYoussef Elghareeb import org.apache.commons.compress.compressors.gzip.GzipParameters; 227f2f5973SJonathan Nieder import org.eclipse.jgit.api.ArchiveCommand; 237f2f5973SJonathan Nieder import org.eclipse.jgit.lib.FileMode; 241448ec37SNaoki Takezoe import org.eclipse.jgit.lib.ObjectId; 257f2f5973SJonathan Nieder import org.eclipse.jgit.lib.ObjectLoader; 267f2f5973SJonathan Nieder 27659cadf0SJonathan Nieder /** 28659cadf0SJonathan Nieder * gzip-compressed tarball (tar.gz) format. 29659cadf0SJonathan Nieder */ 30c0c4c6f0SDavid Ostrovsky public final class TgzFormat extends BaseFormat implements 31c0c4c6f0SDavid Ostrovsky ArchiveCommand.Format<ArchiveOutputStream> { 324ceb25b6SRobin Rosenberg private static final List<String> SUFFIXES = Collections 334ceb25b6SRobin Rosenberg .unmodifiableList(Arrays.asList(".tar.gz", ".tgz")); //$NON-NLS-1$ //$NON-NLS-2$ 3456cb2d92SJonathan Nieder 357f2f5973SJonathan Nieder private final ArchiveCommand.Format<ArchiveOutputStream> tarFormat = new TarFormat(); 367f2f5973SJonathan Nieder 3732022e97SMatthias Sohn /** {@inheritDoc} */ 3830628e3bSMatthias Sohn @Override createArchiveOutputStream(OutputStream s)397f2f5973SJonathan Nieder public ArchiveOutputStream createArchiveOutputStream(OutputStream s) 407f2f5973SJonathan Nieder throws IOException { 41c0c4c6f0SDavid Ostrovsky return createArchiveOutputStream(s, 42c0c4c6f0SDavid Ostrovsky Collections.<String, Object> emptyMap()); 43c0c4c6f0SDavid Ostrovsky } 44c0c4c6f0SDavid Ostrovsky 4532022e97SMatthias Sohn /** {@inheritDoc} */ 4630628e3bSMatthias Sohn @Override createArchiveOutputStream(OutputStream s, Map<String, Object> o)47c0c4c6f0SDavid Ostrovsky public ArchiveOutputStream createArchiveOutputStream(OutputStream s, 48c0c4c6f0SDavid Ostrovsky Map<String, Object> o) throws IOException { 49*6f82690aSYoussef Elghareeb GzipCompressorOutputStream out; 50*6f82690aSYoussef Elghareeb int compressionLevel = getCompressionLevel(o); 51*6f82690aSYoussef Elghareeb if (compressionLevel != -1) { 52*6f82690aSYoussef Elghareeb GzipParameters parameters = new GzipParameters(); 53*6f82690aSYoussef Elghareeb parameters.setCompressionLevel(compressionLevel); 54*6f82690aSYoussef Elghareeb out = new GzipCompressorOutputStream(s, parameters); 55*6f82690aSYoussef Elghareeb } else { 56*6f82690aSYoussef Elghareeb out = new GzipCompressorOutputStream(s); 57*6f82690aSYoussef Elghareeb } 58c0c4c6f0SDavid Ostrovsky return tarFormat.createArchiveOutputStream(out, o); 597f2f5973SJonathan Nieder } 607f2f5973SJonathan Nieder 6132022e97SMatthias Sohn /** {@inheritDoc} */ 621448ec37SNaoki Takezoe @Override putEntry(ArchiveOutputStream out, ObjectId tree, String path, FileMode mode, ObjectLoader loader)631448ec37SNaoki Takezoe public void putEntry(ArchiveOutputStream out, 641448ec37SNaoki Takezoe ObjectId tree, String path, FileMode mode, ObjectLoader loader) 651448ec37SNaoki Takezoe throws IOException { 661448ec37SNaoki Takezoe tarFormat.putEntry(out, tree, path, mode, loader); 677f2f5973SJonathan Nieder } 6856cb2d92SJonathan Nieder 6932022e97SMatthias Sohn /** {@inheritDoc} */ 7030628e3bSMatthias Sohn @Override suffixes()7156cb2d92SJonathan Nieder public Iterable<String> suffixes() { 7256cb2d92SJonathan Nieder return SUFFIXES; 7356cb2d92SJonathan Nieder } 740a14909bSJonathan Nieder 7532022e97SMatthias Sohn /** {@inheritDoc} */ 760a14909bSJonathan Nieder @Override equals(Object other)770a14909bSJonathan Nieder public boolean equals(Object other) { 780a14909bSJonathan Nieder return (other instanceof TgzFormat); 790a14909bSJonathan Nieder } 800a14909bSJonathan Nieder 8132022e97SMatthias Sohn /** {@inheritDoc} */ 820a14909bSJonathan Nieder @Override hashCode()830a14909bSJonathan Nieder public int hashCode() { 840a14909bSJonathan Nieder return getClass().hashCode(); 850a14909bSJonathan Nieder } 867f2f5973SJonathan Nieder } 87