|
46 | 46 | import org.json.JSONException;
|
47 | 47 |
|
48 | 48 | import java.io.ByteArrayOutputStream;
|
| 49 | +import java.io.File; |
| 50 | +import java.io.FileInputStream; |
| 51 | +import java.io.FileOutputStream; |
| 52 | +import java.io.FileNotFoundException; |
49 | 53 | import java.io.IOException;
|
50 | 54 | import java.io.InputStream;
|
51 | 55 | import java.net.URI;
|
|
56 | 60 | import java.util.List;
|
57 | 61 | import java.util.Map;
|
58 | 62 | import java.util.Set;
|
| 63 | +import java.util.zip.GZIPInputStream; |
59 | 64 |
|
60 | 65 |
|
61 | 66 |
|
@@ -95,6 +100,7 @@ class Utils {
|
95 | 100 | private static final String SEPARATOR = "\0";
|
96 | 101 | private static Set<String> mFavoriteUrls = new HashSet<>();
|
97 | 102 | private static Set<String> mBlockedUrls = new HashSet<>();
|
| 103 | + private static final int GZIP_SIGNATURE_LENGTH = 2; |
98 | 104 |
|
99 | 105 | // Compares PwPairs by first considering if it has been favorited
|
100 | 106 | // and then considering distance
|
@@ -1119,4 +1125,62 @@ public PwsResultBuilder setPwsTripTimeMillis(PwsResult pwsResult, long timeMilli
|
1119 | 1125 | return this;
|
1120 | 1126 | }
|
1121 | 1127 | }
|
| 1128 | + |
| 1129 | + /** |
| 1130 | + * Determines if a file is gzipped by examining the signature of |
| 1131 | + * the file, which is the first two bytes. |
| 1132 | + * @param file to be determined if is gzipped. |
| 1133 | + * @return true If the contents of the file are gzipped otherwise false. |
| 1134 | + */ |
| 1135 | + public static boolean isGzippedFile(File file) { |
| 1136 | + InputStream input; |
| 1137 | + |
| 1138 | + try { |
| 1139 | + input = new FileInputStream(file); |
| 1140 | + } catch(FileNotFoundException e) { |
| 1141 | + return false; |
| 1142 | + } |
| 1143 | + |
| 1144 | + byte[] signature = new byte[GZIP_SIGNATURE_LENGTH]; |
| 1145 | + |
| 1146 | + try { |
| 1147 | + input.read(signature); |
| 1148 | + } catch(IOException e) { |
| 1149 | + return false; |
| 1150 | + } |
| 1151 | + |
| 1152 | + return ((signature[0] == (byte) (GZIPInputStream.GZIP_MAGIC)) |
| 1153 | + && (signature[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8))); |
| 1154 | + } |
| 1155 | + |
| 1156 | + |
| 1157 | + /** |
| 1158 | + * Out-of-place Gunzips from src to dest. |
| 1159 | + * @param src file containing gzipped information. |
| 1160 | + * @param dest file to place decompressed information. |
| 1161 | + * @return File that has decompressed information. |
| 1162 | + */ |
| 1163 | + public static File gunzip(File src, File dest) { |
| 1164 | + |
| 1165 | + byte[] buffer = new byte[1024]; |
| 1166 | + |
| 1167 | + try{ |
| 1168 | + |
| 1169 | + GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(src)); |
| 1170 | + |
| 1171 | + FileOutputStream out = new FileOutputStream(dest); |
| 1172 | + |
| 1173 | + int len; |
| 1174 | + while ((len = gzis.read(buffer)) > 0) { |
| 1175 | + out.write(buffer, 0, len); |
| 1176 | + } |
| 1177 | + |
| 1178 | + gzis.close(); |
| 1179 | + out.close(); |
| 1180 | + |
| 1181 | + } catch(IOException ex){ |
| 1182 | + ex.printStackTrace(); |
| 1183 | + } |
| 1184 | + return dest; |
| 1185 | + } |
1122 | 1186 | }
|
0 commit comments