22
33import org .apache .commons .io .FileUtils ;
44import java .io .IOException ;
5+ import java .io .InputStream ;
56import java .io .BufferedReader ;
67import java .io .InputStreamReader ;
78import java .io .File ;
89import java .net .URL ;
10+ import java .net .URLConnection ;
911import java .util .regex .Pattern ;
12+ import java .util .zip .GZIPInputStream ;
13+ import java .util .zip .ZipException ;
1014
1115class LocalBinary {
1216
@@ -174,7 +178,7 @@ private void downloadBinary(String destParentDir, Boolean custom) throws LocalEx
174178 URL url = new URL (httpPath );
175179
176180 File f = new File (source );
177- FileUtils . copyURLToFile (url , f );
181+ newCopyToFile (url , f );
178182
179183 changePermissions (binaryPath );
180184 } catch (Exception e ) {
@@ -192,4 +196,22 @@ private void changePermissions(String path) {
192196 public String getBinaryPath () {
193197 return binaryPath ;
194198 }
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+ }
195217}
0 commit comments