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.xz.XZCompressorOutputStream;
21  import org.eclipse.jgit.api.ArchiveCommand;
22  import org.eclipse.jgit.lib.FileMode;
23  import org.eclipse.jgit.lib.ObjectId;
24  import org.eclipse.jgit.lib.ObjectLoader;
25  
26  /**
27   * Xz-compressed tar (tar.xz) format.
28   */
29  public final class TxzFormat extends BaseFormat implements
30  		ArchiveCommand.Format<ArchiveOutputStream> {
31  	private static final List<String> SUFFIXES = Collections
32  			.unmodifiableList(Arrays.asList(".tar.xz", ".txz")); //$NON-NLS-1$ //$NON-NLS-2$
33  
34  	private final ArchiveCommand.Format<ArchiveOutputStream> tarFormat = new TarFormat();
35  
36  	/** {@inheritDoc} */
37  	@Override
38  	public ArchiveOutputStream createArchiveOutputStream(OutputStream s)
39  			throws IOException {
40  		return createArchiveOutputStream(s,
41  				Collections.<String, Object> emptyMap());
42  	}
43  
44  	/** {@inheritDoc} */
45  	@Override
46  	public ArchiveOutputStream createArchiveOutputStream(OutputStream s,
47  			Map<String, Object> o) throws IOException {
48  		XZCompressorOutputStream out;
49  		int compressionLevel = getCompressionLevel(o);
50  		if (compressionLevel != -1) {
51  			out = new XZCompressorOutputStream(s, compressionLevel);
52  		} else {
53  			out = new XZCompressorOutputStream(s);
54  		}
55  		return tarFormat.createArchiveOutputStream(out, o);
56  	}
57  
58  	/** {@inheritDoc} */
59  	@Override
60  	public void putEntry(ArchiveOutputStream out,
61  			ObjectId tree, String path, FileMode mode, ObjectLoader loader)
62  			throws IOException {
63  		tarFormat.putEntry(out, tree, path, mode, loader);
64  	}
65  
66  	/** {@inheritDoc} */
67  	@Override
68  	public Iterable<String> suffixes() {
69  		return SUFFIXES;
70  	}
71  
72  	/** {@inheritDoc} */
73  	@Override
74  	public boolean equals(Object other) {
75  		return (other instanceof TxzFormat);
76  	}
77  
78  	/** {@inheritDoc} */
79  	@Override
80  	public int hashCode() {
81  		return getClass().hashCode();
82  	}
83  }