2
2
3
3
import org .apache .commons .io .FileUtils ;
4
4
import java .io .IOException ;
5
+ import java .io .InputStream ;
5
6
import java .io .BufferedReader ;
6
7
import java .io .InputStreamReader ;
7
8
import java .io .File ;
8
9
import java .net .URL ;
10
+ import java .net .URLConnection ;
9
11
import java .util .regex .Pattern ;
12
+ import java .util .zip .GZIPInputStream ;
13
+ import java .util .zip .ZipException ;
10
14
11
15
class LocalBinary {
12
16
@@ -174,7 +178,7 @@ private void downloadBinary(String destParentDir, Boolean custom) throws LocalEx
174
178
URL url = new URL (httpPath );
175
179
176
180
File f = new File (source );
177
- FileUtils . copyURLToFile (url , f );
181
+ newCopyToFile (url , f );
178
182
179
183
changePermissions (binaryPath );
180
184
} catch (Exception e ) {
@@ -192,4 +196,22 @@ private void changePermissions(String path) {
192
196
public String getBinaryPath () {
193
197
return binaryPath ;
194
198
}
199
+
200
+ private static void newCopyToFile (URL url , File f ) throws IOException {
201
+ URLConnection conn = url .openConnection ();
202
+ conn .setRequestProperty ("User-Agent" , "browserstack-local-java/" + Local .getPackageVersion ());
203
+ conn .setRequestProperty ("Accept-Encoding" , "gzip, *" );
204
+ String contentEncoding = conn .getContentEncoding ();
205
+
206
+ if (contentEncoding == null || !contentEncoding .toLowerCase ().contains ("gzip" )) {
207
+ FileUtils .copyToFile (conn .getInputStream (), f );
208
+ return ;
209
+ }
210
+
211
+ try (InputStream stream = new GZIPInputStream (conn .getInputStream ())) {
212
+ FileUtils .copyToFile (stream , f );
213
+ } catch (ZipException e ) {
214
+ FileUtils .copyURLToFile (url , f );
215
+ }
216
+ }
195
217
}
0 commit comments