Federico Scardina
Federico Scardina

Reputation: 150

Java - gz archive downloaded as octet stream

I'm encountering a problem while downloading a gzip file and saving it in the file system with Java 5. When the download is finished, the file (which has the correct name and extension) appears to have a different MIME type... I'm unable to unzip it from my Linux server (using gunzip) and if I try to open it with WinZip on my Windows pc I see a "recursive" archive (like a matryoshka doll). If I type the command file *filename*.gz from the server, it recognizes the file as an ascii text. Instead, if I try to download the archive using the browser, everything goes well and I can correctly open and unzip the file (even with my Linux server) and now it's recognized as a gzip compressed archive.

Here's the code I'm using to download the file and save it.

Main.java:

public class Main {

public static void main(String[] args) {

    String filePath = "";
    HttpOutgoingCall httpOngoingCall = null;
    httpOngoingCall = new HttpOutgoingCall();
    String endpointUrl = "https://myurl/myfile.gz";
    try {

        InputStream inputStream = httpOngoingCall.callHttps(endpointUrl);
        //I also tried with ZipInputStream and GZIPInputStream

        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

        filePath = parseAndWriteResponse(br, "myfile.gz", "C:\\");

        System.out.println(filePath);

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}


private static String parseAndWriteResponse(BufferedReader br, String fileName,
                                     String destPath) {
    String outputFileName = null;

    outputFileName = destPath + File.separator + fileName;

    String line;
    File outputFile = new File(outputFileName);
    FileWriter fileWriter = null;
    BufferedWriter bw = null;

    try {
        if (!outputFile.exists()) {
            outputFile.createNewFile();
        }
    } catch (IOException e1) {

    }

    try {
        fileWriter = new FileWriter(outputFile);
        bw = new BufferedWriter(fileWriter);
        while ((line = br.readLine()) != null) {
            bw.write(line);
            bw.write("\n");
        }
    } catch (IOException e) {

    } finally {
        try {
            bw.close();
            fileWriter.close();
        } catch (IOException e) {

        }
    }
    return outputFileName;
}

HttpOutgoingCall.java:

public class HttpOutgoingCall {

private InputStream inStream = null;

private HttpsURLConnection httpsConnection = null;


private final static int CONNECTION_TIMEOUT = 20000;


public InputStream callHttps(String endpointUrl) throws Exception {

    String socksServer = "";
    String socksPort = "";

    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

    Properties properties = System.getProperties();

    System.setProperty("java.protocol.handler.pkgs", "javax.net.ssl");
    java.security.Security
            .addProvider(new com.sun.net.ssl.internal.ssl.Provider());

    if (!socksServer.equals("")) {
        if (System.getProperty("socksProxyHost") == null) {
            properties.put("socksProxyHost", socksServer);
        }
        if (!socksPort.equals("")) {
            if (System.getProperty("socksProxyPort") == null) {
                properties.put("socksProxyPort", socksPort);
            }
        }
    }
    System.setProperties(properties); 

    System.setProperty("java.protocol.handler.pkgs", "javax.net.ssl");
    java.security.Security
            .addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {

            return null;
        }

        public void checkClientTrusted(
                java.security.cert.X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(
                java.security.cert.X509Certificate[] certs, String authType) {
        }
    } };

    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection
                .setDefaultSSLSocketFactory(sc.getSocketFactory());

        HostnameVerifier hv = new HostnameVerifier() {
            public boolean verify(String urlHostName, SSLSession session) {
                return true;
            }
        };

        HttpsURLConnection.setDefaultHostnameVerifier(hv);

        httpsConnection = (HttpsURLConnection) (new URL(endpointUrl)).openConnection();

        httpsConnection.setDoOutput(true);
        httpsConnection.setUseCaches(false);

        httpsConnection.setConnectTimeout(CONNECTION_TIMEOUT);
        httpsConnection.setReadTimeout(CONNECTION_TIMEOUT);

        inStream = httpsConnection.getInputStream();

    } catch (Exception e) {}

    return inStream;
}

Could someone help me? Thanks!

Upvotes: 0

Views: 484

Answers (1)

BrentR
BrentR

Reputation: 928

When writing your file, you should send it through a java.util.zip.GZIPOutputStream.

Upvotes: 1

Related Questions