View Javadoc
1   /*
2    * Copyright (C) 2013 Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.archive;
11  
12  import java.io.IOException;
13  import java.io.OutputStream;
14  import java.util.Arrays;
15  import java.util.Collections;
16  import java.util.List;
17  import java.util.Map;
18  
19  import org.apache.commons.compress.archivers.ArchiveOutputStream;
20  import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
21  import org.apache.commons.compress.compressors.gzip.GzipParameters;
22  import org.eclipse.jgit.api.ArchiveCommand;
23  import org.eclipse.jgit.lib.FileMode;
24  import org.eclipse.jgit.lib.ObjectId;
25  import org.eclipse.jgit.lib.ObjectLoader;
26  
27  /**
28   * gzip-compressed tarball (tar.gz) format.
29   */
30  public final class TgzFormat extends BaseFormat implements
31  		ArchiveCommand.Format<ArchiveOutputStream> {
32  	private static final List<String> SUFFIXES = Collections
33  			.unmodifiableList(Arrays.asList(".tar.gz", ".tgz")); //$NON-NLS-1$ //$NON-NLS-2$
34  
35  	private final ArchiveCommand.Format<ArchiveOutputStream> tarFormat = new TarFormat();
36  
37  	/** {@inheritDoc} */
38  	@Override
39  	public ArchiveOutputStream createArchiveOutputStream(OutputStream s)
40  			throws IOException {
41  		return createArchiveOutputStream(s,
42  				Collections.<String, Object> emptyMap());
43  	}
44  
45  	/** {@inheritDoc} */
46  	@Override
47  	public ArchiveOutputStream createArchiveOutputStream(OutputStream s,
48  			Map<String, Object> o) throws IOException {
49  		GzipCompressorOutputStream out;
50  		int compressionLevel = getCompressionLevel(o);
51  		if (compressionLevel != -1) {
52  			GzipParameters parameters = new GzipParameters();
53  			parameters.setCompressionLevel(compressionLevel);
54  			out = new GzipCompressorOutputStream(s, parameters);
55  		} else {
56  			out = new GzipCompressorOutputStream(s);
57  		}
58  		return tarFormat.createArchiveOutputStream(out, o);
59  	}
60  
61  	/** {@inheritDoc} */
62  	@Override
63  	public void putEntry(ArchiveOutputStream out,
64  			ObjectId tree, String path, FileMode mode, ObjectLoader loader)
65  			throws IOException {
66  		tarFormat.putEntry(out, tree, path, mode, loader);
67  	}
68  
69  	/** {@inheritDoc} */
70  	@Override
71  	public Iterable<String> suffixes() {
72  		return SUFFIXES;
73  	}
74  
75  	/** {@inheritDoc} */
76  	@Override
77  	public boolean equals(Object other) {
78  		return (other instanceof TgzFormat);
79  	}
80  
81  	/** {@inheritDoc} */
82  	@Override
83  	public int hashCode() {
84  		return getClass().hashCode();
85  	}
86  }