source: josm/trunk/src/org/openstreetmap/josm/io/Compression.java@ 13751

Last change on this file since 13751 was 13350, checked in by stoecker, 6 years ago

see #15816 - add XZ support

  • Property svn:eol-style set to native
File size: 7.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import java.io.File;
5import java.io.IOException;
6import java.io.InputStream;
7import java.io.OutputStream;
8import java.nio.charset.StandardCharsets;
9import java.nio.file.Files;
10import java.util.zip.GZIPInputStream;
11import java.util.zip.GZIPOutputStream;
12import java.util.zip.ZipEntry;
13import java.util.zip.ZipInputStream;
14import java.util.zip.ZipOutputStream;
15
16import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
17import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
18import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
19import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream;
20import org.openstreetmap.josm.tools.Logging;
21import org.openstreetmap.josm.tools.Utils;
22
23/**
24 * An enum representing the compression type of a resource.
25 */
26public enum Compression {
27 /**
28 * no compression
29 */
30 NONE,
31 /**
32 * bzip2 compression
33 */
34 BZIP2,
35 /**
36 * gzip compression
37 */
38 GZIP,
39 /**
40 * zip compression
41 */
42 ZIP,
43 /**
44 * xz compression
45 */
46 XZ;
47
48 /**
49 * Determines the compression type depending on the suffix of {@code name}.
50 * @param name File name including extension
51 * @return the compression type
52 */
53 public static Compression byExtension(String name) {
54 return name != null && name.endsWith(".gz")
55 ? GZIP
56 : name != null && (name.endsWith(".bz2") || name.endsWith(".bz"))
57 ? BZIP2
58 : name != null && name.endsWith(".zip")
59 ? ZIP
60 : name != null && name.endsWith(".xz")
61 ? XZ
62 : NONE;
63 }
64
65 /**
66 * Determines the compression type based on the content type (MIME type).
67 * @param contentType the content type
68 * @return the compression type
69 */
70 public static Compression forContentType(String contentType) {
71 switch (contentType) {
72 case "application/zip":
73 return ZIP;
74 case "application/x-gzip":
75 return GZIP;
76 case "application/x-bzip2":
77 return BZIP2;
78 case "application/x-xz":
79 return XZ;
80 default:
81 return NONE;
82 }
83 }
84
85 /**
86 * Returns an un-compressing {@link InputStream} for {@code in}.
87 * @param in raw input stream
88 * @return un-compressing input stream
89 *
90 * @throws IOException if any I/O error occurs
91 */
92 public InputStream getUncompressedInputStream(InputStream in) throws IOException {
93 switch (this) {
94 case BZIP2:
95 return getBZip2InputStream(in);
96 case GZIP:
97 return getGZipInputStream(in);
98 case ZIP:
99 return getZipInputStream(in);
100 case XZ:
101 return getXZInputStream(in);
102 case NONE:
103 default:
104 return in;
105 }
106 }
107
108 /**
109 * Returns a XZ input stream wrapping given input stream.
110 * @param in The raw input stream
111 * @return a XZ input stream wrapping given input stream, or {@code null} if {@code in} is {@code null}
112 * @throws IOException if the given input stream does not contain valid BZ2 header
113 * @since 13350
114 */
115 public static XZCompressorInputStream getXZInputStream(InputStream in) throws IOException {
116 if (in == null) {
117 return null;
118 }
119 return new XZCompressorInputStream(in, true);
120 }
121
122 /**
123 * Returns a Bzip2 input stream wrapping given input stream.
124 * @param in The raw input stream
125 * @return a Bzip2 input stream wrapping given input stream, or {@code null} if {@code in} is {@code null}
126 * @throws IOException if the given input stream does not contain valid BZ2 header
127 * @since 12772 (moved from {@link Utils}, there since 7867)
128 */
129 public static BZip2CompressorInputStream getBZip2InputStream(InputStream in) throws IOException {
130 if (in == null) {
131 return null;
132 }
133 return new BZip2CompressorInputStream(in, /* see #9537 */ true);
134 }
135
136 /**
137 * Returns a Gzip input stream wrapping given input stream.
138 * @param in The raw input stream
139 * @return a Gzip input stream wrapping given input stream, or {@code null} if {@code in} is {@code null}
140 * @throws IOException if an I/O error has occurred
141 * @since 12772 (moved from {@link Utils}, there since 7119)
142 */
143 public static GZIPInputStream getGZipInputStream(InputStream in) throws IOException {
144 if (in == null) {
145 return null;
146 }
147 return new GZIPInputStream(in);
148 }
149
150 /**
151 * Returns a Zip input stream wrapping given input stream.
152 * @param in The raw input stream
153 * @return a Zip input stream wrapping given input stream, or {@code null} if {@code in} is {@code null}
154 * @throws IOException if an I/O error has occurred
155 * @since 12772 (moved from {@link Utils}, there since 7119)
156 */
157 public static ZipInputStream getZipInputStream(InputStream in) throws IOException {
158 if (in == null) {
159 return null;
160 }
161 ZipInputStream zis = new ZipInputStream(in, StandardCharsets.UTF_8);
162 // Positions the stream at the beginning of first entry
163 ZipEntry ze = zis.getNextEntry();
164 if (ze != null && Logging.isDebugEnabled()) {
165 Logging.debug("Zip entry: {0}", ze.getName());
166 }
167 return zis;
168 }
169
170 /**
171 * Returns an un-compressing {@link InputStream} for the {@link File} {@code file}.
172 * @param file file
173 * @return un-compressing input stream
174 * @throws IOException if any I/O error occurs
175 */
176 public static InputStream getUncompressedFileInputStream(File file) throws IOException {
177 InputStream in = Files.newInputStream(file.toPath());
178 try {
179 return byExtension(file.getName()).getUncompressedInputStream(in);
180 } catch (IOException e) {
181 Utils.close(in);
182 throw e;
183 }
184 }
185
186 /**
187 * Returns a compressing {@link OutputStream} for {@code out}.
188 * @param out raw output stream
189 * @return compressing output stream
190 *
191 * @throws IOException if any I/O error occurs
192 */
193 public OutputStream getCompressedOutputStream(OutputStream out) throws IOException {
194 switch (this) {
195 case BZIP2:
196 return new BZip2CompressorOutputStream(out);
197 case GZIP:
198 return new GZIPOutputStream(out);
199 case ZIP:
200 return new ZipOutputStream(out, StandardCharsets.UTF_8);
201 case XZ:
202 return new XZCompressorOutputStream(out);
203 case NONE:
204 default:
205 return out;
206 }
207 }
208
209 /**
210 * Returns a compressing {@link OutputStream} for the {@link File} {@code file}.
211 * @param file file
212 * @return compressing output stream
213 *
214 * @throws IOException if any I/O error occurs
215 */
216 public static OutputStream getCompressedFileOutputStream(File file) throws IOException {
217 OutputStream out = Files.newOutputStream(file.toPath());
218 try {
219 return byExtension(file.getName()).getCompressedOutputStream(out);
220 } catch (IOException e) {
221 Utils.close(out);
222 throw e;
223 }
224 }
225}
Note: See TracBrowser for help on using the repository browser.