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

Last change on this file since 6883 was 6882, checked in by Don-vip, 10 years ago

enable loading of .osm.zip files

File size: 3.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import org.apache.tools.bzip2.CBZip2OutputStream;
5import org.openstreetmap.josm.tools.Utils;
6
7import java.io.File;
8import java.io.FileInputStream;
9import java.io.FileOutputStream;
10import java.io.IOException;
11import java.io.InputStream;
12import java.io.OutputStream;
13import java.net.URL;
14import java.util.zip.GZIPOutputStream;
15import java.util.zip.ZipOutputStream;
16
17/**
18 * An enum representing the compression type of a resource.
19 */
20public enum Compression {
21 /**
22 * no compression
23 */
24 NONE,
25 /**
26 * bzip2 compression
27 */
28 BZIP2,
29 /**
30 * gzip compression
31 */
32 GZIP,
33 /**
34 * zip compression
35 */
36 ZIP;
37
38 /**
39 * Determines the compression type depending on the suffix of {@code name}.
40 * @param name File name including extension
41 * @return the compression type
42 */
43 public static Compression byExtension(String name) {
44 return name != null && name.endsWith(".gz")
45 ? GZIP
46 : name != null && (name.endsWith(".bz2") || name.endsWith(".bz"))
47 ? BZIP2
48 : name != null && name.endsWith(".zip")
49 ? ZIP
50 : NONE;
51 }
52
53 /**
54 * Returns an un-compressing {@link InputStream} for {@code in}.
55 *
56 * @throws IOException
57 */
58 public InputStream getUncompressedInputStream(InputStream in) throws IOException {
59 switch (this) {
60 case BZIP2:
61 return FileImporter.getBZip2InputStream(in);
62 case GZIP:
63 return FileImporter.getGZipInputStream(in);
64 case ZIP:
65 return FileImporter.getZipInputStream(in);
66 case NONE:
67 default:
68 return in;
69 }
70 }
71
72 /**
73 * Returns an un-compressing {@link InputStream} for the {@link File} {@code file}.
74 *
75 * @throws IOException
76 */
77 public static InputStream getUncompressedFileInputStream(File file) throws IOException {
78 return byExtension(file.getName()).getUncompressedInputStream(new FileInputStream(file));
79 }
80
81 /**
82 * Returns an un-compressing {@link InputStream} for the {@link URL} {@code url}.
83 *
84 * @throws IOException
85 */
86 public static InputStream getUncompressedURLInputStream(URL url) throws IOException {
87 return Utils.openURLAndDecompress(url, true);
88 }
89
90 /**
91 * Returns a compressing {@link OutputStream} for {@code out}.
92 *
93 * @throws IOException
94 */
95 public OutputStream getCompressedOutputStream(OutputStream out) throws IOException {
96 switch (this) {
97 case BZIP2:
98 out.write('B');
99 out.write('Z');
100 return new CBZip2OutputStream(out);
101 case GZIP:
102 return new GZIPOutputStream(out);
103 case ZIP:
104 return new ZipOutputStream(out);
105 case NONE:
106 default:
107 return out;
108 }
109 }
110
111 /**
112 * Returns a compressing {@link OutputStream} for the {@link File} {@code file}.
113 *
114 * @throws IOException
115 */
116 public static OutputStream getCompressedFileOutputStream(File file) throws IOException {
117 return byExtension(file.getName()).getCompressedOutputStream(new FileOutputStream(file));
118 }
119}
Note: See TracBrowser for help on using the repository browser.