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